Struts学习---------Validator框架验证

struts-config.xml

View Code
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">

<struts-config>
  <form-beans >
    <form-bean name="userLoginForm" type="org.xiong.validator.struts.demo.form.UserLoginForm" />

  </form-beans>

  <global-exceptions />
  <global-forwards />
  <action-mappings >
    <action
      attribute="userLoginForm"
      input="/UserLogin/Login.jsp"
      name="userLoginForm"
      path="/userLogin"
      scope="request"
      type="org.xiong.validator.struts.demo.action.UserLoginAction"
      cancellable="true" validate="true">
          <forward name="sucess" path="/UserLogin/Sucess.jsp"></forward>
      </action>
  </action-mappings>

  <message-resources parameter="org.xiong.validator.struts.demo.ApplicationResources" />
  <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
      <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
       <set-property property="stopOnFirstError" value="true"/>
  </plug-in>
</struts-config>

validation.xml

View Code
  1 <!DOCTYPE form-validation PUBLIC
  2           "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN"
  3           "http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">
  4 <!--
  5   $Id: validator-rules.xml,v 1.1.2.1 2006/07/01 20:50:46 eugene-proddev Exp $
  6 
  7    This file contains the default Struts Validator pluggable validator
  8    definitions.  It should be placed somewhere under /WEB-INF and
  9    referenced in the struts-config.xml under the plug-in element
 10    for the ValidatorPlugIn.
 11 
 12       <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
 13         <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,
 14                                                   /WEB-INF/validation.xml"/>
 15       </plug-in>
 16 
 17    These are the default error messages associated with
 18    each validator defined in this file.  They should be
 19    added to your projects ApplicationResources.properties
 20    file or you can associate new ones by modifying the
 21    pluggable validators msg attributes in this file.
 22 
 23    # Struts Validator Error Messages
 24    errors.required={0} is required.
 25    errors.minlength={0} can not be less than {1} characters.
 26    errors.maxlength={0} can not be greater than {1} characters.
 27    errors.invalid={0} is invalid.
 28 
 29    errors.byte={0} must be a byte.
 30    errors.short={0} must be a short.
 31    errors.integer={0} must be an integer.
 32    errors.long={0} must be a long.
 33    errors.float={0} must be a float.
 34    errors.double={0} must be a double.
 35 
 36    errors.date={0} is not a date.
 37    errors.range={0} is not in the range {1} through {2}.
 38    errors.creditcard={0} is an invalid credit card number.
 39    errors.email={0} is an invalid e-mail address.
 40 
 41    Note: Starting in Struts 1.2.0 the default javascript definitions have
 42          been consolidated to commons-validator. The default can be overridden
 43          by supplying a <javascript> element with a CDATA section, just as
 44          in struts 1.1.
 45 
 46 -->
 47 
 48 <form-validation>
 49     <formset>
 50         <form name="userLoginForm">
 51             <field property="user.username" depends="required">
 52                 <arg key="prompt.username"/>            
 53             </field>
 54             <field property="user.password" depends="required,minlength,maxlength">
 55                 <arg key="prompt.password" position="0"/>
 56                 <arg key="${var:minlength}" name="minlength" resource="false" position="1"/>
 57                 <arg key="${var:maxlength}" name="maxlength" resource="false" position="1"/>            
 58                 <var>
 59                     <var-name>
 60                         maxlength
 61                     </var-name>
 62                     <var-value>
 63                         16
 64                     </var-value>
 65                 </var>
 66                 <var>
 67                     <var-name>
 68                         minlength
 69                     </var-name>
 70                     <var-value>
 71                         3
 72                     </var-value>
 73                 </var>
 74             </field>
 75             <field property="password2" depends="required,validwhen">
 76                 <arg key="prompt.password"/>
 77                 <msg name="validwhen" key="errors.password2"/>
 78                 <var>
 79                     <var-name>test</var-name>
 80                     <var-value>(*this* == user.password)</var-value>
 81                 </var>        
 82             </field>
 83             <field property="user.birthday" depends="date">
 84                 <arg key="prompt.birthday"/>    
 85                 <var>
 86                     <var-name>datePatternStrict</var-name>
 87                     <var-value>yyyy-MM-dd</var-value>
 88                 </var>        
 89             </field>
 90             <field property="user.email" depends="email">
 91                 <arg key="prompt.email"/>            
 92             </field>
 93             <field property="user.zipcode" depends="mask">
 94                 <arg key="prompt.zipcode"/>    
 95                   <var>
 96                     <var-name>mask</var-name>
 97                     <var-value>^[1-9][0-9]{5}$</var-value>
 98                 </var>        
 99             </field>
100         </form>
101     </formset>
102 </form-validation>

Login.jsp

View Code
 1 <%@ page language="java" pageEncoding="GBK"%>
 2 
 3 <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
 4 <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
 5 <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
 6 <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
 7 
 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 9 <html:html lang="true">
10   <head>
11     <html:base />    
12     <title>Login.jsp</title>
13   </head>
14   <html:javascript formName="userLoginForm"/>
15   <body>
16     <html:form action="userLogin" method="post" >
17       <table border="0">
18         <tr>
19           <td>用户名:</td>
20           <td><html:text property="user.username" /></td>
21           <td><font color="red"><html:errors property="user.username" /></font></td>
22         </tr>
23         <tr>
24           <td>密码:</td>
25           <td><html:password property="user.password" /></td>
26           <td><font color="red"><html:errors property="user.password" /></font></td>
27         </tr>
28         <tr>
29           <td>重复密码:</td>
30           <td><html:password property="password2" /></td>
31           <td><font color="red"><html:errors property="password2" /></font></td>         
32         </tr>
33         <tr>
34           <td>生日:</td>
35           <td><html:text property="user.birthday" /></td>
36           <td><font color="red"><html:errors property="user.birthday" /></font></td>
37         </tr>
38         <tr>
39           <td>E-Mail:</td>
40           <td><html:text property="user.email" /></td>
41           <td><font color="red"><html:errors property="user.email" /></font></td>
42         </tr>
43         <tr>
44           <td>邮政编码:</td>
45           <td><html:text property="user.zipcode" /></td>
46           <td><font color="red"><html:errors property="user.zipcode" /></font></td>
47         </tr>        
48         <tr>
49           <td colspan="2" align="center"><html:submit value="提交"/></td>
50         </tr>
51       </table>
52       
53     </html:form>
54   </body>
55 </html:html>

ApplicationResources.properties

View Code
# Resources for parameter 'org.xiong.validate.struts.ApplicationResources'
# Project ValidatorDemo
   errors.required=<li>{0}\u662F\u5FC5\u987B\u7684\uFF01
   errors.minlength=<li>{0}\u957F\u5EA6\u4E0D\u5C0F\u4E8E{1}\u4E2A\u5B57\u7B26\uFF01
   errors.maxlength=<li>{0}\u957F\u5EA6\u4E0D\u5927\u4E8E{1}\u4E2A\u5B57\u7B26\uFF01.
   errors.invalid=<li>{0}\u662F\u65E0\u6548\u7684\uFF01

   errors.byte=<li>{0} must be a byte.
   errors.short=<li>{0} must be a short.
   errors.integer=<li>{0} must be an integer.
   errors.long=<li>{0} must be a long.
   errors.float=<li>{0} must be a float.
   errors.double=<li>{0} must be a double.

   errors.date=<li>{0}\u4E0D\u662F\u4E00\u4E2A\u5408\u6CD5\u7684\u65E5\u671F\u683C\u5F0F\uFF01
   errors.range=<li>{0} is not in the range {1} through {2}.
   errors.creditcard=<li>{0} is an invalid credit card number.
   errors.email=<li>{0}\u683C\u5F0F\u4E0D\u5408\u6CD5\uFF01
   
   
   prompt.username=\u7528\u6237\u540D
   prompt.password=\u5BC6\u7801
   errors.password2=<li>\u4E24\u6B21\u8F93\u5165\u7684\u5BC6\u7801\u4E0D\u4E00\u81F4\uFF01
   prompt.birthday=\u51FA\u751F\u65E5\u671F
   prompt.email=E-Mail
   prompt.zipcode=\u90AE\u653F\u7F16\u7801
   

posted on 2012-04-11 02:00  茫然若失  阅读(267)  评论(0编辑  收藏  举报

导航