关闭

Strust2 输入校验

标签: actionxmlstrutsencodinguserlogin
246人阅读 评论(0) 收藏 举报
分类:

一 文件名约定:

 

1)验证XML的文件名: Action类名-validation.xml , 并与相关的Action类放在一起.

 

2)当一个Action中有多个业务方法时 : Action名-映射名-validation.xml

什么是映射名:<action name="login".../> ,name属性的值:login就是映射名, 千万注意,很多人写错

例子:

URL是:http://localhost/struts/user_addUser.action

struts.xml是:

Xml代码  收藏代码
  1. <action name="user_*" class="ssh.org.web.UserAction" method="{1}">  
  2.     <result name="success">/user/success.jsp</result>  
  3.     <result name="input">/user/user.jsp</result>              
  4. </action>  

 匹配后<action name="user_*".../>  实为<action name="user_addUser" .../>

 

所以验证框架XML文件名是: UserAction-user_addUser-validation.xml

 

 

 

二 验证不通过时,  会执行addFieldError()方法,  在JSP页面, 要用<s:fielderror />显示信息.

关于addFieldError,请看第6讲:6 addActionError与addFieldError

 

 

三 例子

Registration-validation.xml  : 用于校验Registration.java类

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">  
  3.   
  4. <validators>  
  5.   
  6. <!--   
  7.       非字段校验器  
  8.       <validator type="requiredstring">  
  9.         <param name="fieldName">username</param>  
  10.         <message>username should not be blank!</message>  
  11.     </validator>  
  12.       
  13.     <validator type="stringlength">  
  14.         <param name="fieldName">username</param>  
  15.         <param name="minLength">6</param>  
  16.         <param name="maxLength">10</param>  
  17.         <message>username should be between ${minLength} and ${maxLength}</message>  
  18.     </validator>  
  19.  -->   
  20.   
  21.     字段校验器-->  
  22.       <field name="username">  
  23.         <field-validator type="requiredstring">  
  24.             <param name="trim">true</param>  
  25.             <message>username should not be blank!</message>  
  26.         </field-validator>  
  27.           
  28.         <field-validator type="stringlength">  
  29.             <param name="minLength">6</param>  
  30.             <param name="maxLength">10</param>  
  31.             <message>username should be between ${minLength} and ${maxLength}</message>  
  32.         </field-validator>  
  33.     </field>  
  34.   
  35.     <field name="password">  
  36.         <field-validator type="requiredstring">  
  37.             <message>password should not be blank!</message>  
  38.         </field-validator>  
  39.           
  40.         <field-validator type="stringlength">  
  41.             <param name="minLength">6</param>  
  42.             <param name="maxLength">10</param>  
  43.             <message>password should be between ${minLength} and ${maxLength}</message>  
  44.         </field-validator>  
  45.     </field>  
  46.       
  47.     <field name="repassword">  
  48.         <field-validator type="requiredstring">  
  49.             <message>repassword should not be blank!</message>  
  50.         </field-validator>  
  51.           
  52.         <field-validator type="stringlength">  
  53.             <param name="minLength">6</param>  
  54.             <param name="maxLength">10</param>  
  55.             <message>repassword should be between ${minLength} and ${maxLength}</message>  
  56.         </field-validator>  
  57.     </field>  
  58.       
  59.     <field name="age">  
  60.         <field-validator type="required">  
  61.             <message>age should not be blank!</message>  
  62.         </field-validator>  
  63.       
  64.         <field-validator type="int">  
  65.             <param name="min">1</param>  
  66.             <param name="max">150</param>  
  67.             <message>age should be between ${min} and ${max}</message>  
  68.         </field-validator>  
  69.     </field>  
  70.       
  71.     <field name="birthday">  
  72.         <field-validator type="required">  
  73.             <message>birthday should not be blank!</message>  
  74.         </field-validator>  
  75.           
  76.         <field-validator type="date">  
  77.             <param name="min">2001-01-01</param>  
  78.             <param name="max">2003-12-31</param>  
  79.             <message>birthday should be between ${min} and ${max}</message>  
  80.         </field-validator>  
  81.     </field>  
  82.       
  83.     <field name="graduation">  
  84.         <field-validator type="required">  
  85.             <message>graduation should not be blank!</message>  
  86.         </field-validator>  
  87.           
  88.         <field-validator type="date">  
  89.             <param name="min">2005-01-01</param>  
  90.             <param name="max">2007-12-31</param>  
  91.             <message>graduation should be between ${min} and ${max}</message>  
  92.         </field-validator>  
  93.     </field>  
  94.       
  95.   
  96.       
  97. </validators>  



四  Struts2内建的 校验器有:

Xml代码  收藏代码
  1. <validators>  
  2.     <validator name="required" class="com.opensymphony.xwork2.validator.validators.RequiredFieldValidator"/>  
  3.     <validator name="requiredstring" class="com.opensymphony.xwork2.validator.validators.RequiredStringValidator"/>  
  4.     <validator name="int" class="com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator"/>  
  5.     <validator name="long" class="com.opensymphony.xwork2.validator.validators.LongRangeFieldValidator"/>  
  6.     <validator name="short" class="com.opensymphony.xwork2.validator.validators.ShortRangeFieldValidator"/>  
  7.     <validator name="double" class="com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator"/>  
  8.     <validator name="date" class="com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator"/>  
  9.     <validator name="expression" class="com.opensymphony.xwork2.validator.validators.ExpressionValidator"/>  
  10.     <validator name="fieldexpression" class="com.opensymphony.xwork2.validator.validators.FieldExpressionValidator"/>  
  11.     <validator name="email" class="com.opensymphony.xwork2.validator.validators.EmailValidator"/>  
  12.     <validator name="url" class="com.opensymphony.xwork2.validator.validators.URLValidator"/>  
  13.     <validator name="visitor" class="com.opensymphony.xwork2.validator.validators.VisitorFieldValidator"/>  
  14.     <validator name="conversion" class="com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator"/>  
  15.     <validator name="stringlength" class="com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator"/>  
  16.     <validator name="regex" class="com.opensymphony.xwork2.validator.validators.RegexFieldValidator"/>  
  17.     <validator name="conditionalvisitor" class="com.opensymphony.xwork2.validator.validators.ConditionalVisitorFieldValidator"/>  
  18. </validators> 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1016384次
    • 积分:8135
    • 等级:
    • 排名:第2480名
    • 原创:125篇
    • 转载:448篇
    • 译文:3篇
    • 评论:35条
    最新评论