本文为原创,如需转载,请注明作者和出处,谢谢! 《Struts 2系列教程》 上一篇:Struts1.x系列教程(1):用MyEclipse开发第一个Struts程序
简单验证从本质上说就是在服务端来验证客户端提交的form中的数据。这种验证只是对form中的数据规则进行检查,如必须输入用户ID,价格不能小于0或是对email格式的验证。在这个验证过程中,并不需要访问数据库。因此,简单验证需要在用户提交form后,并且在服务器处理form中的数据之前进行。
在进行完简单验证后,如果form中的数据不合法,程序就会forward到指定的JSP页(一般是包含form的页面),并显示相应的错误信息。如果form中的数据完全正确,程序就会继续执行。
一、在validate方法中进行简单验证
在上一篇文章中我们知道,Struts1.x通过ActionForm的子类来封装了客户端提交的form中的数据。而服务端程序只需要通过ActionForm的子类的对象实例就可以访问form中的数据,而如果不使用ActionForm类,就必须通过request对象来获得form中的数据。通过这种封装机制可以使代码更容易理解。然而,ActionForm类不仅可以封装form中的数据,还可以通过ActionForm类的validate方法来验证form中的数据。validate方法的定义如下:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> public ActionErrorsvalidate(ActionMappingmapping,HttpServletRequestrequest)<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="ProgId" content="Word.Document"><meta name="Generator" content="Microsoft Word 11"><meta name="Originator" content="Microsoft Word 11"><link rel="File-List" href="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\02\clip_filelist.xml"><!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><style> <!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} --> </style><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]-->
当客户端向服务端提交form后,Servlet引擎首先通过ActionForm的子类的对象实例装载form中的数据,然后再调用validate方法进行验证。validate方法返回了一个ActionErrors对象。这个对象相当于一个Map,如果ActionErrors中没有错误信息,Servlet引擎就认为form中的数据是正确的,这时服务端程序就会继续执行。如果ActionErrors中有错误信息,程序就会跳转到指定的错误页面。下面让我们通过一个完整的例子来演示一下如何通过validate方法来验证form中的数据。实现这个例子需要如下五步:【第1步】建立JSP页面
在这一步将建立一个叫simpleValidation.jsp的页面,这个JSP页面用于采集用户的输入信息。在<samples工程目录>中建立一个simpleValidation.jsp文件,并编写如下的代码:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> <% @pagepageEncoding = " GBK " %> <% @tagliburi = " http://struts.apache.org/tags-html " prefix = " html " %> < html > < head > < title > 注册信息(测试简单验证) </ title > < style type ="text/css" > .text { height : 20px ; width : 160px ; } </ style > </ head > < body > < html:form action ="simpleValidation" > < table width ="100%" > < tr > < td align ="right" width ="45%" > 用户名: </ td > < td width ="55%" > < html:text property ="user" styleClass ="text" /> < font color ="red" >< html:errors property ="errorUser" /></ font > </ td > </ tr >< tr />< tr /> < tr > < td align ="right" > 登录密码: </ td > < td > < html:password property ="password" styleClass ="text" /> < font color ="red" >< html:errors property ="errorPassword" /></ font > </ td > </ tr >< tr />< tr /> < tr > < td align ="right" > 重复登录密码: </ td > < td > < html:password property ="password1" styleClass ="text" /> < font color ="red" >< html:errors property ="errorPassword1" /></ font > </ td > </ tr >< tr />< tr /> < tr > < td align ="right" > 电子邮件: </ td > < td > < html:text property ="email" styleClass ="text" /> < font color ="red" >< html:errors property ="errorEmail" /></ font > </ td > </ tr >< tr />< tr /> < tr > < td align ="right" > < br > ${requestScope.success} </ td > < td align ="left" > < br > < html:submit value ="提交" /> </ td > </ tr > </ table > </ html:form > </ body > </ html ><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="ProgId" content="Word.Document"><meta name="Generator" content="Microsoft Word 11"><meta name="Originator" content="Microsoft Word 11"><link rel="File-List" href="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\02\clip_filelist.xml"><!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><style> <!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} --> </style><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]-->在启动Tomcat后,在IE的地址栏中输入如下的URL:http://localhost:8080/samples/simpleValidation.jsp <meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="ProgId" content="Word.Document"><meta name="Generator" content="Microsoft Word 11"><meta name="Originator" content="Microsoft Word 11"><link rel="File-List" href="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\02\clip_filelist.xml"><!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><style> <!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} /* List Definitions */ @list l0 {} @list l0:level1 { margin-left:39.75pt; text-indent:-18.0pt;} ol {margin-bottom:0cm;} ul {margin-bottom:0cm;} --> </style><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]-->
当通过上面的URL访问simpleValidation.jsp时,并不能正确显示用户信息采集界面。原因是<html:form>标签使用了一个simpleValidation,当JSP转换成Servlet时,这个动作必须在struts-config.xml文件中正确定义,否则将抛出一个javax.servlet.jsp.JspException异常。【第2步】建立simpleValidation动作
由于本例的着重点是简单验证,因此,simpleValidation动作并不需要处理更多的工作。一个动作对应于一个动作类,这个动作类一般是org.apache.struts.action.Action类的子类。simpleValidation动作只做如下两项工作:
1. 设置验证成功后,在目标页中显示的信息字符串(保存在request的属性中)。
2. 跳转到目标页。
simpleValidation动作对应的动作类是SimpleValidationAction,在<samples工程目录>"src"action目录中建立一个SimpleValidationAction.java文件,并输入如下的代码:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> package action; import javax.servlet.http. * ; import org.apache.struts.action. * ; public class SimpleValidationAction extends Action { public ActionForwardexecute(ActionMappingmapping,ActionFormform, HttpServletRequestrequest,HttpServletResponseresponse) throws Exception { request.setAttribute( " success " , " 提交成功! " ); // 设置在目标页中显示的信息字符串 return mapping.findForward( " simple " ); // 跳转到目录页(simple所指的JSP页) } }<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="ProgId" content="Word.Document"><meta name="Generator" content="Microsoft Word 11"><meta name="Originator" content="Microsoft Word 11"><link rel="File-List" href="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\02\clip_filelist.xml"><!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><style> <!--{cps..21}--</style>【第3步】建立ActionForm类 在这一步我们来建立一个用于接收有户的提交信息的ActionForm类:SimpleValidationForm。这个类从org.apache.struts.action.ActionForm类继承。在<samples工程目录>"src"actionform目录中建立一个SimpleValidationForm.java文件,代码如下:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> package actionform; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action. * ; public class SimpleValidationForm extends ActionForm { // 以下四个变量分别对应于simpleValidation.jsp中的四个文本框中的值。 private Stringuser; private Stringpassword; private Stringpassword1; private Stringemail; public StringgetUser() { return user; } public void setUser(Stringuser) { this .user = user; } public StringgetPassword() { return password; } public void setPassword(Stringpassword) { this .password = password; } public StringgetPassword1() { return password1; } public void setPassword1(Stringpassword1) { this .password1 = password1; } public StringgetEmail() { return email; } public void setEmail(Stringemail) { this .email = email; } // 开始验证用户提交的信息 public ActionErrorsvalidate(ActionMappingmapping,HttpServletRequestrequest) { ActionErrorserror = new ActionErrors(); if (user.equals( "" )) // 必须输入用户名 error.add( " errorUser " , new ActionMessage( " error.user.blank " )); if (password.equals( "" )) // 必须输入密码 error.add( " errorPassword " , new ActionMessage( " error.password.blank " )); else if ( ! password.equals(password1)) // 两个登录密码必须一致 error.add( " errorPassword1 " , new ActionMessage( " error.password1.confirmation " )); if (email.equals( "" )) // 必须输入email error.add( " errorEmail " , new ActionMessage( " error.email.blank " )); else if ( ! email.matches( " \\w+(\\.\\w+)*@\\w+(\\.\\w+)+ " )) // 验证email的格式是否正确 error.add( " errorEmail " , new ActionMessage( " error.email.invalid " )); // 返回错误信息,如果error中没有错误信息, // 就会调用SimpleValidationAction类的对象实例来执行execute方法。 return error; } }<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="ProgId" content="Word.Document"><meta name="Generator" content="Microsoft Word 11"><meta name="Originator" content="Microsoft Word 11"><link rel="File-List" href="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\02\clip_filelist.xml"><!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><style> <!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} /* List Definitions */ @list l0 {} @list l0:level1 { margin-left:39.75pt; text-indent:-18.0pt;} ol {margin-bottom:0cm;} ul {margin-bottom:0cm;} --> </style><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]-->
在编写SimpleValidationAction类时应注意如下八点: 1.要想在ActionForm类中进行验证,必须在ActionForm类的子类中覆盖validate方法。 2.validate方法在ActionForm类的对象实例装载完用户提交的数据后调用,因此,在调用validate方法时,ActionForm类的属性值已经是用户提交的信息了。所以可以直接使用这些属性值进行验证。 3.在validate方法中验证用户提交的数据时,要使用ActionErrors类的实例对象返回错误信息4.ActionErrors类的构造方法的第二个参是一个ActionMessage类的对象实例,而不是错误描述信息。 5.ActionMessage类的构造方法的参数并不是错误描述信息,而是错误描述信息的key,具体的信息在Java属性文件中(将在下一步实现)。 6.使用ActionForm的属性可以非常好地验证字符串类型,但对于其他的数据类型(如整型)的某些验证却不太适合。如当用户提交数据时,本该提交一个整数,但用户却提交了一个非整数信息。对于这种情况,在ActionForm类的对象实例中这个用户提交的数据的值为0。虽然使用ActionForm类的属性无法准确验证这种情况,但我们可以使用validate方法的第二个参数request的getParameter方法直接获得客户端提交的数据来进行验证。 7.如果ActionErrors对象中有错误信息,在JSP中需要使用<html:errors>标签显示错误信息。 8. Struts实际上是将ActionErrors对象以org.apache.struts.action.ERROR作为键值保存在了request的属性中。因此,<html:errors>标签实际上是从request的属性中获得的错误信息描述。如我们也可以通过如下的Java代码来获得produceID属性的错误描述信息:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> < % java .util.Iterator<org.apache.struts.action.ActionMessage > it= ((org.apache.struts.action.ActionErrors)request .getAttribute("org.apache.struts.action.ERROR")).get("productID"); out.println(((org.apache.struts.util.PropertyMessageResources)request .getAttribute("org.apache.struts.action.MESSAGE")).getMessage("error.productID.blank",null)); %><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="ProgId" content="Word.Document"><meta name="Generator" content="Microsoft Word 11"><meta name="Originator" content="Microsoft Word 11"><link rel="File-List" href="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\02\clip_filelist.xml"><!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><style> <!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} --> </style><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]-->
【第4步】建立Java属性文件
Java属性文件相当于资源文件,以key = value形式保存了在程序中需要的字符串信息。Java属性文件的扩展名为properties。在<samples工程目录>"src目录中建立一个struts目录,在struts目录中建立一个ErrorDescription.properties文件,并输入如下的内容:ErrorDescription.properties
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> error.user.blank = Usercan'tbenull. error.password.blank = Passwordcan'tbenull. error.password1.confirmation = Passworddoesn'tmatchconfirmation. error.email.blank = Emailcan'tbenull. error.email.invalid = Itisnotavalidemailaddress.<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="ProgId" content="Word.Document"><meta name="Generator" content="Microsoft Word 11"><meta name="Originator" content="Microsoft Word 11"><link rel="File-List" href="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\02\clip_filelist.xml"><!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><style> <!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} --> </style><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]-->
【第5步】配置struts-config.xml文件 在本例中需要配置struts-config.xml文件的三个标签:<form-bean>、<action>和<message-resources>。 1.配置<form-bean>标签
这个标签用来定义ActionForm。在<form-beans>标签中加入如下所示的<form-bean>标签:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> < form-bean name ="simpleValidationForm" type ="actionform.SimpleValidationForm" /><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="ProgId" content="Word.Document"><meta name="Generator" content="Microsoft Word 11"><meta name="Originator" content="Microsoft Word 11"><!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><style> <!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} --> </style><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]-->2.配置<action>标签 这个标签用来定义Struts中的动作类。在<action-mappings>标签中加入如下所示的<action>标签:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> < action name ="simpleValidationForm" path ="/simpleValidation" scope ="request" type ="action.SimpleValidationAction" input ="simpleValidation.jsp" > < forward name ="simple" path ="simpleValidation.jsp" /> </ action > <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="ProgId" content="Word.Document"> <meta name="Generator" content="Microsoft Word 11"> <meta name="Originator" content="Microsoft Word 11"> <link rel="File-List" href="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\02\clip_filelist.xml"> <!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><style> <!-- /* Font Definitions */ @font-face {font-family:Wingdings; panose-1:5 0 0 0 0 0 0 0 0 0;} @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} /* List Definitions */ @list l0 {} @list l0:level1 { margin-left:41.65pt; text-indent:-21.0pt; font-family:Wingdings;} @list l1 {} @list l1:level1 { margin-left:42.0pt; text-indent:-21.0pt; font-family:Wingdings;} @list l2 {} @list l2:level1 { margin-left:39.75pt; text-indent:-18.0pt;} ol {margin-bottom:0cm;} ul {margin-bottom:0cm;} --> </style> <!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]--><action>标签中的属性含义描述如下:
1. name:表示ActionForm的名称。也就是<form-bean>标签中的name属性的值。
2. path:表示Struts动作,必须以“/”开头。
3. scope:表示ActionForm类的对象实例(在本例中是SimpleValidationForm类的对
象实例)保存的范围。这个属性值只能取request和session。默认值是session。如果scope的值为request,表示将SimpleValidationForm类的对象实例以simpleValidationForm作为键值保存到了request的属性中。如果scope的值为session,表示不将SimpleValidationForm类的对象实例保存到request的属性中。但不管scope的值是request还是session。Struts都会将SimpleValidationForm类的对象实例保存到session的属性中。
4. type:表示SimpleValidationAction类的全名。
5. input:表示如果客户端提交的数据未通过简单验证后要跳转到的页面,也就是在
SimpleValidationForm类的validate方法中返回的ActionErrors对象中含有错误描述信息。Struts会自动跳转到input属性所指的JSP页面。
<action>标签中的子标签<forward>定义了可以在动作类(在本例中就是SimpleValidationAction类)中读取的forward页的URL。
2. 配置<message-resources>标签
这个标签用来定义程序中所使用的属性文件。在struts-config.xml文件的<struts-config>根节点中加入如下内容:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> < message-resources parameter ="struts.ErrorDescription" /><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="ProgId" content="Word.Document"><meta name="Generator" content="Microsoft Word 11"><meta name="Originator" content="Microsoft Word 11"><link rel="File-List" href="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\02\clip_filelist.xml"><!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><style> <!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} --> </style><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]-->
其中parameter属性表示属性文件的路径,在本例中属性文件ErrorDescription.properties位于struts目录中,因此,parameter属性的值为struts.ErrorDescription。扩展名properties不用写。其中“.”也可以使用“/”或“"”代替。下面我们测试一下这个例子程序。首先启动Tomcat,然后在IE中输入如下的URL:http://localhost:8080/samples/simpleValidation.jsp <meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="ProgId" content="Word.Document"><meta name="Generator" content="Microsoft Word 11"><meta name="Originator" content="Microsoft Word 11"><link rel="File-List" href="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\02\clip_filelist.xml"><!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><style> <!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ @page {} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} --> </style><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]-->
访问上面的URL后的界面如图1所示。
图1 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name=" 相关资源:敏捷开发V1.0.pptx