在页面上提交时出错 - JSF2 + tomcat 6

时间:2013-09-03 23:24:43

标签: jsf submit tomcat6

我是JSF新手并使用jsf2创建了一个示例应用程序,使用eclipse kepler创建了tomcat6.0。当我点击ubmit按钮时,我遇到异常:

SEVERE: Error Rendering View[/index.xhtml] com.sun.faces.mgbean.ManagedBeanCreationException: Unable to create managed bean helloWorldBean.  
The following problems were found:      - Bean or property class com.vigilance.jsf for managed bean helloWorldBean cannot be found.     at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:265)    at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:244)   at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:116)  at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)   at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)    at org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:69)   at org.apache.el.parser.AstValue.getValue(AstValue.java:112)    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)     at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)   at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)   at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)   at javax.faces.component.UIOutput.getValue(UIOutput.java:169)   at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getValue(HtmlBasicInputRenderer.java:205)  at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(HtmlBasicRenderer.java:355)     at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeEnd(HtmlBasicRenderer.java:164)   at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:924)    at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:312) .

我的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"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>JavaServerFaces</display-name>
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>       <!-- Welcome page list -->
    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>      <!-- JavaServer Faces Servlet -->
    <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 to URL pattern -->
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.faces</url-pattern>
    </servlet-mapping>
</web-app>

Managedbean - HelloWorldBean.java:

package com.vigilance.jsf;

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;

@ManagedBean(name="helloWorldBean")
@SessionScoped
public class HelloWorldBean implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private String firstName;

    private String lastName;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

}

xhtml文件 - index.xhtmls

<!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:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core">

    <f:loadBundle basename="resources.application" var="msg" />

    <head>
        <title>
            <h:outputText value="#{msg.welcomeTitle}" />
        </title>
    </head>

    <body>
        <h3>
            <h:outputText value="#{msg.welcomeHeading}" />
        </h3>
        <p>
            <h:outputText value="#{msg.welcomeMessage}" />
        </p>
        <h:form>
            <h:panelGrid columns="2">
                <h:outputLabel for="firstName">
                    First Name *
                </h:outputLabel>
                <h:inputText id="firstName"
                    value="#{helloWorldBean.firstName}">
                </h:inputText>

                <h:outputLabel for="lastName">
                    Last Name *
                </h:outputLabel>
                <h:inputText id="lastName"
                    value="#{helloWorldBean.lastName}">
                </h:inputText>

                <h:commandButton value="Submit" action="hello"></h:commandButton>

            </h:panelGrid>
        </h:form>
    </body>
</html>

hello.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">
    <f:loadBundle basename="resources.application" var="msg" />

    <head>
        <title>
            <h:outputText value="#{msg.welcomeTitle}" />
        </title>
    </head>
    <h:body>
        <h3>
            <h:outputText value="#{msg.helloHeading}" />
            &nbsp;#{helloWorldBean.firstName}&nbsp;#{helloWorldBean.lastName}
        </h3>
        <p>
            <h:outputText value="#{msg.helloMessage}" />
        </p>
    </h:body>
</html>

1 个答案:

答案 0 :(得分:0)

  

发现了以下问题: - 无法找到托管bean com.vigilance.jsf的Bean或属性类helloWorldBean

此错误消息表明您有一个/WEB-INF/faces-config.xml,其中包含以下条目:

<managed-bean>
    <managed-bean-class>com.vigilance.jsf</managed-bean-class>
    <managed-bean-name>helloWorldBean</managed-bean-name>
    <managed-bean-scope>[some scope]</managed-bean-scope>
</managed-bean>

这在两个方面是错误的:

  1. <managed-bean-class>必须代表完全限定的类名,而不是包名。

  2. 如果您已使用<managed-bean>,则无需输入整个@ManagedBean条目。完全摆脱整个条目。