在Struts 1中一起使用validate方法和validationxml

我无法在validation.xml中validation方法和validation中使用validation,如果我对validate()方法进行注释,那么表单validation.xmlvalidation工作正常,否则只有在validate方法中完成的validation才有效!

我正在粘贴下面涉及的代码摘录,请告诉我有价值的建议:

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) && ( getInVoiceDate()==null || getInVoiceDate().length() 1) || ( getInVoiceDate()!=null && getInVoiceDate().length()>1 ) ) ) { errors.add("invreqyn", new ActionMessage("error.dva.invreqyn")); } } return errors; } 

}

validation.xml中

    
datePatternStrictMM-dd-yyyy

struts-config.xml中

    

我遇到了同样的问题ashwinsakthi。 这是我解决它的方式。

注意:我的表单beaninheritance自ValidatorForm,而不是代码中的ValidatorActionForm。 ValidatorActionForminheritance自ValidatorForm,所以我认为它会起作用。

我们的想法是从表单内部调用父validation方法。 您将首先从validation器框架中获取错误(如果有)。 然后你继续以正常方式添加自己的validation。

 @Override public ActionErrors validate( ActionMapping mapping, HttpServletRequest request) { // errors return from validator framework (validation.xml file rules) ActionErrors errors = super.validate(mapping, request); // Now check other properties (outside of validation.xml) if(somefield == "" ) { errors.add("example", new ActionMessage("some.resource.file.key")); } ... continue to validate your properties return errors; } 

在我的jsp文件中(我的表单是)我在发布表单后包含以下所有错误:

    

最后,这是我的struts-config.xml文件的一个片段。

      

在我的测试中,我得到了您期望的预期结果。 试试看。