主题:[讨论]请大家帮忙解决下问题,郁闷啊。
我想用JSF写一个简单的HelloWorld的小程序,结果出现了这个问题:
[color=FF0000]javax.servlet.ServletException: Error testing property 'name' in bean of type null
javax.faces.webapp.FacesServlet.service(FacesServlet.java:209)[/color]
下面是我的代码:
Hello类:
package com.guo;
public class HelloBean {
private String name;
private String message;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String sayHello(){
String result = "SayHello";
if(this.name != null){
this.message = "Hello, "+this.name+" !";
}else{
this.message = "Hello, World";
}
return result;
}
}
Helloworld.jsp页面
<%@ page language="java" contentType="text/html; charset=GB2312"
pageEncoding="GB2312"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Hello JSF</title>
</head>
<body>
<f:view><%-- 包围所有JSF标签的标签 --%>
<h:form id="form1" styleClass="form"><%-- HtmlForm组件 --%>
<h:panelGrid columns="2">
<!-- Lable组件 -->
<h:outputLabel value="请输入你的名字" id="label1" styleClass="outputLabel" for="input1">
</h:outputLabel>
<h:inputText id="input1" styleClass="inputText" value="#{helloBean.name}">
</h:inputText>
<h:commandButton type="submit" value="提交" id="button1" styleClass="commandButton" action="#{helloBean.sayHello}"/>
<h:commandButton type="reset" value="重置" id="button2" styleClass="commandButton"/>
</h:panelGrid>
</h:form>
</f:view>
</body>
</html>
Result.jsp页面
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Say Hello</title>
</head>
<body>
<f:view>
<h:outputText id="text1" styleClass="outputText" value="#{helloBean.message}"></h:outputText>
</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_1_2.xsd"
version="1.2">
<managed-bean>
<managed-bean-name>helloBean</managed-bean-name>
<managed-bean-class>com.guo.HelloBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>name</property-name>
<property-class>com.guo.HelloBean</property-class>
<value>#{helloBean.name}</value>
</managed-property>
</managed-bean>
<navigation-rule>
<from-view-id>/HelloWorld.jsp</from-view-id>
<navigation-case>
<from-outcome>SayHello</from-outcome>
<to-view-id>/Result.jsp</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>JSFtest</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>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
</web-app>
请大家帮忙解决一下,很急啊。。。
[color=FF0000]javax.servlet.ServletException: Error testing property 'name' in bean of type null
javax.faces.webapp.FacesServlet.service(FacesServlet.java:209)[/color]
下面是我的代码:
Hello类:
package com.guo;
public class HelloBean {
private String name;
private String message;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String sayHello(){
String result = "SayHello";
if(this.name != null){
this.message = "Hello, "+this.name+" !";
}else{
this.message = "Hello, World";
}
return result;
}
}
Helloworld.jsp页面
<%@ page language="java" contentType="text/html; charset=GB2312"
pageEncoding="GB2312"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Hello JSF</title>
</head>
<body>
<f:view><%-- 包围所有JSF标签的标签 --%>
<h:form id="form1" styleClass="form"><%-- HtmlForm组件 --%>
<h:panelGrid columns="2">
<!-- Lable组件 -->
<h:outputLabel value="请输入你的名字" id="label1" styleClass="outputLabel" for="input1">
</h:outputLabel>
<h:inputText id="input1" styleClass="inputText" value="#{helloBean.name}">
</h:inputText>
<h:commandButton type="submit" value="提交" id="button1" styleClass="commandButton" action="#{helloBean.sayHello}"/>
<h:commandButton type="reset" value="重置" id="button2" styleClass="commandButton"/>
</h:panelGrid>
</h:form>
</f:view>
</body>
</html>
Result.jsp页面
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Say Hello</title>
</head>
<body>
<f:view>
<h:outputText id="text1" styleClass="outputText" value="#{helloBean.message}"></h:outputText>
</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_1_2.xsd"
version="1.2">
<managed-bean>
<managed-bean-name>helloBean</managed-bean-name>
<managed-bean-class>com.guo.HelloBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>name</property-name>
<property-class>com.guo.HelloBean</property-class>
<value>#{helloBean.name}</value>
</managed-property>
</managed-bean>
<navigation-rule>
<from-view-id>/HelloWorld.jsp</from-view-id>
<navigation-case>
<from-outcome>SayHello</from-outcome>
<to-view-id>/Result.jsp</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>JSFtest</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>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
</web-app>
请大家帮忙解决一下,很急啊。。。