Struts1.2中的validation验证框架的实现步骤(转帖)

步骤:

1 环境配置:首先要为struts加入Validator支持
      lib包:加入validator所需的jar包
      配置文件:struts-config.xml里加入

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

      这里所需的validator-rules.xml和validation.xml也需要放到/WEB-INF/下

2 配置actionForm
      2.1 修改Form基类:将actionForm继承的基类改为ValidatorForm      
      2.2 配置struts-config.xml文件:把要验证的form的validator属性设为true

<? xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd" >
< struts-config >
  
< form-beans >
    
< form-bean  name ="userForm"  type ="com.satesoft.test.UserForm"   />
  
</ form-beans >
  
< action-mappings >
    
< action  input ="/index.jsp"  name ="userForm"  path ="/show"  scope ="request"  type ="com.satesoft.test.ShowAction"  validate ="true" >
      
< forward  name ="forward"  path ="/show.jsp"   />
    
</ action >
  
</ action-mappings >
  
< message-resources  parameter ="ApplicationResources"   />
  
< plug-in  className ="org.apache.struts.validator.ValidatorPlugIn" >
    
< set-property  property ="pathnames"  value ="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"   />
  
</ plug-in >
</ struts-config >

      注意:一定要配置对应的Action里的input属性,否则struts找不到要返回的路径    

      2.3  配置validator.xml文件:在validation.xml里加入对要验证的actionform的定义

<? xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0//EN" "http://jakarta.apache.org/commons/dtds/validator_1_0.dtd" >
< form-validation >
  
< formset >
    
< form  name ="userForm" >
      
< field  property ="name"  depends ="required" >
        
< arg0  key ="user.name.required" />
      
</ field >
    
</ form >
  
</ formset >
</ form-validation >

      这里同时要配置的还有消息资源。在资源文件中加入上述配置文件中所需的消息

errors.header= < H3 >< font  color ="blue" > errors </ font >< br >
errors.footer=
< p ></ p >
errors.required={0} is required.
errors.minlength={0} cannot be less than {1} characters.
errors.maxlength={0} cannot 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 not a valid credit card number.
errors.email={0} is an invalid email address
user.name.required=user name

 2.4 配置作为input的jsp页面: 

<% @ page contentType="text/html; charset=GBK"  %>
<% @ taglib uri="/WEB-INF/struts-html.tld" prefix="html"  %>
< html >
< head >
< title >
index
</ title >
</ head >
< body  bgcolor ="#ffffff" >
< html:form  action ="show.do"  method ="post" >
  
< html:text  property ="name" />
  
< html:submit  title ="确定" />
  
< html:cancel  title ="取消" >
  
</ html:cancel >
</ html:form >
< html:errors />

</ body >
</ html >

      如果需要加入javascript支持,则:

<% @ page contentType="text/html; charset=GBK"  %>
<% @ taglib uri="/WEB-INF/struts-html.tld" prefix="html"  %>
< html >
< head >
< title >
index
</ title >
</ head >
< body  bgcolor ="#ffffff" >
< html:form  action ="show.do"  method ="post"  onsubmit ="return validateUserForm(this);" >
  
< html:text  property ="name" />
  
< html:submit  title ="确定" />
  
< html:cancel  title ="取消" >
  
</ html:cancel >
</ html:form >
< html:javascript  formName ="userForm" />
</ body >
</ html >

      注意:onsubmit里的validatorUserForm要和下面的formName里的名字一致,只不过首字母大写

 

本文源自:http://blog.myspace.cn/e/401896052.htm

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值