Struts下运用validator框架进行表单验证的常用方法

第一步,在struts-config文件里添加valudator插件
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
    <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,  /WEB-INF/validation.xml" />
  </plug-in>

在action配置里添加属性
  validate="true"


第二步,

在validation.xml文件里添加要验证的表单字段的信息

<form-validation>
    <formset>
 <form name="logonForm">
  <field
     property="ipcode"
     depends="required,minlength">
  <arg0 key="LogonForm.ipcode"/>
  <arg1 name="minlength" key="${var:minlength}" resource="false"/>
          <var>
           <var-name>minlength</var-name>
           <var-value>4</var-value>
         </var>
  </field>
  <field
     property="password"
     depends="required,mask">
     <arg0 key="LogonForm.password"/>
     <var>
  <var-name>mask</var-name>
  <var-value>^[0-9a-zA-Z]*$</var-value>
     </var>
  </field>
        </form>
    </formset>
</form-validation>


在资源文件里添加


validation.test.title=struts validation test
errors.required={0} is required.<p>
errors.minlength={0} can not be less than {1} characters.<p>
errors.maxlength={0} can not be greater than {1} characters.<p>
errors.invalid={0} is invalid.

errors.byte={0} must be an byte.
errors.short={0} must be an short.
errors.integer={0} must be an integer.
errors.long={0} must be an long.
errors.float={0} must be an float.
errors.double={0} must be an double.

errors.date={0} is not a date.
errors.range={0} is not in the range {1} through {2}.
errors.creditcard={0} is not a valid credit card number.
errors.email={0} is an invalid e-mail address.<p>

LogonForm.ipcode=<font color=blue>User name</font>
LogonForm.password=<font color=blue>User password</font>

等等你所想要添加的信息

你可能感兴趣的:(Struts下运用validator框架进行表单验证的常用方法)