使用struts2表单数据校验功能

上篇文章《使用struts2实现crud操作》中并未涉及数据有效性的验证,struts2也提供了一套验证框架,下面我们就体验一下struts的表单数据校验功能。
      struts2校验框架的使用,并不需要在struts配置文件中配置,有点ror中约定替代配置的味道。还是使用上节的例子,TestAction在struts中的配置文件如下:
< package  name ="struts_crud"  extends ="struts-default"  namespace ="/test" >
        
< action  name ="List"  class ="com.yangqiang.strutsdemo.web.TestAction"  method ="list" >
            
< result > list.jsp </ result >
        
</ action >
        
< action  name ="Edit"  class ="com.yangqiang.strutsdemo.web.TestAction"  method ="load" >
            
< result > edit.jsp </ result >
        
</ action >
        
< action  name ="Store"  class ="com.yangqiang.strutsdemo.web.TestAction"  method ="store" >
               
< result  name ="input"  type ="dispatcher" > edit.jsp </ result >
            
< result  type ="redirect" > List.action </ result >
        
</ action >
        
< action  name ="Remove"  class ="com.yangqiang.strutsdemo.web.TestAction"  method ="remove" >
            
< result  type ="redirect" > List.action </ result >
        
</ action >
    
</ package >

        首先需要注意的是, 数据校验的文件也必须和该Action的存放位置一致,即放在” com.yangqiang.strutsdemo.web“下,xml的命名规则如下 Action类名-validation.xml(例如:TestAction-validation.xml),如果只是对某个action的数据校验,那么命名规则即为: Action类名 -别名-validation.xml (例如:TestAction-Store-validation.xml)即调用Store方法时进行数据检验[注意,这里使用action别名命名数据校验配置文件非常有用,我在系统中就只是使用了 TestAction-Store-validation.xml,在这种情况下我只给Store action配置了”input“ result。如果使用 TestAction-validation.xml,使用上面的struts.xml,打开调用List,Edit,Remove这三个action的页面时,就会报“没有定义input  result”的错误]
         其次,看看
TestAction-Store-validation.xml文件内容:
<? xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE validators PUBLIC 
          "-//OpenSymphony Group//XWork Validator 1.0//EN" 
          "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"
>           
< validators >
    
< field  name  ="test.name"   >
        
< field-validator  type  ="requiredstring" >
            
< message  key  = "test.name" />
        
</ field-validator >
    
</ field  >
        
< field  name ="test.name" >
        
< field-validator  type ="stringlength" >
            
< param  name ="maxLength" > 100 </ param >
            
< param  name ="minLength" > 5 </ param >
            
< param  name ="trim" > true </ param >
            
< message > 必须5-100之内的字符长度 </ message >
        
</ field-validator >
    
</ field >
    
< field  name  ="test.author"   >
        
< field-validator  type  ="requiredstring" >
            
< message >  作者不能为空!  </ message >
        
</ field-validator >
    
</ field  >
</ validators  >  
       struts已经提供了很多校验器,大家可以查查资料看看。
       上面的校验配置文件使用了资源文件,调用使用key。资源文件也使用规则命名,action类名_locale信息.properties(例如:TestAction_zh_CN.properties)
test.name = 书名不能为空!
test.author
= 作者不能为空!
Constant.isRequired
=  不能为空!

使用native2ascii -encoding utf-8 Test_cn.properties Test_zh_CN.properties命令进行编码转换
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值