我正在尝试在 Apache Tomcat 7.0 上使用 prime faces 3.5 运行一个简单的 JSF 2.0 项目。我使用了 Managed Bean 的注解。
我在提交 index.xhtml 时收到此错误
javax.el.PropertyNotFoundException: /index.xhtml @16,60 value="#{homeBean.firstname}": Target Unreachable, identifier 'homeBean' resolved to null
com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:97)
org.primefaces.renderkit.InputRenderer.findImplicitConverter(InputRenderer.java:170)
org.primefaces.renderkit.InputRenderer.findConverter(InputRenderer.java:190)
org.primefaces.renderkit.InputRenderer.getConvertedValue(InputRenderer.java:196)
javax.faces.component.UIInput.getConvertedValue(UIInput.java:1023)
javax.faces.component.UIInput.validate(UIInput.java:953)
javax.faces.component.UIInput.executeValidate(UIInput.java:1204)
javax.faces.component.UIInput.processValidators(UIInput.java:693)
javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1081)
javax.faces.component.UIForm.processValidators(UIForm.java:240)
javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1081)
javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1159)
com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:72)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:308)
下面是包含托管 Bean、欢迎页面、部署描述符、faces-config 的代码:
package main.com.java;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
public class HomeBean implements Serializable
{
private static final long serialVersionUID = 1L;
private String firstname;
public String getFirstname()
{
return firstname;
}
public void setFirstname(String firstname)
{
this.firstname = firstname;
}
}
索引.xhtml:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Home Page</title>
</head>
<body>
<f:view>
<h:form id="form">
<h:panelGrid columns="4" cellpadding="5">
<h:outputLabel for="name" value="Name:" style="font-weight:bold" />
<p:inputText id="name" value="#{homeBean.firstname}" />
<p:commandButton value="Submit" update="display" />
<h:outputText value="#{homeBean.firstname}" id="display" />
</h:panelGrid>
</h:form>
</f:view>
</body>
</html>
面孔-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
</faces-config>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>JSFExample</display-name>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
<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>
</web-app>