WebWork的Validation

       WebWork依赖XWork的校验框架,在action执行之前,来启动对action的输入数据的校验规则的运用。
1,添加校验器,在/WEB-INF/class添加一个名为validators.xml,添加自定义校验器
xml 代码
  
  1. <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN"
        "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd">
  2. <validators>  
  3.     <validator name="required" class="com.opensymphony.xwork.validator.validators.RequiredFieldValidator"/>  
  4.     <validator name="requiredstring" class="com.opensymphony.xwork.validator.validators.RequiredStringValidator"/>  
  5.     <validator name="int" class="com.opensymphony.xwork.validator.validators.IntRangeFieldValidator"/>  
  6.     <validator name="double" class="com.opensymphony.xwork.validator.validators.DoubleRangeFieldValidator" />  
  7.     <validator name="date" class="com.opensymphony.xwork.validator.validators.DateRangeFieldValidator"/>  
  8.     <validator name="expression" class="com.opensymphony.xwork.validator.validators.ExpressionValidator"/>  
  9.     <validator name="fieldexpression" class="com.opensymphony.xwork.validator.validators.FieldExpressionValidator"/>  
  10.     <validator name="email" class="com.opensymphony.xwork.validator.validators.EmailValidator"/>  
  11.     <validator name="url" class="com.opensymphony.xwork.validator.validators.URLValidator"/>  
  12.     <validator name="visitor" class="com.opensymphony.xwork.validator.validators.VisitorFieldValidator"/>  
  13.     <validator name="conversion" class="com.opensymphony.xwork.validator.validators.ConversionErrorFieldValidator"/>  
  14.     <validator name="stringlength" class="com.opensymphony.xwork.validator.validators.StringLengthFieldValidator"/>  
  15. <!---->validators>  
2,打开校验,在xwork定义interceptor,为需要开发校验的action开启拦截器,拦截器的校验范围
xml 代码
 
  1. <interceptors>  
  2.             <!---->  
  3.             <interceptor name="adminOnly" class="adminInterceptor"/> 
  4.              <interceptor-stack name="defaultStack">  
  5.                 <interceptor-ref name="exception"/>  
  6.                 <interceptor-ref name="alias"/>  
  7.                 <interceptor-ref name="servlet-config"/>  
  8.                 <interceptor-ref name="prepare"/>  
  9.                 <interceptor-ref name="i18n"/>  
  10.                 <interceptor-ref name="chain"/>  
  11.                 <interceptor-ref name="model-driven"/>  
  12.                 <interceptor-ref name="fileUpload"/>  
  13.                 <interceptor-ref name="static-params"/>  
  14.                 <interceptor-ref name="params"/>  
  15.                 <interceptor-ref name="conversionError"/>  
  16.                 <interceptor-ref name="validation">  
  17.                     <param name="excludeMethods">cancel,execute,delete,edit,list,default<!---->param>  
  18.                 <!---->interceptor-ref>  
  19.                 <interceptor-ref name="workflow">  
  20.                     <param name="excludeMethods">input,back,cancel<!---->param>  
  21.                 <!---->interceptor-ref>  
  22.             <!---->interceptor-stack>  
  23.             <!----><interceptor-stack name="validationWorkflowStack">  
  24.                 <interceptor-ref name="defaultStack"/>  
  25.                 <interceptor-ref name="validation"/>  
  26.                 <interceptor-ref name="workflow"/>  
  27.             <!---->interceptor-stack>-->  
  28.             <interceptor-stack name="fileUploadStack">  
  29.                 <interceptor-ref name="fileUpload"/>  
  30.                 <interceptor-ref name="defaultStack"/>  
  31.             <!---->interceptor-stack>  
  32.             <interceptor-stack name="adminCheck">  
  33.                 <interceptor-ref name="defaultStack"/>  
  34.                 <interceptor-ref name="adminOnly"/>  
  35.             <!---->interceptor-stack>  
  36.         <!----><!---->interceptors>  
3,校验规则,每个action类,指定在ActionName-validation.xml文件中,
xml 代码
 
  1. <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
        "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
     
  2. <validators>  
  3.     <field name="customerCreditDetails.customer.id">  
  4.         <field-validator type="required">  
  5.             <message key="errors.required"/>  
  6.         <!----><!---->field-validator>  
  7.         <field-validator type="stringlength">  
  8.             <param name="minLength">1<!---->param>  
  9.             <param name="trim">true<!---->param>  
  10.             <message key="errors.required"/>  
  11.         <!----><!---->field-validator>  
  12.    <!---->field>  
  13.    <field name="customerCreditDetails.amount">  
  14.         <field-validator type="required">  
  15.             <message key="errors.required"/>  
  16.         <!----><!---->field-validator>  
  17.         <field-validator type="double">  
  18.             <param name="minInclusive">1.0<!---->param>  
  19.             <message key="errors.intLeast"/>  
  20.         <!----><!---->field-validator>  
  21.     <!----><!---->field>  
  22.     <field name="customerCreditDetails.operateTime">  
  23.         <field-validator type="required">  
  24.             <message key="errors.required"/>  
  25.         <!----><!---->field-validator>  
  26.         <field-validator type="date">  
  27.             <message key="errors.required"/>  
  28.         <!----><!---->field-validator>  
  29.     <!---->field>  
  30.     <field name="customerCreditDetails.currencyType">  
  31.         <field-validator type="required">  
  32.             <message key="errors.required"/>  
  33.         <!----><!---->field-validator>      
  34.         <field-validator type="int">  
  35.             <param name="min">1<!---->param>  
  36.             <message key="errors.required"/>  
  37.         <!----><!---->field-validator>  
  38.     <!---->field>  
  39.     <field name="customerCreditDetails.payType">  
  40.         <field-validator type="required">  
  41.             <message key="errors.required"/>  
  42.         <!---->field-validator>      
  43.         <field-validator type="int">  
  44.             <param name="min">1<!---->param>  
  45.             <message key="errors.required"/>  
  46.         <!----><!---->field-validator>  
  47.     <!----><!---->field>  
  48. <!---->>  
这是简单验证

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