Struts原理与实践(6)

来源:互联网 发布:sql中count 1 编辑:程序博客网 时间:2024/05/21 02:20

本文我们来讨论一下Struts中的输入校验问题。我们知道,信息系统有垃圾进垃圾出的特点,为了避免垃圾数据的输入,对输入进行校验是任何信息系统都要面对的问题。在传统的编程实践中,我们往往在需要进行校验的地方分别对它们进行校验,而实际上需要校验的东西大多都很类似,如必需的字段、日期、范围等等。因此,应用程序中往往到处充斥着这样一些显得冗余的代码。而与此形成鲜明对照的是Struts采用Validator框架(Validator框架现在是Jakarta Commons项目的一部分)来解决校验问题,它将校验规则代码集中到外部的且对具体的应用程序中立的.xml文件中,这样,就将那些到处出现的校验逻辑从应用程序中分离出来,任何一个Struts应用都可以使用这个文件,同时还为校验规则的扩展提供了便利。更难能可贵的是由于Validator框架将校验中要用到的一些消息等信息与资源绑定有机结合在一起,使得校验部分的国际化编程变得十分的便捷和自然。

    Validator框架大致有如下几个主要组件:

    Validators:是Validator框架调用的一个Java类,它处理那些基本的通用的校验,包括required、mask(匹配正则表达式)、最小长度、最大长度、范围、日期等

    .xml配置文件:主要包括两个配置文件,一个是validator-rules.xml,另一个是validation.xml。前者的内容主要包含一些校验规则,后者则包含需要校验的一些form及其组件的集合。

    资源绑定:提供(本地化)标签和消息,缺省地共享struts的资源绑定。即校验所用到的一些标签与消息都写在ApplicationResources.properity文件中。

    Jsp tag:为给定的form或者action path生成JavaScript validations。

    ValidatorForm:它是ActionForm的一个子类。

    为了对Validator框架有一个比较直观的认识,我们还是以前面的登陆例子的输入来示范一下Validator框架的使用过程:

    首先,找一个validator-rules.xml文件放在mystruts/WEB-INF目录下,下面是该文件中涉及到的required验证部分代码的清单:

 

  1. <validator name="required"  
  2. <!--①-->  
  3.             classname="org.apache.struts.validator.FieldChecks"  
  4.                method="validateRequired"  
  5.          methodParams="java.lang.Object,   
  6.                        org.apache.commons.validator.ValidatorAction,   
  7.                        org.apache.commons.validator.Field,   
  8.                        org.apache.struts.action.ActionErrors,   
  9. javax.servlet.http.HttpServletRequest"   
  10. <!--②-->  
  11.                   msg="errors.required">  
  12. <!--③-->  
  13.          <javascript><![CDATA[  
  14.             function validateRequired(form) {  
  15.                 var isValid = true;  
  16.                 var focusField = null;  
  17.                 var i = 0;  
  18.                 var fields = new Array();  
  19.                 oRequired = new required();  
  20.                 for (x in oRequired) {  
  21.                     var field = form[oRequired[x][0]];  
  22.                       
  23.                     if (field.type == 'text' ||  
  24.                         field.type == 'textarea' ||  
  25.                         field.type == 'file' ||  
  26.                         field.type == 'select-one' ||  
  27.                         field.type == 'radio' ||  
  28.                         field.type == 'password') {  
  29.                           
  30.                         var value = '';  
  31.                 // get field's value  
  32.                 if (field.type == "select-one") {  
  33.                 var si = field.selectedIndex;  
  34.                 if (si >= 0) {  
  35.                 value = field.options[si].value;  
  36.                             }  
  37.                         } else {  
  38.                             value = field.value;  
  39.                         }  
  40.                           
  41.                         if (trim(value).length == 0) {  
  42.                           
  43.                             if (i == 0) {  
  44.                                 focusField = field;  
  45.                             }  
  46.                             fields[i++] = oRequired[x][1];  
  47.                             isValid = false;  
  48.                         }  
  49.                     }  
  50.                 }  
  51.                 if (fields.length > 0) {  
  52.                    focusField.focus();  
  53.                    alert(fields.join('/n'));  
  54.                 }  
  55.                 return isValid;  
  56.             }  
  57.               
  58.             // Trim whitespace from left and right sides of s.  
  59.             function trim(s) {  
  60.                 return s.replace( /^/s*/, "" ).replace( //s*$/, "" );  
  61.             }  
  62.               
  63.             ]]>  
  64.          </javascript>  
  65.   
  66. </validator>  



    ① 节的代码是引用一个服务器边的验证器,其对应的代码清单如下: 

 

  1. public static boolean validateRequired(Object bean,   
  2.                                            ValidatorAction va, Field field,   
  3.                                            ActionErrors errors,   
  4.                                            HttpServletRequest request) {   
  5.   
  6.         String value = null;   
  7.         if (isString(bean)) {   
  8.             value = (String) bean;   
  9.         } else {   
  10.             value = ValidatorUtil.getValueAsString(bean, field.getProperty());   
  11.         }   
  12.            
  13.         if (GenericValidator.isBlankOrNull(value)) {   
  14.             errors.add(field.getKey(), Resources.getActionError(request, va, field));   
  15.             return false;   
  16.         } else {   
  17.             return true;   
  18.         }   
  19. }  



    ② 节是验证失败后的出错信息,要将对应这些键值的信息写入到ApplicationResources.properity文件中,常见的错误信息如下: 

 

  1. # Standard error messages for validator framework checks   
  2. errors.required={0} is required.   
  3. errors.minlength={0} can not be less than {1} characters.   
  4. errors.maxlength={0} can not be greater than {1} characters.   
  5. errors.invalid={0} is invalid.   
  6. errors.byte={0} must be a byte.   
  7. errors.short={0} must be a short.   
  8. errors.integer={0} must be an integer.   
  9. errors.long={0} must be a long.   
  10. errors.float={0} must be a float.   
  11. errors.double={0} must be a double.   
  12. errors.date={0} is not a date.   
  13. errors.range={0} is not in the range {1} through {2}.   
  14. errors.creditcard={0} is an invalid credit card number.   
  15. errors.email={0} is an invalid e-mail address.  



    ③ 节的代码用于客户边的JavaScript验证

其次,在validation.xml文件中配置要验证的form极其相应的字段,下面是该文件中的代码: 

 

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE form-validation PUBLIC "-//Apache Software Foundation   
  3. //DTD Commons Validator Rules Configuration 1.0//EN"    
  4. "http://jakarta.apache.org/commons/dtds/validator_1_0.dtd">  
  5. <form-validation>  
  6. <formset>  
  7. <form name="userInfoForm">  
  8. <field property="username"  
  9. depends="required,mask,minlength,maxlength">  
  10. <arg0 key="logon.jsp.prompt.username" resource="true"/>  
  11. <arg1 name="minlength" key="${var:minlength}" resource="false"/>  
  12. <arg1 name="maxlength" key="${var:maxlength}" resource="false"/>  
  13. <var>  
  14. <var-name>mask</var-name>  
  15. <var-value>^/w</var-value>  
  16. </var>  
  17. <var>  
  18. <var-name>minlength</var-name>  
  19. <var-value>2</var-value>  
  20. </var>  
  21. <var>  
  22. <var-name>maxlength</var-name>  
  23. <var-value>16</var-value>  
  24. </var>  
  25. </field>  
  26. <field property="password"  
  27. depends="required,minlength,maxlength">  
  28. <arg0 key="logon.jsp.prompt.password" resource="true"/>  
  29. <arg1 name="minlength" key="${var:minlength}" resource="false"/>  
  30. <arg1 name="maxlength" key="${var:maxlength}" resource="false"/>  
  31. <var>  
  32. <var-name>minlength</var-name>  
  33. <var-value>2</var-value>  
  34. </var>  
  35. <var>  
  36. <var-name>maxlength</var-name>  
  37. <var-value>16</var-value>  
  38. </var>  
  39. </field>  
  40. </form>  
  41. </formset>  
  42. </form-validation>  



    这里要注意的是:该文中的和中的键值都是取自资源绑定中的。前面还讲到了出错信息也是写入ApplicationResources.properity文件中,因此,这就为国际化提供了一个很好的基础。

    再次,为了使服务器边的验证能够进行,将用到的formBean从ActionForm的子类改为ValidatorForm的子类,即:
    将public class UserInfoForm extends ActionForm改为:public class UserInfoForm extends ValidatorForm

    到此,进行服务器边的验证工作已经一切准备得差不多了,此时,只要完成最后步骤就可以实验服务器边的验证了。但大多数情况下,人们总希望把这些基本的简单验证放在客户边进行。

    为了能进行客户边的验证,我们还要对logon.jsp文件做适当的修改。

    将

  1. <html:form action="/logonAction.do" focus="username">  

改为

 

  1. <html:form action="/logonAction.do" focus="username" 
  2.                    onsubmit="return validateUserInfoForm(this)">  



    在标签后加上:

  1. <html:javascript dynamicJavascript="true" staticJavascript="true" 
  2.                                                   formName="userInfoForm"/>  



    最后,对struts的配置文件struts-config.xml作适当的修改:
    1、将

  1. <action input="/logon.jsp" name="userInfoForm"  
  2.  path="/logonAction" scope="session" type="action.LogonAction" validate="false" >  

改为

  1. <action input="/logon.jsp" name="userInfoForm"    
  2. path="/logonAction" scope="session" type="action.LogonAction" validate="true" >  

其作用是要求进行校验

    2、将下列代码放在struts-config.xml文件中的标签前。其作用是将用于校验的各个组件结合在一起。 

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



    到此为止,我们的一切工作准备就绪,您可以享受自己的劳动成果了,试着输入各种组合的用户名和口令,看看它们的验证效果。仔细体会你会发现,服务器边的验证要更全面一些,比如对password的字符长度的验证。

    参考文献:
《Struts in Action》Ted Husted Cedric Dumoulin George Franciscus David Winterfeldt著
《Programming Jakarta Struts》Chuck Cavaness著

    本文作者:张永美 罗会波 湖北省当阳市国税局 可通过lhbf@sina.com与他们联系

 
原创粉丝点击