快速业务通道

Struts1.x系列教程(2):简单的数据验证 - 编程入门网

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-06-21
<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>

Struts1.x系列教程(2):简单的数据验证(2)

时间:2011-01-10 BlogJava 银河使者

在启动Tomcat后,在IE的地址栏中输入如下的URL:

http://localhost:8080/samples/simpleValidation.jsp

当通过上面的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文件,并输入如下的代码:

package action;  import javax.servlet.http.*;  import org.apache.struts.action.*;  public class SimpleValidationAction extends Action  {      public ActionForward execute(ActionMapping mapping, ActionForm form,              HttpServletRequest request, HttpServletResponse response)              throws Exception      {                  request.setAttribute("success", "提交成功!");  // 设置在目标页中显示的信息字符串          return mapping.findForward("simple");  // 跳转到目录页(simple所指的JSP页)      }  }

在编写SimpleValidationAction类时应注意如下八点:

1.要想在ActionForm类中进行验证,必须在ActionForm类的子类中覆盖validate方法。

2.validate方法在ActionForm类的对象实例装载完用户提交的数据后调用,因此,在调用validate方法时,ActionForm类的属性值已经是用户提交的信息了。所以可以直接使用这些属性值进行验证。

3.在validate方法中验证用户提交的数据时,要使用ActionErrors类的实例对象返回错误信息

4.ActionErrors类的

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号