管理Bean注释无法访问JSF 2.0目标

时间:2013-05-30 20:03:15

标签: jsf-2 annotations

我正在尝试使用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)

下面是代码,其中包括Managed 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;
    }

}

index.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>

faces-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>

1 个答案:

答案 0 :(得分:1)

我已经使用提供的配置创建并测试了一个项目,无法重现您的错误。我怀疑你没有重建/重新部署你的项目。

此外,您应该在JSF 2中使用h:head和h:body。如果省略h:head,PrimeFaces将无法为AJAX函数添加所需的脚本,因此您的项目无法正常工作。所以你应该改变你的 index.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">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Home Page</title>
</h:head>
<h: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>
</h:body>
</html>