在Struts 1中一起使用validate方法和验证xml

时间:2013-11-13 09:17:49

标签: java validation struts-1

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

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

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;
}

}

的validation.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>

1 个答案:

答案 0 :(得分:4)

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

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

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

@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文件中(我的表单是)我在发布表单后显示以下所有错误:

<font color="red">
<html:errors/>
</font>

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

   <action 
        path="/hellotest"
        type="com.dan.HelloTestAction"
        name="helloTestForm"
        scope="request"
        input="/index.jsp"
        validate="true">
    <forward name="success" path="/result.jsp"/>
    <forward name="failure" path="/index.jsp"/>
   </action>

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