目标无法到达,标识符“用户"解析为空 [英] Target Unreachable, identifier 'user' resolved to null

查看:137
本文介绍了目标无法到达,标识符“用户"解析为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个非常奇怪的问题……我试图在整个StackOverflow上进行搜索,并尝试了所有可以找到的东西,但是我根本无法解决此错误.错误是:

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}":无法到达目标,标识符'user'解析为空

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

这是一个学校项目,该项目适用于我的一些同学,但也不适用于某些同学.当我们打开NetBeans时可能会搞砸吗?到目前为止,至少有2个人可以使用它,但是至少有2个人也有此错误.

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

它的基本作用是在index.xhtml上输入任何用户名,ID和密码.然后按登录,将您发送到welcome.xhtml(由于action="welcome").然后,welcome.xhtml应该在页面上发布您的用户名,用户名和密码.但是,当我按下登录按钮时,出现此错误:

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}":无法到达目标,标识符'user'解析为空

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

我确实尝试过切换到ManagedBean(@ManagedBean(name="user")),但这导致了同样的事情.

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

推荐答案

我为此找到了解决方法.显然,对于我们中的某些人来说,导入的项目名称与<context-root>元素的名称不匹配.我们要做的就是进入WEB-INF并找到glassfish-web.xml.在该文件中,检查<context-root>中的内容,然后将其替换为您的项目名称,或者将您的项目重命名为该元素中的任何内容.

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.

这篇关于目标无法到达,标识符“用户"解析为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆