留空當在struts-config.xml中長田不來爲空,Struts表單在UI
<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>
在驗證-rules.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=""/>
In 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
您是否收到任何錯誤? – NPKR
@Pradeep:不,我沒有收到任何錯誤。其實我想拋出一個錯誤消息,如果字段留空。但自從它來到0時留空,所以它返回true,因此通過驗證 – JAB