在 struts-config.xml 中
<form-bean name="myForm" type="com.validator.CustomeDynaValidatorForm">
<form-property name="testId" type="java.lang.Long"/>
</form-bean>
在validation.xml
<form name="myForm">
<field property="testId" depends="required">
<msg name="required" key="test ID required."/>
</field>
</form>
在验证规则.xml
<validator name="required"
classname="com.validator.CustomFieldChecks"
method="validateRequired"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
org.apache.commons.validator.Validator,
javax.servlet.http.HttpServletRequest"
msg=""/>
在 CustomFieldChecks.java 中
public static boolean validateRequired(Object bean, ValidatorAction va, Field field, ActionMessages errors,
Validator validator, HttpServletRequest request)
{
String value = null;
if (isString(bean))
{
value = (String) bean;
}
else
{
value = ValidatorUtils.getValueAsString(bean,
field.getProperty());
**// here value is 0 , when the field is left blank in UI**
}
if (GenericValidator.isBlankOrNull(value))
{
//add error message
return false;
}
else
{
return true;
}
}
有人可以告诉我,我如何确保如果该字段在 UI 中留空,则该值应为 null 而不是 0。有没有办法这样做???我正在使用 struts 1.2