目标无法到达,标识符 'user' 解析为空
Target Unreachable, identifier 'user' resolved to null
所以我遇到了这个非常奇怪的问题...我已经尝试在整个 Whosebug 上搜索并尝试了所有我能找到的东西,但我就是无法解决这个错误的发生。错误是:
/index.xhtml @15,60 value="#{user.name}": Target Unreachable, identifier 'user' resolved to null
这是一个学校项目,该项目适用于我的一些同学,但也不适用于某些同学。会不会是 NetBeans 在我们打开它们时搞砸了?到目前为止,至少有 2 人成功了,但至少有 2 人也遇到了这个错误。
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 然后应该 post 您在页面上的用户名、ID 和密码。但是,当我按下登录按钮时,出现此错误:
/index.xhtml @15,60 value="#{user.name}": Target Unreachable, identifier 'user' resolved to null
我确实尝试切换到 ManagedBean (@ManagedBean(name="user")
),但结果是一样的。
我找到了解决办法。显然,对于我们中的一些人……,导入的项目名称与 <context-root>
元素的名称不匹配。我们所要做的就是进入 WEB-INF 并找到 glassfish-web.xml
。在该文件中,检查 <context-root>
中的内容,并将其替换为您的项目名称,或者将您的项目重命名为该元素中的任何内容。
花了几个小时才弄明白,但希望其他人会发现这一点并感谢我的回答。
所以我遇到了这个非常奇怪的问题...我已经尝试在整个 Whosebug 上搜索并尝试了所有我能找到的东西,但我就是无法解决这个错误的发生。错误是:
/index.xhtml @15,60 value="#{user.name}": Target Unreachable, identifier 'user' resolved to null
这是一个学校项目,该项目适用于我的一些同学,但也不适用于某些同学。会不会是 NetBeans 在我们打开它们时搞砸了?到目前为止,至少有 2 人成功了,但至少有 2 人也遇到了这个错误。
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 然后应该 post 您在页面上的用户名、ID 和密码。但是,当我按下登录按钮时,出现此错误:
/index.xhtml @15,60 value="#{user.name}": Target Unreachable, identifier 'user' resolved to null
我确实尝试切换到 ManagedBean (@ManagedBean(name="user")
),但结果是一样的。
我找到了解决办法。显然,对于我们中的一些人……,导入的项目名称与 <context-root>
元素的名称不匹配。我们所要做的就是进入 WEB-INF 并找到 glassfish-web.xml
。在该文件中,检查 <context-root>
中的内容,并将其替换为您的项目名称,或者将您的项目重命名为该元素中的任何内容。
花了几个小时才弄明白,但希望其他人会发现这一点并感谢我的回答。