struts 自定义validate 问题

06-03-01 小赵aa
请教 : 我的自定义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>

猜你喜欢