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驗證部分代碼的清單:
<validator name="required" <!--①--> classname="org.apache.struts.validator.FieldChecks" method="validateRequired" methodParams="java.lang.Object, org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.Field, org.apache.struts.action.ActionErrors, javax.servlet.http.HttpServletRequest" <!--②--> msg="errors.required"> <!--③--> <javascript><![CDATA[ function validateRequired(form) { var isValid = true; var focusField = null; var i = 0; var fields = new Array(); oRequired = new required(); for (x in oRequired) { var field = form[oRequired[x][0]]; if (field.type == 'text' || field.type == 'textarea' || field.type == 'file' || field.type == 'select-one' || field.type == 'radio' || field.type == 'password') { var value = ''; // get field's value if (field.type == "select-one") { var si = field.selectedIndex; if (si >= 0) { value = field.options[si].value; } } else { value = field.value; } if (trim(value).length == 0) { if (i == 0) { focusField = field; } fields[i++] = oRequired[x][1]; isValid = false; } } } if (fields.length > 0) { focusField.focus(); alert(fields.join('/n')); } return isValid; } // Trim whitespace from left and right sides of s. function trim(s) { return s.replace( /^/s*/, "" ).replace( //s*$/, "" ); } ]]> </javascript> </validator> |
① 節的代碼是引用一個服務器邊的驗證器,其對應的代碼清單如下:
public static boolean validateRequired(Object bean, ValidatorAction va, Field field, ActionErrors errors, HttpServletRequest request) { String value = null; if (isString(bean)) { value = (String) bean; } else { value = ValidatorUtil.getValueAsString(bean, field.getProperty()); } if (GenericValidator.isBlankOrNull(value)) { errors.add(field.getKey(), Resources.getActionError(request, va, field)); return false; } else { return true; } } |
② 節是驗證失敗後的出錯信息,要將對應這些鍵值的信息寫入到ApplicationResources.properity文件中,常見的錯誤信息如下:
# Standard error messages for validator framework checks errors.required={0} is required. errors.minlength={0} can not be less than {1} characters. errors.maxlength={0} can not 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 an invalid credit card number. errors.email={0} is an invalid e-mail address. |
③ 節的代碼用於客戶邊的JavaScript驗證
其次,在validation.xml文件中配置要驗證的form極其相應的字段,下面是該文件中的代碼:
<?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="userInfoForm"> <field property="username" depends="required,mask,minlength,maxlength"> <arg0 key="logon.jsp.prompt.username" resource="true"/> <arg1 name="minlength" key="${var:minlength}" resource="false"/> <arg1 name="maxlength" key="${var:maxlength}" resource="false"/> <var> <var-name>mask</var-name> <var-value>^/w</var-value> </var> <var> <var-name>minlength</var-name> <var-value>2</var-value> </var> <var> <var-name>maxlength</var-name> <var-value>16</var-value> </var> </field> <field property="password" depends="required,minlength,maxlength"> <arg0 key="logon.jsp.prompt.password" resource="true"/> <arg1 name="minlength" key="${var:minlength}" resource="false"/> <arg1 name="maxlength" key="${var:maxlength}" resource="false"/> <var> <var-name>minlength</var-name> <var-value>2</var-value> </var> <var> <var-name>maxlength</var-name> <var-value>16</var-value> </var> </field> </form> </formset> </form-validation> |
這裏要注意的是:該文中的和中的鍵值都是取自資源綁定中的。前面還講到了出錯信息也是寫入ApplicationResources.properity文件中,因此,這就爲國際化提供了一個很好的基礎。
再次,爲了使服務器邊的驗證能夠進行,將用到的formBean從ActionForm的子類改爲ValidatorForm的子類,即:
將public class UserInfoForm extends ActionForm改爲:public class UserInfoForm extends ValidatorForm
到此,進行服務器邊的驗證工作已經一切準備得差不多了,此時,只要完成最後步驟就可以實驗服務器邊的驗證了。但大多數情況下,人們總希望把這些基本的簡單驗證放在客戶邊進行。
爲了能進行客戶邊的驗證,我們還要對logon.jsp文件做適當的修改。
將
<html:form action="/logonAction.do" focus="username"> |
<html:form action="/logonAction.do" focus="username" οnsubmit="return validateUserInfoForm(this)"> |
在標籤後加上:
<html:javascript dynamicJavascript="true" staticJavascript="true" formName="userInfoForm"/> |
最後,對struts的配置文件struts-config.xml作適當的修改:
1、將
<action input="/logon.jsp" name="userInfoForm" path="/logonAction" scope="session" type="action.LogonAction" validate="false" > |
<action input="/logon.jsp" name="userInfoForm" path="/logonAction" scope="session" type="action.LogonAction" validate="true" > |
2、將下列代碼放在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> |
到此爲止,我們的一切工作準備就緒,您可以享受自己的勞動成果了,試着輸入各種組合的用戶名和口令,看看它們的驗證效果。仔細體會你會發現,服務器邊的驗證要更全面一些,比如對password的字符長度的驗證。