我无法同时使用 validate 方法中的验证和validation.xml 中的验证,如果我评论 validate() 方法,则表单 validation.xml 验证正在工作,否则只有在 validate 方法中完成的验证才有效!
我正在粘贴下面涉及的代码摘录,请让我知道有价值的建议:
public class DvaUpdateBean extends ValidatorActionForm implements Serializable {
//getter setters
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
String method = request.getParameter("method");
if (method == null){
return errors;
}
if(method.equalsIgnoreCase("updateDvar")){
if (getDescription() == null || getDescription().length() < 1) {
errors.add("descreq", new ActionMessage("error.ps.descreq"));
}
if (getReason() == null || getReason().length() < 1) {
errors
.add("reasonreqd", new ActionMessage(
"error.ps.reasonreqd"));
}
if( getInVoiceRadio() == null || getInVoiceRadio().length() < 1 ) {
errors.add("invreq",new ActionMessage("error.dva.invreq"));
}
if ( getInVoiceRadio()!=null && getInVoiceRadio().equalsIgnoreCase("Y") && ( getInVoiceNumber()==null || getInVoiceNumber().length() < 1) ) {
errors.add("invnumreq",new ActionMessage("error.dva.invnumreq"));
//if ( getCrDate()!=null && getCrDate().length()<1) {
//errors.add("condatereq", new ActionMessage("error.wlrr.condatereq"));
//}
}
if ( getInVoiceRadio()!=null && getInVoiceRadio().equalsIgnoreCase("Y") && ( getInVoiceNumber()!=null || getInVoiceNumber().length() > 1) && ( getInVoiceDate()==null || getInVoiceDate().length()<1 ) ) {
errors.add("invdatereq", new ActionMessage("error.dva.invdatereq"));
}
if ( getInVoiceRadio()!=null && getInVoiceRadio().equalsIgnoreCase("N") && ( ( getInVoiceNumber()!=null && getInVoiceNumber().length() > 1) || ( getInVoiceDate()!=null && getInVoiceDate().length()>1 ) ) ) {
errors.add("invreqyn", new ActionMessage("error.dva.invreqyn"));
}
}
return errors;
}
}
验证.xml
<form-validation>
<formset>
<form name="/dvaSearch">
<field property="dvaSearchBean.dvasrNumber" depends="integer">
<msg name="integer" key="label.invalidDvasrNumber" />
</field>
<field property="searchOriDate" depends="date">
<var><var-name>datePatternStrict</var-name><var-value>MM-dd-yyyy</var-value></var>
<msg name="date" key="label.invalidOrignDate"/>
</field>
</form>
</formset>
</form-validation>
Struts-config.xml
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />
</plug-in>