Target Unreachable, identifier 'user' resolved to null

流过昼夜 提交于 2020-01-07 09:50:28

问题


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.



来源:https://stackoverflow.com/questions/34707083/target-unreachable-identifier-user-resolved-to-null

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!