Target Unreachable, identifier 'user' reso

2019-09-21 18:06发布

问题:

So I have this very weird issue... I have tried searching all over StackOverflow and tried everything I could find, but I simply cannot fix this error from happening. The error is:

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

It's a school project and the project works for some of my classmates, but it also won't work for some. Could it be NetBeans screwing up when we open them? So far at least 2 people got it working, but at least 2 people also got this error.

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://java.sun.com/jsf/html">
    <h:head>
        <title>Welcome</title>
    </h:head>
    <h:body>
        <h:form>
            <h3>Please enter your name and password.</h3>   
            <table>
                <tr>
                    <td>Name:</td>
                    <td><h:inputText value="#{user.name}"/></td>
                    <td>.</td>
                    <td><h:inputText value="#{user.userId}"/></td>
                </tr>
                <tr>
                    <td>Password:</td>
                    <td><h:inputSecret value="#{user.password}"/></td>
                </tr>
            </table>
            <p><h:commandButton value="Login" action="welcome"/></p>
        </h:form>
    </h:body>
</html>

welcome.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>Welcome</title>
    </h:head>
    <h:body>
        <h3>Welcome to JavaServer Faces, #{user.name}.#{user.userId}!</h3>
        <h3>your password is #{user.password}</h3>
    </h:body>
    <h:form>
        <h:commandButton value="Logout" action="index"/>
    </h:form>
</html>

UserBean.java

package com.corejsf;

import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;

@Named("user")
@SessionScoped
public class UserBean implements Serializable {

    private String name;
    private String password;
    private Integer userId;

    public String getName() {
        return name;
    }

    public void setName(String newValue) {
        name = newValue;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String newValue) {
        password = newValue;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer newValue) {
        userId = newValue;
    }

    public String logout() {
        FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
        return "index";
    }
}

What it basically does, is take any username, id, and password on index.xhtml. Then you press login, which sends you over to welcome.xhtml (because of action="welcome"). welcome.xhtml should then post your username, id, and password on the page. However, when I press the login button, I get this error:

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

I did try switching to ManagedBean (@ManagedBean(name="user")) but that resulted in the same thing.

回答1:

I found a fix for this. Apparently, for some of us.., the imported project name was not matching the <context-root> element's name. All we have to do, is go into WEB-INF and find glassfish-web.xml. In that file, check what is inside <context-root> and either replace that with your project name or simply rename your project to whatever is inside that element.

Took a few hours to figure that out, but hopefully others will find this and appreciate my answer.



标签: jsf jsf-2.2