小弟我刚开始学习struts,版本是1.1 我想问一下,表单数据提交后,先在WEB.XML里面找ACTION 的mapping然后去struts-config.xml里面找相应的<action name>找到formbean,如果我的formbean第一次基本的表单验证失败,比如:输入为空,现在把errors返回给request,之后就会通过struts-config.xml中的<action input>属性返回给页面,我想知道以上流程是对的吗?如果是对的,那就是说不需要经过Action罗?不知道我理解的对不对,请高手大哥大姐们指点一下小弟,万分感谢!
struts好象有个专门的验证机制,不用经过action
看看图片吧,我从别的地方贴过来的。你的formbean是只什么地方的formbean?普通的bean?还是ActionForm?如果你在ActionForm里面验证然后就转了的话,你的说法是成立的。但是如果你的bean是在Action里面实例话然后执行execute以后又转的,这样就执行Action了。尽量不要在ActionFrom里面写业务逻辑。
<html:errors/>
<br>
<center>
<html:form action="logon.do" focus="userName">
<table>
<tr>
<td align="right">
<bean:message key="prompt.username"/>
</td>
<td align="right">
<html:text property="userName" size="15" maxlength="15"/>
</td>
</tr>
<tr>
<td align="right">
<bean:message key="prompt.password"/>
</td>
<td align="right">
<html:password property="password" size="15" maxlength="15"/>
</td>
</tr>
<tr>
<td align="center">
<html:submit property="submit">
<bean:message key="button.logon"/>
</html:submit>
</td>
<td align="center">
<html:reset>
<bean:message key="button.reset"/>
</html:reset>
</td>
</tr>
</table>
</html:form>
</center>
FormAction:
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;public final class HelloForm extends ActionForm{
private String userName = null;
public String getUserName(){
return (this.userName);
}
public void setUserName(String userName){
this.userName =userName;
}
public void reset(ActionMapping mapping,HttpServletRequest request){
this.userName = null;
}
public ActionErrors validate(ActionMapping mapping,HttpServletRequest request){
ActionErrors errors = new ActionErrors();
if((userName == null)||(userName.length()<1))
errors.add("userName",new ActionMessage("hello.no.username.error"));
return errors;
}
}WEB.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<display-name>Struts AddressBook Sample Applications</display-name>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<taglib>
<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
<taglib-location>/WEB-INF/tld/struts-logic.tld</taglib-location>
</taglib>
</web-app>struts-config.xml:
<?xml version="1.0" encoding="ISO-8859-1" ?><!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config> <form-beans>
<form-bean name="logonForm" type="forms.LogonForm"/>
<form-bean name="searchForm" type="forms.SearchForm"/>
<form-bean name="insertForm" type="forms.InsertForm"/>
</form-beans> <global-forwards>
<forward name="logoff" path="/logoff.do"/>
<forward name="logon" path="/logon.jsp"/>
<forward name="success" path="/mainMenu.jsp"/>
<forward name="search" path="/search.jsp"/>
<forward name="displayall" path="/displayall.do"/>
<forward name="insert" path="/insert.jsp"/>
<forward name="mainMenu" path="/mainMenu.jsp"/>
<forward name="confirmation" path="/confirmation.jsp"/>
</global-forwards> <action-mappings>
<action path="/search"
type="actions.SearchAction"
name="searchForm"
attribute="myForm"
scope="request"
input="/search.jsp">
<forward name="success" path="/display.jsp"/>
</action>
<action path="/displayall"
type="actions.DisplayAllAction"
name="nestedForm"
scope="request"
input="/mainMenu.jsp">
<forward name="success" path="/display.jsp"/>
</action>
<action path="/insert"
type="actions.InsertAction"
name="insertForm"
scope="request"
input="/insert.jsp"
validate="true">
</action>
<action path="/logoff"
type="actions.LogoffAction">
<forward name="success" path="/index.jsp"/>
</action>
<action path="/logon"
type="actions.LogonAction"
name="logonForm"
scope="request"
validate="true"
input="/logonContent.jsp">
<forward name="success" path="/mainMenu.jsp"/>
</action>
</action-mappings>
<message-resources parameter="ApplicationResources"/></struts-config>按照上面的程序,进入logonContent.jsp后如果不在输入框里面输入,直接点Submit,按理说应该弹出errors提示啊。结果网页报错:
HTTP Status 500 -
type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:372)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1069)
org.apache.struts.action.RequestProcessor.internalModuleRelativeForward(RequestProcessor.java:1012)
org.apache.struts.action.RequestProcessor.processValidate(RequestProcessor.java:980)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:255)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause
java.lang.ClassCastException
org.apache.struts.taglib.html.ErrorsTag.doStartTag(ErrorsTag.java:215)
org.apache.jsp.logonContent_jsp._jspx_meth_html_errors_0(logonContent_jsp.java:119)
org.apache.jsp.logonContent_jsp._jspService(logonContent_jsp.java:90)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1069)
org.apache.struts.action.RequestProcessor.internalModuleRelativeForward(RequestProcessor.java:1012)
org.apache.struts.action.RequestProcessor.processValidate(RequestProcessor.java:980)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:255)
note The full stack trace of the root cause is available in the Apache Tomcat/5.0.30 logs.
Apache Tomcat/5.0.30
public final class HelloForm extends ActionForm