struts 自定義validate 問題

小趙aa發表於2006-03-01
請教 : 我的自定義validator 不經過驗證,直接進入action 請幫忙

自定義validator類:
package com.house.validator;
import java.io.Serializable;
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.ActionMessages;
import org.apache.struts.validator.Resources;

public class ReValidator implements Serializable {
public ReValidator() {
super();
}
/**
* 驗證重複密碼
*/
public static boolean validateTwoFields(Object bean, ValidatorAction va,
Field field, ActionMessages errors, HttpServletRequest request) {
System.out.println("aaa");
String value = ValidatorUtil.getValueAsString(bean, field.getProperty());

String sProperty2 = field.getVarValue("secondProperty");
String value2 = ValidatorUtil.getValueAsString(bean, sProperty2);
System.out.println(value);
System.out.println(value2);
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;
}
}

form 是 動態form
<form-bean name="registerForm" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="key" type="java.lang.String" />
<form-property name="password" type="java.lang.String" />
<form-property name="re_password" type="java.lang.String"/>
<form-property name="clue" type="java.lang.String" />
<form-property name="email" type="java.lang.String" />
<form-property name="name" type="java.lang.String" />
</form-bean>

validator-rules

<validator name="twoFields"
classname="com.house.validator.ReValidator"
method="validateTwoFields"
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.twofields" />

validation 中

<field
property="password"
depends="required,minlength,twoFields">
<arg0 key="variable.password"/>
<arg1 name="minlength" key="${var:minlength}" resource="false"/>
<arg2 name="twoFields" key="variable.re_password"/>
<var>
<var-name>minlength</var-name>
<var-value>8</var-value>
</var>
<var>
<var-name>secondProperty</var-name>
<var-value>re_password</var-value>
</var>
</field>

相關文章