在Struts中應用Validator進行兩個欄位的比較的方法。

chenjianxiong發表於2004-03-19
在struts中進行採用Validator進行輸入驗證有很多好處,完全可以透過修改XML檔案來根據客戶的要求定製資料驗證而不用修改程式碼。但是Valiator沒有提供現成的進行兩個欄位比較的驗證方法,如驗證輸入密碼和確認密碼一致,後面的日期必須大於前面的日期等等。下面是我實現的兩個欄位進行比較的驗證方法,可以進行兩個欄位是字串、日期、數字型別的資料的比較。下面是我的實現方法,望指正:

package com.sysway.quintum.validator;

import java.io.Serializable;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.DecimalFormat;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.validator.Field;
import org.apache.commons.validator.GenericTypeValidator;
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;
import com.sysway.quintum.validator.WrapData;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: Sysway Co.Ltd</p>
 * @author chen jianxiong
 * @version 1.0
 * @usage:
 *<validator name="compareTwoFields"
       classname="com.sysway.quintum.validator.CompareTwoFieldsValidator"
       method="validateTwoFields"
       msg="errors.twofields"/>
      <field property="password"
       depends="required,twofields">
          <arg0 key="typeForm.password.displayname"/>
          <var>
             <var-name>secondField</var-name>
             <var-value>confirmedPassword</var-value>
          </var>
          <var>
             <var-name>dataType</var-name>
             <var-value>String</var-value>
          </var>
          <var>
             <var-name>logicOperate</var-name>
             <var-value>Great</var-value>
          </var>
          <var>
             <var-name>pattern</var-name>
             <var-value>yyyy-mm-dd</var-value>
          </var>
     </field>
     * @logicOperate:equal, greaterEqual, greaterThan, lessEqual, lessThan, notEqual
     * @dataType:String,date,Number
     * @pattern:yyyy-mm-dd

 */

public class CompareTwoFieldsValidator implements Serializable {
    public static boolean validateTwoFields(Object bean, ValidatorAction va,
            Field field, ActionErrors errors, HttpServletRequest request) {
        String firstFieldValue = ValidatorUtil.getValueAsString(bean, field.getProperty());
        String secondFieldName = field.getVarValue("secondField");
        String secondFieldValue = ValidatorUtil.getValueAsString(bean, secondFieldName);
        if (!GenericValidator.isBlankOrNull(firstFieldValue) &&
            !GenericValidator.isBlankOrNull(secondFieldValue)) {
            String logicOperate = field.getVarValue("logicOperate");
            String dataType = field.getVarValue("dataType");
            String pattern = field.getVarValue("pattern");
//            System.out.println("firstFieldValue:" + firstFieldValue + "," +
//                               "secondFieldName:" + secondFieldName + "," +
//                               "secondFieldValue:" + secondFieldValue + "," +
//                               "logicOperate:" + logicOperate + "," +
//                               "dataType:" + dataType + "," +
//                               "pattern:" + pattern);
            Object oValue1 = null, oValue2 = null;
            try {
                if (dataType.equals("Number")) {
                    DecimalFormat df = new DecimalFormat();
                    oValue1 =  df.parse(firstFieldValue);
                    oValue2 = df.parse(secondFieldValue);
                }
                else if (dataType.equals("Date")) {
                    SimpleDateFormat sdf = new SimpleDateFormat(pattern);
                    oValue1 = sdf.parse(firstFieldValue);
                    oValue2 = sdf.parse(secondFieldValue);
                }
                else {
                    oValue1 = (String ) firstFieldValue;
                    oValue2 = (String ) secondFieldValue;
                }
            }
            catch (Exception ex) {
                ex.printStackTrace();
                return false;
            }
            try {
                if (logicOperate.equals("equal")) {
                    if (WrapData.compareTo(oValue1, oValue2) == WrapData.EQUAL)
                     return true;
                    else {
                        errors.add(field.getKey(),
                               Resources.getActionError(request, va, field));
                        return false;
                    }
                }
                else if (logicOperate.equals("greaterThan")) {
                    if (WrapData.compareTo(oValue1, oValue2) == WrapData.GREATERTHAN)
                        return true;
                    else {
                        errors.add(field.getKey(),
                               Resources.getActionError(request, va, field));
                        return false;
                    }
                }
                else {
                    errors.add(field.getKey(),
                               Resources.getActionError(request, va, field));
                    return false;
                }
/*                else if (logicOperate.equals("greaterEqual")) {
                }
                else if (logicOperate.equals("lessEqual")) {
                }
                else if (logicOperate.equals("lessThan")) {
                }
                else if (logicOperate.equals("notEqual")) {
                }*/
            } catch (Exception e) {
                errors.add(field.getKey(),
                           Resources.getActionError(request, va, field));
                return false;
            }
        }
        else {
            errors.add(field.getKey(),
                           Resources.getActionError(request, va, field));
            return false;
        }
    }
}

<p class="indent">

相關文章