我是 JSF 的新手,并使用 jsf2、tomcat6.0 使用 eclipse kepler 创建了一个示例应用程序。当我单击提交按钮时,我遇到了以下异常:
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>
托管豆 - 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>
你好.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}" />
#{helloWorldBean.firstName} #{helloWorldBean.lastName}
</h3>
<p>
<h:outputText value="#{msg.helloMessage}" />
</p>
</h:body>
</html>