qiyadeng

专注于Java示例及教程
posts - 84, comments - 152, trackbacks - 0, articles - 34

Struts之自定义Validator

Posted on 2005-10-31 18:00 qiyadeng 阅读(1166) 评论(0)  编辑  收藏 所属分类: J2EE
在使用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中的使用就不再说明了,大功告成:)。



只有注册用户登录后才能发表评论。


网站导航: