前言:我已经彻底查看了大约 10 篇不同的相关 stackoverflow 文章,如下面的文章,没有一篇能解决我的问题。所以我展示了完整的代码,看看是否有人知道我的项目可能出了什么问题。请不要将此标记为重复,因为我已经对此类其他文章进行了详尽的研究。
识别和解决 javax.el.PropertyNotFoundException: Target Unreachable
Java EE 6:目标不可达,标识符“helloBean”解析为空
当我尝试运行以下 JSF 应用程序(这是一个非常简单的 Web 应用程序)时,我得到的确切错误是
javax.el.PropertyNotFoundException: /index.xhtml @15,45 value="#{user.name}": Target Unreachable, identifier 'user' resolved to null
at com.sun.jsf-impl@2.3.9.SP02//com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:64) ...
即使在查阅了大量其他 stackoverflow 文章之后,我也不知道如何解决这个问题。我得出以下结论:
1)如果代码有错误,那么我不知道是什么问题,所以请指出,谢谢!
2) 如果代码对您来说看起来不错,您能否在您自己的机器上重新构建它,看看它是否可以正确部署?我有一种感觉,它可能与代码无关,因为我确实尝试了很多不同的变体。
使用的工具:我正在使用 Eclipse、Java 11、JSF 2.2、Servlet 3.1、Wildfly 17
用户Bean.java
package com.corejsf;
import java.io.Serializable;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
@Named("user")
@SessionScoped
public class UserBean implements Serializable {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String newValue) {
name = newValue;
}
public String getPassword() {
return password;
}
public void setPassword(String newValue) {
password = newValue;
}
}
索引.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>
</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>
欢迎.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}!</h3>
</h:body>
</html>
web.xml
<?xml version="1.0"?>
<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">
<display-name>login</display-name>
<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>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
</web-app>
面孔-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">
</faces-config>
但是,Web 项目显示了这一点(请注意,“Beans”文件夹或“类”文件夹下没有文件。不确定这是否应该发生,因为我是 JSF 的新手)。如果这是正常现象,请忽略。我只是想包含尽可能多的信息