JSF目标不可达标识符解析为null [英] JSF Target unreachable identifier resolved to null
问题描述
我刚开始使用JSF, 并且我在提交index.xhtml时遇到了这个问题:
I just started using JSF, and i encountered this problem when submitting the index.xhtml:
/index.xhtml @ 11,65 value =#{user.name}":目标无法访问, 标识符用户"解析为空
/index.xhtml @11,65 value="#{user.name}": Target Unreachable, identifier 'User' resolved to null
这是我的3个文件: index.xhtml
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
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
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;
}
}
和faces-config.xml
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>
我使用Netbeans 8和JSF 2.2 我知道还有许多其他标题相同的问题,但没有一个起作用.
Im using Netbeans 8, and JSF 2.2 I know there are many other questions with the same title, but none of them worked.
推荐答案
问题似乎与具有同名的托管bean的双重配置有关:
The problem seems related to the double configuration of your managed bean with the same name:
-
通过在名为
- 注释. 通过称为
- XML配置.
user
的类上使用@ManagedBean
和@SessionScoped
注释来配置User
的<managed-bean>
节点,在faces-config.xml文件中进行- Annotation configuration by using
@ManagedBean
and@SessionScoped
annotations on the class calleduser
. - XML configuration in faces-config.xml file through the
<managed-bean>
node calledUser
.
每个受管Bean应该具有一个配置,即名称.在这种情况下,由于基本上将相同的受管Bean配置了两次,因此只需删除其中一项配置即可.我建议删除XML配置,因为注释配置更易于理解.
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.
这篇关于JSF目标不可达标识符解析为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!