Struts2输入校验(一)

1.校验规则和对应校验器类的声明文件

    xwork-2.1.2.jar\com.opensymphony.xwork2.validator.validators.default.xml 的部分内容:

<validators>
    <validator name="required" class="com.opensymphony.xwork2.validator.validators.RequiredFieldValidator"/>
</validators>

2.校验规则文件名

  Action类名-Validation.xml Action类名-Action别名-Validation.xml 

    (Action别名是struts.xml中action标签的name属性值)  

   2.1.6最少需7个jar:

    struts2-core-2.1.6.jar;  

    xwork-2.1.2.jar;    

    ognl-2.6.11.jar;    

    commons-fileupload-1.2.1.jar

    commons-io-1.3.2.jar

    commons-logging-1.1.jar

    freemarker-2.3.13.jar

   2.1.8最少需6个jar:

    struts2-core-2.1.8.jar;  

    xwork-2.1.6.jar;    

    ognl-2.7.3.jar;    

    commons-fileupload-1.2.1.jar

    commons-logging-1.0.4.jar

    freemarker-2.3.15.jar

  在上面两种配置下均没有 Action类名-Action方法名-Validation.xml 这种用法 准确说应该是这样命名的话验证框架不起作用

   com.opensymphony.xwork2.validator.Validator接口的部分注释:

To define validation rules for an Action, create a file named ActionName-validation.xml in the same package as the Action. You may also create alias-specific validation rules which add to the default validation rules defined in ActionName-validation.xml by creating another file in the same directory named ActionName-aliasName-validation.xml. In both cases, ActionName is the name of the Action class, and aliasName is the name of the Action alias defined in the xwork.xml configuration for the Action.

   在2.1.6下可能会出很多警告  无关痛痒,但非常多

2012-3-14 23:02:40 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: Could not find property [struts.actionMapping]
2012-3-14 23:02:40 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: Could not find property [org.apache.catalina.jsp_file]
2012-3-14 23:03:27 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: Could not find property [struts.actionMapping]
2012-3-14 23:03:27 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: Could not find property [struts.valueStack]
2012-3-14 23:03:28 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: Could not find property [org.apache.catalina.jsp_file]
2012-3-14 23:03:28 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: Could not find property [templateDir]
2012-3-14 23:03:28 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: Could not find property [theme]
2012-3-14 23:03:28 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: Could not find property [.freemarker.Request]
2012-3-14 23:03:28 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: Could not find property [.freemarker.RequestParameters]

3.查找顺序 从上到下

The framework will also search up the inheritance tree of the Action to find validation rules for directly implemented interfaces and parent classes of the Action. This is particularly powerful when combined with ModelDriven Actions and the VisitorFieldValidator. Here's an example of how validation rules are discovered. Given the following class structure:


 

  The framework method will look for the following config files if Dog is to be validated:

  • Animal
  • Animal-aliasname
  • AnimalImpl
  • AnimalImpl-aliasname
  • Quadraped
  • Quadraped-aliasname
  • QuadrapedImpl
  • QuadrapedImpl-aliasname
  • Dog
  • Dog-aliasname
4. xwork-2.1.2.jar\com.opensymphony.xwork2.validator.validators.default.xml 中的16个校验规则对应校验类全部直接或间接继承

     com.opensymphony.xwork2.validator.validators.ValidatorSupport 抽象类






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值