Struts1.x的验证框架

页面控件中name属性值一般是form中的实体元素.实体类中的属性名

 

先介绍下validator-rules.xml文件,在这个文件中是别人给我们提供好啦的一些验证,我们不需要去管它是如何做的验证,在这个文件中我们需要做的是copy,copy什么呢?两个东西

1. 我们要使用验证框架 我们要手动的告诉它我要用它

即 <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
        <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,
                                                  /WEB-INF/validation.xml"/>
      </plug-in> 把这短代码copy到struts-config.xml文件最后

2.把这断代码

   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.

copy到资源文件中,先这样做,稍后在讲有什么好处。这两步完成后 在form中修改(以前我们的form是继承的ActionForm现在继承ValidatorForm) 在修改struts-config.xml文件在对应的action中添加两个属性

input="/index.jsp"  input 的意思是当验证错误时倒向哪个页面

validate="true" 使用验证框架 跟通俗的讲就是调用ValidatorForm类中的validate()方法 这个方法是框架提供的

这几个准备工作完成,我们来编写validation.xml文件啦 这里面是最喜欢出错的

 

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE form-validation PUBLIC
          "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN"
          "http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">

<form-validation>
    <formset>
        <!-- An example form -->

        这个name的意思是你struts-config.xml文件中配置的对应form中name的值

       <form name="userForm">
            <field

                proerty它是页面控件的name属性
                property="user.strName"

                这个required这是个验证方式 即不能为空
                depends="required">

                    这里的key是自己定义的 但好在资源文件中给它赋值
                    <arg key="strName"/>
            </field>
            <field
                property="user.strPwd"

                 这里有那两个验证方式 即 非空 和这个正则表达式 ^[0-9a-zA-Z]*$
                depends="required,mask">
                    <arg key="strPwd"/>
                    <var>
                        <var-name>mask</var-name>
                        <var-value>^[0-9a-zA-Z]*$</var-value>
                    </var>
            </field>
        </form>

    </formset>

</form-validation>

 

我们在来看看这短代码

   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.

 

   errors.required 意思是使用的是那种验证方式 那个key对应的值就是它后面的  即={0} is required

 

  上面的key是strName 赋值

  strName=用户名

 

  那在页面中如何如输出呢?那就要就到struts标签啦

 <html:errors property=" "/> 这里property中写什么呢?页面控件name属性的值

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值