Struts2 框架校验时容易出现的问题

通过继承ActionSupport重写validate()方法,实现数据校验,会存在大量的重复代码,
类似于struts1,Struts2也提供通过配置文件来进行数据校验,使用校验框架不必重复的编写校验代码.
配置文件名称: (Action名称-actionname名- validation.xml) (注意中间是Action名,不是方法名)
 <?xml version="1.0" encoding="UTF-8"?>  
   
 <!DOCTYPE validators PUBLIC   
   
           "-//OpenSymphony Group//XWork Validator 1.0//EN"   
   
           "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">  
   
 <validators>  
   
     <field name="userName">  
   
         <!-- 验证字符串不能为空 -->  
   
         <field-validator type="requiredstring">  
   
             <!-- 去空格 -->  
   
             <param name="trim">true</param>  
   
             <!-- 错误提示信息 -->  
   
             <message>userName is required</message>  
   
         </field-validator>  
   
           
   
         <!-- 验证字符串长度 -->  
   
         <field-validator type="stringlength">  
   
             <param name="minLength">5</param>  
   
             <param name="maxLength">16</param>  
   
             <message>userName should be between ${minLength} and ${maxLength}</message>  
   
         </field-validator>  
   
     </field>  
   
       
   
     <field name="password">  
   
         <field-validator type="requiredstring">  
   
             <param name="trim">true</param>  
   
             <message>password is required</message>  
   
         </field-validator>  
   
           
   
         <field-validator type="stringlength">  
   
             <param name="minLength">5</param>  
   
             <param name="maxLength">16</param>  
   
             <message>password should be between ${minLength} and ${maxLength}</message>  
   
         </field-validator>  
   
     </field>  
   
       
   
     <!-- 验证字符串为数字 -->  
   
     <field name="age">  
   
         <field-validator type="int">  
   
             <param name="min">1</param>  
   
             <param name="max">150</param>  
   
             <message>age should be between ${min} and ${max}</message>  
   
         </field-validator>  
   
     </field>  
   
       
   
     <!-- 验证字符串为日期类型 -->  
   
     <field name="birthday">  
   
         <field-validator type="date">  
   
             <param name="min">1888-01-01</param>  
   
             <param name="max">2008-01-01</param>  
   
             <message>birthday should be between ${min} and ${max}</message>  
   
         </field-validator>  
   
     </field>  
   


2: 如果是无论是否使用的是框架验证如果在Action中对Execute方法自定义如:insertStudent 平且自定义validate方法如:validateInsertStudent , 那么这个俩个验证将都会执行。

你可能感兴趣的:(框架,xml)