5

I just started using JSF, and i encountered this problem when submitting the index.xhtml:

/index.xhtml @11,65 value="#{user.name}": Target Unreachable, identifier 'User' resolved to null

Here are my 3 files: 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:h="http://xmlns.jcp.org/jsf/html">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        Hello from Facelets
        <h:form>
            <h:inputText  id="inputText" value="#{user.name}" />
            <h:commandButton id="submit" value="Submit" action="home"/>
        </h:form>
    </h:body>
</html>

home.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://xmlns.jcp.org/jsf/html">
    <h:head>
        <title>Home</title>
    </h:head>
    <h:body>
        Welcome.
        <h:form>
            <h4>#{user.name}</h4>
        </h:form>
    </h:body>
</html>

User.java

package com.jsf;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name = "user")
@SessionScoped
public class User {
    private String name = "";

    public String getName() {
        return name;
    }

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

and faces-config.xml

<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
              xmlns="http://xmlns.jcp.org/xml/ns/javaee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">

    <managed-bean>
        <managed-bean-name>User</managed-bean-name>
        <managed-bean-class>com.jsf.User</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
    </managed-bean>.
</faces-config>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <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>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
    </welcome-file-list>
</web-app>

Im using Netbeans 8, and JSF 2.2 I know there are many other questions with the same title, but none of them worked.

Ouerghi Yassine
  • 1,646
  • 7
  • 39
  • 66
  • 1
    Why are you using both annotations and a `faces-config.xml` to specify your bean class? And why are the scopes are different. – Robby Cornelissen Jul 03 '14 at 14:54
  • i added the `faces-config.xml` after searching on google, but that didn't seem to work either :/ – Ouerghi Yassine Jul 03 '14 at 14:54
  • 1
    When do you get this error, at opening index.xhtml or home.xhtml? Besides that, you should only configure the managed bean using annotations or XML in faces-config, not both. – Luiggi Mendoza Jul 03 '14 at 14:55
  • @RobbyCornelissen careful, this doesn't look like the error since annotation configured managed bean is `user` (lowercase) and XML configured managed bean is `User` (first letter uppercase). – Luiggi Mendoza Jul 03 '14 at 14:56
  • im getting that when submitting the index.xhtml, and even if i remove the xml file, i get the same error, ill remove it anyways. – Ouerghi Yassine Jul 03 '14 at 14:56
  • Please post your web.xml to see the FacesServlet configuration. Also, provide the whole stacktrace. – Luiggi Mendoza Jul 03 '14 at 14:57
  • @LuiggiMendoza edited, added the web.xml. – Ouerghi Yassine Jul 03 '14 at 14:58
  • 2
    Ok, everything looks *fine* except for the double configuration of the managed bean. Please remove it, clean your project, re build it, generate a new war file, redeploy it and test again. – Luiggi Mendoza Jul 03 '14 at 15:00
  • 1
    awh .. it worked .. dont know what i did there, but i had the same error before i add the faces-config .. i removed it and rebuild, it works now, thank you. – Ouerghi Yassine Jul 03 '14 at 15:01

2 Answers2

7

I want to share my experience with this Exception. My JSF 2.2 application worked fine with WildFly 8.0, but one time, when I started server, i got this "Target Unreacheable" exception. Actually, there was no problem with JSF annotations or tags.

Only thing I had to do was cleaning the project. After this operation, my app is working fine again.

I hope this will help someone!

akelec
  • 3,075
  • 3
  • 36
  • 35
  • 1
    One day I was debugging and my working project started throwing this error, out of the blue. **Restarting the Glassfish server** fixed it. – Rajat Saxena Sep 18 '16 at 12:23
2

The problem seems related to the double configuration of your managed bean with the same name:

  • Annotation configuration by using @ManagedBean and @SessionScoped annotations on the class called user.
  • XML configuration in faces-config.xml file through the <managed-bean> node called User.

You should have a single configuration per managed bean i.e. the name. In this case, since you basically have the same managed bean configured twice, just remove one of the configurations. I recommend removing the XML configuration since the annotation configuration is more readable and understandable.

Luiggi Mendoza
  • 81,685
  • 14
  • 140
  • 306