Chinaunix首页 | 论坛 | 博客
  • 博客访问: 409782
  • 博文数量: 114
  • 博客积分: 7010
  • 博客等级: 少将
  • 技术积分: 1395
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-05 17:54
文章分类

全部博文(114)

文章存档

2011年(2)

2009年(1)

2008年(111)

我的朋友

分类: Java

2008-09-02 23:26:48

 校验框架:双校验
   使用校验框架:
   1.在struts-config.xml中加入validate plug-in(copy)
 

<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
        <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,
                                                  /WEB-INF/validation.xml"
/>
      </plug-in>

 2.在WEB-INF跟目录下,建立validation.xml,并描述校验规则

 

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0//EN" "">

<form-validation>
    <formset>
        <form name="loginForm">
            <field property="userName" depends="required,minlength,maxlength,integer">
            <arg0 key="userName"/>
            <arg1 key="${var:minlength}" name="minlength" resource="false"/>
            <var>
                <var-name>minlength</var-name>
                <var-value>3</var-value>
            </var>
            <arg1 key="${var:maxlength}" name="maxlength" resource="false"/>
            <var>
                <var-name>maxlength</var-name>
                <var-value>8</var-value>
            </var>
            </field>
            <field property="passWord" depends="required,minlength,maxlength">
            <arg0 key="passWord"/>
            <arg1 key="${var:minlength}" name="minlength" resource="false"/>
            <var>
                <var-name>minlength</var-name>
                <var-value>3</var-value>
            </var>
            <arg1 key="${var:maxlength}" name="maxlength" resource="false"/>
            <var>
                <var-name>maxlength</var-name>
                <var-value>8</var-value>
            </var>
            </field>
        </form>
    </formset>
</form-validation>

 

  3.copy error message to resource

 

userName=userName

passWord=passWord

errors.footer=</span>

errors.header=<span style="color:red;">

errors.required={0} is required.

errors.minlength={0} can not be less than {1} characters. errors.maxlength={0} can not be greater than {1} characters. errors.invalid={0} is invalid.

errors.byte={0} must be a byte.

errors.short={0} must be a short.

errors.integer={0} must be an integer.

errors.long={0} must be a long.

errors.float={0} must be a float.

errors.double={0} must be a double.

errors.date={0} is not a date.

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

4.form类必须继承ValidatorForm 并且要干掉其中的validate方法!

  OK 完成!

阅读(1491) | 评论(0) | 转发(0) |
0

上一篇:Action校验

下一篇: Struts开发工作流程

给主人留下些什么吧!~~