又是一个低级错误,唉,过程是痛苦的.
想过无数种可能,试过各种办法,但是最终都是不好使,都是取不到数据,报空指针异常,搞得自己头晕脑涨,昨天辛苦赢来的分在今天便又成了涛涛江水了,也算是福至心灵吧,晚上看
代码时在错误信息中找到了错误的出处,也算是对得起自己了,原因如下:请看
<form-bean name="allForm" type="org.apache.struts.action.DynaActionForm"> 中的form-property属性,其type类型必须是已存在的,无论是已有的系统类型还是自己定义的,
换句话说,即使你写错的那个也算是一个类型,只是找不到罢了,就是因为找不到,所以就显得很奇怪了,就是下面这些东西.
----------------------------------------------------------------------------------------------
严重: Error creating form bean of class org.apache.struts.action.DynaActionForm
java.lang.NullPointerException: The type for property password is invalid
at org.apache.struts.action.DynaActionForm.set(DynaActionForm.java:413)
at org.apache.struts.action.DynaActionFormClass.newInstance(DynaActionFormClass.java:182)
at org.apache.struts.config.FormBeanConfig.createActionForm(FormBeanConfig.java:210)
at org.apache.struts.util.RequestUtils.createActionForm(RequestUtils.java:292)
at org.apache.struts.util.RequestUtils.createActionForm(RequestUtils.java:191)
at org.apache.struts.action.RequestProcessor.processActionForm(RequestProcessor.java:331)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:202)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)
2009-11-23 0:36:59 org.apache.struts.action.RequestProcessor processException
警告: Unhandled Exception thrown: class java.lang.NullPointerException
2009-11-23 0:36:59 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet actionServlet threw exception
java.lang.NullPointerException
at com.kettas.struts.UserMgmDispAction.login(UserMgmDispAction.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:270)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:187)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)
----------------------------------------------------------------------------------------------
这是当时的错误信息,也怪自己不够留心,其实已经在第二行报出错误来了,只是习惯性的看了个开头,而没看到后面的这句话The type for property password is invalid,向死亡
的脑细胞默哀三分钟.
===============================================================================================
以下为使用DispatchAction及DynaActionForm的类及配置文件的写法:代码并不是很严谨,只是走了一个流程.
说一个测试此种类的一个小方法,当无法确定是页面还是后台出错时就在地址栏中输入url,写法如下,我是以此处代码为例的:
http://localhost:8888/struts/user.do?method=login&userName=test&password=test,其实此处的action地址是user.do?method=login,后面的才是提交上来的参数,要特别注意,
一定要分清.
struts-config.xml
*****************************************************************************************
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<form-bean name="allForm" type="org.apache.struts.action.DynaActionForm">
<form-property name="userName" type="java.lang.String"/>
<form-property name="password" type="java.lang.String"/>
</form-bean>
</form-beans>
<action-mappings>
<action path="/user" parameter="method" name="allForm" type="com.kettas.struts.UserMgmDispAction" >
<forward name="ok" path="/ok.jsp"/>
<forward name="error" path="/error.jsp"/>
</action>
</action-mappings>
<message-resources parameter="res.AppResources"/>
</struts-config>
*****************************************************************************************
----------------------------------------------------------------------------------------------
web.xml
*****************************************************************************************
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app 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-app_2_4.xsd"
version="2.4">
<servlet>
<servlet-name>actionServlet</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<load-on-startup>2</load-on-startup>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>actionServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
*****************************************************************************************
----------------------------------------------------------------------------------------------
package com.kettas.struts ;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;
public class UserMgmDispAction extends DispatchAction{
public ActionForward login( ActionMapping mapping ,
ActionForm form ,
HttpServletRequest request ,
HttpServletResponse response )
{
DynaActionForm daf = (DynaActionForm) form ;
String name = (String)daf.get("userName");
String pwd = (String)daf.get("password") ;
System.out.println(name+pwd);
if( true){
ActionMessages messages = new ActionMessages();
ActionMessage m1 = new ActionMessage( "m1" );
messages.add( "a" , m1 ) ;
ActionMessage m2 = new ActionMessage( "m2" ) ;
messages.add( "a" , m2) ;
ActionMessage m3 = new ActionMessage( "m3" ) ;
messages.add( "b" , m3 ) ;
request.setAttribute( "msgs", messages );
return mapping.findForward( "ok" ) ;
}else{
return mapping.findForward( "error" ) ;
}
}
public ActionForward reg( ActionMapping mapping ,
ActionForm form ,
HttpServletRequest request ,
HttpServletResponse response )
{
System.out.println( "======= execute reg ======" ) ;
try{
return mapping.findForward( "ok" ) ;
}catch( Exception e ){
return mapping.findForward( "error" ) ;
}
}
}
----------------------------------------------------------------------------------------------
<%@page contentType="text/html;charset=utf-8" isELIgnored="false"%>
<html>
<head>
</head>
<body>
<form action="<%=request.getContextPath() %>/user.do?method=login" method="post">
<table>
<tr>
<td>
<input type="text" name="userName"/>
<input type="password" name="password"/>
</td>
</tr>
<tr>
<td><b><br></b></td>
<td>
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="button"/>
</td>
</tr>
</table>
</form>
</body>
</html>
----------------------------------------------------------------------------------------------
这个是用struts 的html标签做的,功能都是一样的,一同列出来了
<%@page contentType="text/html;charset=utf-8" isELIgnored="false"%>
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
</head>
<body>
<h1><bean:message key="login.title"/></h1><hr/>
<html:form action="/user.do?method=login" method="post" οnsubmit="return validateAllForm( this );">
<table>
<tr>
<td><b><bean:message key="login.username"/></b></td>
<td>
<html:text property="userName" />
</td>
</tr>
<tr>
<td><b><bean:message key="login.password"/></b></td>
<td>
<html:password property="password" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<html:submit property="btn">
<bean:message key="login.submit"/>
</html:submit>
</td>
</tr>
</table>
</html:form>
<div>
<html:errors />
</div>
</body>
</html>
----------------------------------------------------------------------------------------------
我只将最原始的列出来了,没列其它语言的
AppResources.properties
**********************************************************************************************
login.title=USER LOGIN
login.username=USER NAME:
login.password=PASSWORD:
login.submit=USER LOGIN
errors.name=Must enter user name
errors.password=password can't less than 3 characters
btn.add=+
btn.subtract=-
msg.action=This is message from resoruce file
m1=This is message 1
m2=This is message 2
m3=This is message 3
----------------------------------------------------------------------------------------------