Java 在Struts 1中同时使用验证方法和验证xml

Java 在Struts 1中同时使用验证方法和验证xml,java,validation,struts-1,Java,Validation,Struts 1,我无法同时使用validate方法中的validation和validation.xml中的validation,如果我对validate()方法进行注释,那么form validation.xml validation有效,否则只有validate方法中完成的验证有效 我正在粘贴下面涉及的代码摘录,请务必让我知道有价值的建议: public class DvaUpdateBean extends ValidatorActionForm implements Serializable { //

我无法同时使用validate方法中的validation和validation.xml中的validation,如果我对validate()方法进行注释,那么form validation.xml validation有效,否则只有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;
}
公共类DvaUpdateBean扩展了ValidatorActionForm,实现了可序列化{
//吸气剂二传手
公共ActionErrors验证(ActionMapping映射,
HttpServletRequest(请求){
ActionErrors=新的ActionErrors();
String方法=request.getParameter(“方法”);
if(方法==null){
返回错误;
}
if(method.equalsIgnoreCase(“updateDvar”)){
if(getDescription()==null | | getDescription().length()<1){
添加(“descreq”,新操作消息(“error.ps.descreq”);
}
如果(getReason()==null | | getReason().length()<1){
错误
.添加(“reasonreqd”,新操作消息(
“error.ps.reasonreqd”);
}
if(getInVoiceRadio()==null | | getInVoiceRadio().length()<1){
errors.add(“invreq”,新操作消息(“error.dva.invreq”);
}
如果(getInVoiceRadio()!=null&&getInVoiceRadio().equalsIgnoreCase(“Y”)和&(getInVoiceNumber()==null | | getInVoiceNumber().length()<1)){
errors.add(“invnumreq”,新操作消息(“error.dva.invnumreq”);
//如果(getCrDate()!=null&&getCrDate().length()1)&&(getInVoiceDate()=null | | | getInVoiceDate().length()1)| |(getInVoiceDate()!=null&&getInVoiceDate().length()>1))){
添加(“invreqyn”,新操作消息(“error.dva.invreqyn”);
}   
}   
返回错误;
}
}

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>

我在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>


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

我在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>

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