我刚开始使用JSF,在提交index.xhtml时遇到了这个问题:
/index.xhtml @ 11,65 value ="#{user.name}":目标无法访问,标识符'User'已解析为null
这是我的3个文件: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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
和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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
我使用Netbeans 8和JSF 2.2我知道有许多其他问题具有相同的标题,但它们都没有奏效.
我想与此例外分享我的经验.我的JSF 2.2应用程序在WildFly 8.0中运行良好,但有一次,当我启动服务器时,我得到了这个"Target Unreacheable"异常.实际上,JSF注释或标签没有问题.
我唯一要做的就是清理项目.完成此操作后,我的应用程序再次正常运行.
我希望这会对某人有所帮助!
该问题似乎与同名托管 bean 的双重配置有关:
@ManagedBean和注释进行注释配置。@SessionScopeduser<managed-bean>名为 的节点进行User。每个托管 bean 应该有一个配置,即名称。在这种情况下,由于您基本上配置了两次相同的托管 bean,因此只需删除其中一项配置即可。我建议删除 XML 配置,因为注释配置更容易阅读和理解。
| 归档时间: |
|
| 查看次数: |
18690 次 |
| 最近记录: |