struts validator 的简单介绍(一)

花费时间找,但找到的几个地方的资料讲解均不是很系统,因此在这里大略介绍struts validator的使用.我所使用的是struts 1.3.8
首先要配置好struts-config.xml,如下:
xml 代码
 
  1. xml version="1.0" encoding="ISO-8859-1" ?>    
  2.     
  3.           "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"    
  4.           "http://struts.apache.org/dtds/struts-config_1_3.dtd">    
  5.     
  6. <struts-config>      
  7.     
  8.     
  9.     
  10.     <form-beans>      
  11.     <form-bean name="validationForm"  type="com.ibm.struts.validation.ValidationForm"/>               
  12.     <form-beans>     
  13.     
  14.     
  15.     
  16.     <global-forwards>    
  17.             
  18.             
  19.         <forward    
  20.             name="welcome"    
  21.             path="/Welcome.do"/>    
  22.     global-forwards>    
  23.     
  24.     
  25.     
  26.     
  27.     <action-mappings>    
  28.                 
  29.                 
  30.         <action    
  31.             path="/Welcome"    
  32.             forward="/pages/Welcome.jsp"/>    
  33.         <action path="/TestSimpleValidation"    
  34.                 forward="/pages/test_validation.jsp" />    
  35.         <action path="/SubmitValid"    
  36.                 type="com.ibm.struts.validation.ValidationAction"    
  37.                 name="validationForm"    
  38.                 scope="request"     
  39.                 validate="true"    
  40.                 input="/pages/test_validation.jsp">     
  41.           <forward name="success" path="/pages/success.jsp" redirect="true"/>    
  42.           <forward name="failure" path="/pages/test_validation.jsp"    
  43.                    redirect="true" />    
  44.         action>   
  45.     action-mappings>    
  46.     
  47.     
  48.     
  49.     
  50.     <message-resources parameter="MessageResources" >   
  51.   message-resources>    
  52.   <message-resources key="zh_CN" parameter="MessageResources_zh_CN" />    
  53.     
  54.     
  55.    
  56.       
  57.     
  58.   <plug-in className="org.apache.struts.validator.ValidatorPlugIn">    
  59.     <set-property    
  60.         property="pathnames"    
  61.         value="/org/apache/struts/validator/validator-rules.xml,    
  62.                /WEB-INF/validation.xml"/>    
  63.   plug-in>    
  64.     
  65. struts-config>   
上面59行至64行,增加validator的功能.第12行,设置form,第36行至45行,设置action,由于validator主要的使用不涉及form,action,因此这里不做详细介绍,要提的是form需要继承org.apache.struts.validator.ValidatorForm,

/WEB-INF/validation.xml文件介绍,这个文件主是要针对每个字段设置验证规则,
xml 代码
 
  1. xml version="1.0" encoding="ISO-8859-1" ?>  
  2.      "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.3.0//EN"  
  3.      "http://jakarta.apache.org/commons/dtds/validator_1_3_0.dtd">  
  4.   
  5. <form-validation>  
  6.      
  7.     <formset>          
  8.           
  9.         <form name="validationForm">  
  10.             <field property="username" depends="required">  
  11.                 <arg key="prompt.username"/>  
  12.             field>  
  13.             <field  
  14.                 property="password"  
  15.                 depends="required,mask">  
  16.                     <arg key="prompt.password"/>  
  17.                     <var>  
  18.                         <var-name>maskvar-name>  
  19.                         <var-value>^t[0-9]n$var-value>  
  20.                     var>  
  21.             field>  
  22.         form>  
  23.   
  24.     formset>  
  25.   
  26. form-validation>  
容易理解的不做介绍,第11行depends=required的意思当然是该字段是必需的,不能为空,所在资源文件里的相应的错误信息是这样的"errors.required={0} is required.",接下来的第12行,就是定义{0}参数是用什么替代,用的是也是资源文件里的"prompt.username=\u5e10\u53f7"

资源文件
xml 代码
 
  1. # -- standard errors --  
  2. errors.header=validation error message  
  3. errors.prefix=<LI>  
  4. errors.suffix=LI>  
  5. errors.footer=UL>  
  6. # -- validator --  
  7. errors.invalid={0} is invalid.  
  8. errors.maxlength={0} can not be greater than {1} characters.  
  9. errors.minlength={0} can not be less than {1} characters.  
  10. errors.range={0} is not in the range {1} through {2}.  
  11. errors.required={0} is required.  
  12. errors.byte={0} must be an byte.  
  13. errors.date={0} is not a date.  
  14. errors.double={0} must be an double.  
  15. errors.float={0} must be an float.  
  16. errors.integer={0} must be an integer.  
  17. errors.long={0} must be an long.  
  18. errors.short={0} must be an short.  
  19. errors.creditcard={0} is not a valid credit card number.  
  20. errors.email={0} is an invalid e-mail address.  
  21. # -- other --  
  22. errors.cancel=Operation cancelled.  
  23. errors.detail={0}  
  24. errors.general=The process did not complete. Details should follow.  
  25. errors.token=Request could not be completed. Operation is not in sequence.  
  26. # -- welcome --  
  27. welcome.title=Struts Blank Application  
  28. welcome.heading=Welcome!  
  29. welcome.message=To get started on your own application, copy the struts-blank.war to a new WAR file using the name for your application. Place it in your container's "webapp" folder (or equivalent), and let your container auto-deploy the application. Edit the skeleton configuration files as needed, restart your container, and you are on your way! (You can find the MessageResources.properties file with this message in the /WEB-INF/src folder.)  
  30. valid.title=\u7b80\u5355\u9a8c\u8bc1  
  31. prompt.username=\u5e10\u53f7  
  32. prompt.password=\u5bc6\u7801  
  33. prompt.phone=\u7535\u8bdd\u53f7\u7801  
  34. prompt.email=E-Mail  
  35. prompt.url=URL  


最后,要在jsp里引用错误提示信息:
xml 代码
 
  1. <logic:messagesPresent>  
  2.    <bean:message key="errors.header"/>  
  3.    <ul>  
  4.    <html:messages id="error">  
  5.       <li><bean:write name="error"/></li>  
  6.    </html:messages>  
  7.    </ul><hr />  
  8. </logic:messagesPresent>  


将这一段代码放在body后面即可.

参考资源:
http://struts.apache.org/1.3.8/faqs/validator.html
http://struts.apache.org/1.3.8/userGuide/building_view.html#validator
struts-examples-1.3.8示例里有validator的例子
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值