Struts之自定義Validator

在使用Struts的時候,驗證是使用Validator來做,但是有時候需要有自己的驗證規則,幸運的是我們可以方便地進行擴展。
比如我們想定義一個規則來判定兩次輸入的密碼是相等的。我們需要這樣做,首先我們需要一個類,象這樣子 :

package com.motel168.util;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.validator.Field;
import org.apache.commons.validator.GenericValidator;
import org.apache.commons.validator.ValidatorAction;
import org.apache.commons.validator.ValidatorUtil;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.validator.Resources;

public class MyValidator {
    public static boolean validateTwoFields(Object bean,ValidatorAction va,Field field,ActionErrors errors,HttpServletRequest request){
       
        String value = ValidatorUtil.getValueAsString(bean,field.getProperty());
        String sProperty2 = field.getVarValue("secondProperty");
        String value2 = ValidatorUtil.getValueAsString(bean,sProperty2);
        if(!GenericValidator.isBlankOrNull(value)){
            try{
                if(!value.equals(value2)){
                    errors.add(field.getKey(),Resources.getActionError(request,va,field));
                    return false;
                }
            }catch(Exception e){
                errors.add(field.getKey(),Resources.getActionError(request,va,field));
                return false;
            }
        }
        return true;
    }
   
}

上面這個類就是用來判斷兩個域是否相等,很簡單,但是注意方法名一定要是validateXXX。

接下來需要擴展validator-rules.xml,在之前加入這麼一段:
      
             
   
 methodParams="java.lang.Object,org.apache.commons.validator.ValidatorAction,org.apache.commons.validator.Field,org.apache.struts.action.ActionErrors,javax.servlet.http.HttpServletRequest"
                   depends="required" msg="errors.twofield">
     
              function validateTwoFields(form){
              var bValid = true;
              var focusField = null;
              var i = 0;
              var fields = new Array();
              oTwoFields = new twofields();
              for(x in oTwoFields){
                  var field = form[oTwoFields[x][0]];
                  var secondField = form[oTwoFields[x][2]("secondProperty")];
                  if(field.type=="text"||field.type=="textarea"||field.type=="select-one"||field.type=="radio"||field.type=="password"){
                      var value;
                      var secondValue;
                      if(field.type=="select-one"){
                          var si = field.selectedIndex;
                          value = field.options[si].value;
                          secondValue = secondField.options[si].value;
                      }else{
                          value=field.value;
                          secondValue = secondField.value;
                      }
                      if(value!=secondValue){
                          if(i==0){
                              focusField = field;
                      }
                      fields[i++]=oTwoFields[x][1];
                      bValid = flase;
                  }
              }
          }
          if(fiels.length > 0){
              focusFiled.focus();
              alert(fields.join('\n'));
              }
          return bValid;
     
      ]]>
     

         
然後使用的時候就和其他沒什麼兩樣的了:
        
           
               
               
               
               
               
                    secondProperty
                    newPassword2
               

           

           
               
               
           

       


注意這個地方
                
                    secondProperty
                    newPassword2
               

就是我們在java類中讀取的變量,它代表的是另外的一個域。
在JSP中的使用就不再說明了,大功告成:)。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章