17

I am trying to get a simple JSF 2 tutorial example to work.

I am using the dynamic web project in Eclipse and publishing to a Glassfish 3 server (run -> run on server). The first index.xhtml page loads correctly, but when I have to access a managed bean, the following error displays:

/index.xhtml @14,48 value="#{helloBean.name}": Target Unreachable, identifier 'helloBean' resolved to null

I've had a look at the various other discussions on this topic, however the solutions never seem to work for me (e.g. adding beans.xml, giving the managed bean a name etc, following naming conventions).

Any help would be appreciated.

Here is the code I am currently working with:

Index.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!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:f="http://java.sun.com/jsf/core"      
      xmlns:h="http://java.sun.com/jsf/html">

    <h:head>
        <title>JSF 2.0 Hello World</title>
    </h:head>
    <h:body>
        <h3>JSF 2.0 Hello World Example - hello.xhtml</h3>
        <h:form>
           <h:inputText value="#{helloBean.name}"></h:inputText>
           <h:commandButton value="Welcome Me" action="response"></h:commandButton>
        </h:form>
    </h:body>
</html>

response.xhtml:

<?xml version="1.0" encoding="UTF-8"?>
<!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 2.0 Hello World</title>
    </h:head>
    <h:body bgcolor="white">
        <h3>JSF 2.0 Hello World Example - welcome.xhtml</h3>
        <h4>Welcome #{helloBean.name}</h4>
    </h:body>
</html>

Managed bean:

package java.hello1;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import java.io.Serializable;


@ManagedBean
@SessionScoped
public class HelloBean implements Serializable {

    private static final long serialVersionUID = 1L;

    private String name = "Ricardo";

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

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">

  <display-name>JavaServerFaces</display-name>

  <!-- Change to "Production" when you are ready to deploy -->
  <context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
  </context-param>

  <!-- Welcome page -->
  <welcome-file-list>
    <welcome-file>faces/index.xhtml</welcome-file>
  </welcome-file-list>

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

  <!-- Map these files with JSF -->
  <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>
Elrond_EGLDer
  • 47,430
  • 25
  • 189
  • 180
Lou Rous
  • 231
  • 1
  • 3
  • 5
  • 4
    If you are running your code using maven, try run it with maven goal `tomcat:run-war` instead of `tomcat:run`. Hope this help. :) – huahsin68 Oct 28 '12 at 08:22
  • @stiv: then you've not the same problem as the OP. Have you copy'n'paste'n'runned his code in a completely blank playground project with everything set to default? – BalusC Apr 27 '13 at 16:17
  • I've created project with Intellij IDEA, added support for JSF, but it doesn't want to see my bean. At the same time – Stepan Yakovenko Apr 27 '13 at 19:01
  • @stiv: if you used **exactly** the same code and environment setup (Eclipse + Glassfish 3) then I don't see other causes to the problem than already answered. Conclusion is then that you don't have **exactly** the same problem as the OP. – BalusC Apr 30 '13 at 10:19

6 Answers6

14

You need to have a JSF 2.0 compliant /WEB-INF/faces-config.xml file in order to get JSF to interpret the annotations.

<?xml version="1.0" encoding="UTF-8"?>
<faces-config
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
    version="2.0">

    <!-- Config here. Can even be kept empty. -->

</faces-config>

If you already have one or if that doesn't solve the problem, please pay attention to server startup logs if you don't see any warnings/errors.

By the way, your /WEB-INF/web.xml file is declared conform Servlet 2.5 specification. While this may not necessarily harm, it makes no sense if you're using a Servlet 3.0 compliant container. Update the root declaration as follows:

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

    <!-- Config here. -->

</web-app>

The /WEB-INF/beans.xml is intented for CDI annotations like @Named, @Inject and so on. Just a completely empty file is sufficient to turn it on. It has totally no relationship to JSF annotations like @ManagedBean, @ManagedProperty and so on. It should also not be confused/mixed with each other.

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
10

I've been stuck with such a problem for half a day. The problem in my case appears only when I do run the WebApp from Eclipse. JSF 2 looks in WEB-INF/classes for annotated beans and doesn't find them. To solve this I've changed build output path to WebContent/WEB-INF/classes. Here is detailed explanation of similar case: Jetty maven goal jetty:run does not work with JSF 2.0 @ManagedBean

alehro
  • 2,131
  • 2
  • 24
  • 38
  • This is the fault of the build tool and/or the server plugin. Eclipse in its default trim (without Maven or any custom server plugins) does the job rightly. – BalusC Apr 19 '12 at 15:43
  • for anyone trying mkyong example (as it is in 2015) http://www.mkyong.com/jsf2/jsf-2-0-hello-world-example/ that is the solution!! – Ilya Yevlampiev Oct 18 '15 at 18:29
1

In the exact same scenario as OP (with eclipse, publishing to a Glassfish 3 server using run -> run on server) I get exactly the same error until removing spaces from the eclipse Project Name. Simply removing any spaces solved the problem.

Forge_7
  • 1,659
  • 1
  • 18
  • 17
1

I had the same problem, i tried everything. After that, i just clicked "Click and Build Project" button, "Build Project" button and resarted GlassFish server. After, it works to me, for now :)

umitkilic
  • 135
  • 2
  • 15
0

Please you check your war file and see if your classes are under WEB-INF/classes folder. I ran into the same problem and found out there are no class files WEB-INF/classes folder.

sendon1982
  • 7,088
  • 42
  • 36
-1

Include @Named in your Controller/Bean..

  • This is indeed one of possible answers to the problem as described in the question title, but this does not answer the problem as described in the question body itself. – BalusC Sep 05 '14 at 14:24
  • I got the error when I used Named annotation on my hello bean. It got resolved by using @ManagedBean annotation. – Hetal Rachh Jan 15 '19 at 10:19