0

I'm using IntelliJ Ultimate free trial, Maven 3.5.0, Tomcat 6.0.53, Java EE web profile which I downloaded and unzipped (that's all it said to do). I started a Maven project. My program will only print the heading "Test" but not the bean.

In the home.xhtml, I originally tried to output: #{helloWorld.getMessage()}

This would be colored pink with a message: "JSF EL out of attribute." If I run, I get a crash message: "Encountered " "(" "( "" at line 1, column 24"

I changed the code to what you now see below. It runs, but does not access the bean, only prints the heading "Test".

Any ideas, please?

home.xhtml:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns = "http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>JSF Tutorial!</title>
</h:head>

<h:body>
<h1>Test </h1>
<h:outputText value="#{helloWorld.message}"/>

</h:body>
</html>

HelloWorld.java:

package com.tutorialspoint.test;

import javax.faces.bean.ManagedBean;

@ManagedBean(name = "helloWorld", eager = true)
public class HelloWorld {

public String message = "This is the string from class HelloWorld";

public HelloWorld() {
    System.out.println("HelloWorld started!");
}

public String getMessage() {
    return message;
}
}

web.xml:

<?xml version = "1.0" encoding = "UTF-8"?>
<web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
     xmlns = "http://java.sun.com/xml/ns/javaee"
     xmlns:web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     id = "WebApp_ID" version="2.5">

<welcome-file-list>
<welcome-file>faces/home.xhtml</welcome-file>
</welcome-file-list>

<!--
 FacesServlet is main servlet responsible to handle all request.
 It acts as central controller.
 This servlet initializes the JSF components before the JSP is displayed.
-->

<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

</web-app>

pom.xml:

<project xmlns = "http://maven.apache.org/POM/4.0.0"
     xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>com.tutorialspoint.test</groupId>
<artifactId>helloworld</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>helloworld Maven Webapp</name>
<url>http://maven.apache.org</url>

<dependencies>
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>3.8.1</version>
  <scope>test</scope>
</dependency>

<dependency>
  <groupId>com.sun.faces</groupId>
  <artifactId>jsf-api</artifactId>
  <version>2.1.7</version>
</dependency>

<dependency>
  <groupId>com.sun.faces</groupId>
  <artifactId>jsf-impl</artifactId>
  <version>2.1.7</version>
</dependency>

</dependencies>

<build>
<finalName>helloworld</finalName>
<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.1</version>

    <configuration>
      <source>1.6</source>
      <target>1.6</target>
    </configuration>
  </plugin>


  <plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat6-maven-plugin</artifactId>
    <version>2.2</version>

    <configuration>
      <port>9999</port>
      <path>/</path>
    </configuration>
  </plugin>


</plugins>

</build>
</project>
Ken
  • 139
  • 12
  • I have run your sample and it is OK. How are you publishing the app? – C.P.O Sep 10 '17 at 17:02
  • I'm not even sure what that means. I run it in IntelliJ and then I click the link that shows up in the output. It brings up the browser. – Ken Sep 10 '17 at 17:08
  • I have commented the plugin in the pom.xml and works (http://localhost:8080/helloworld/), try that! – C.P.O Sep 10 '17 at 17:15
  • Still doesn't work. How did you create this project? Also, is there anything else I need to set up in the path? – Ken Sep 10 '17 at 17:21
  • 1
    Please use better tutorials than tutorialspoint. See the comment on https://stackoverflow.com/questions/38930106/jsp-file-uploading. E.g. if you are starting new with jsf, use jsf 2.2, cdi (and not jsf managed beans)... – Kukeltje Sep 10 '17 at 19:18
  • To Kukeltje: The link you provided wouldn't be relevant now since the previous person (C.P.O) was able to run my app. So there's something different on his computer, either the way he created the project or the way his applications (Java EE, etc.) are set up. I can't figure out what the difference is. – Ken Sep 10 '17 at 19:39
  • Kukeltje has a valid point: if you had read an authoritative tutorial/book instead of a random code snippet scraping site, you'd not have faced this problem in first place. Start here: https://stackoverflow.com/q/1958808 – BalusC Sep 11 '17 at 06:33
  • And how is a beginner supposed to know what are good or bad lessons? I've searched the internet extensively for a solution and nothing tells me to create this little program differently. It's very basic so if you know what I should adjust, please let me know. – Ken Sep 11 '17 at 12:24
  • The problem is that Google puts everyone off to a wrong start. The number of hits/click (amongst others) determines ranking. So because of that it is not strange. But there are always the Oracle/java tutorials. Those are really authoritative, up to date and more. But then again, it does not dismiss you from looking for what version a tutorial was written, if there are newer things, what has changed (jsf 2.1.7 is 5 years old -> 2.1.29 (or 2.3), web.xml 2.5 is 9 years old jsf managed beans superseded by CDI... All of which I already suggested!. So we don't blame you, it is just an advice – Kukeltje Sep 11 '17 at 13:14
  • And not to mention Tomcat 6 being superseded by Tomcat 8 and Tomcat 9 almost being around... (or choose a real Java-EE container)... – Kukeltje Sep 11 '17 at 13:16
  • I FOUND THE PROBLEM!! WHOO-HOO!! I didn't set up my Tomcat configuration properly. I was supposed to add a Tomcat Server configuration, instead I had reused the Maven Tomcat configuration from a previous tutorial. – Ken Sep 11 '17 at 18:29

0 Answers0