【问题标题】:AJAX validation fails for all form data所有表单数据的 AJAX 验证失败
【发布时间】:2014-12-05 21:27:26
【问题描述】:

我正在使用 ajax jquery 验证。

https://code.google.com/p/struts2-jquery/wiki/Validation

在我的 struts.xml 中,我有

<action name="createChannel" class="channel.ChannelAction" method="createChannel">
<interceptor-ref name="jsonValidationWorkflowStack"/>
<result  name="success">createSuccess.jsp</result>
<result  name="input">createChannel.jsp</result>
</action>

在 JSP 页面中我有:

<s:form method="get" action="createChannel" >
<s:textfield name="channelName" label="Channel Name" />
<s:textfield name="channelBand" label="Channel Band"/>
<s:textfield name="vcFrequency" label="Video Carrier Frequency"/>
<s:textfield name="acFrequency" label="Audio Carrier Frequency"/>
<s:select name="chargeType" label="Charge Type" list="{'PrePaid','PostPaid'}"/>
<s:select name="transmissionType" label="Transmission Type" list="{'HD','Standard'}"/>
<s:textfield name="channelCharge" label="Channel Charge"/>
<sj:submit value="Submit" button="true" validate="true" targets="result"  />
</s:form>
<div id="result">Result:</div>

我有带有注释验证的模型类 Channel:

public class Channel {
    private int channelId;
    private String channelName;
    private String channelBand;
    private double vcFrequency;
    private double acFrequency;
    private String chargeType; //PrePaid or PostPaid
    private String transmissionType; //HD or Standard
    private double channelCharge;

    private Set<ChannelPackage> channelPackage;

    public Channel(){
        channelPackage=new HashSet<>();
    }

    public int getChannelId() {
        return channelId;
    }

    public void setChannelId(int channelId) {
        this.channelId = channelId;
    }

    @RequiredStringValidator(trim=true,type=ValidatorType.FIELD,message="Please enter valid channel name")
    public String getChannelName() {
        return channelName;
    }

    public void setChannelName(String channelName) {
        this.channelName = channelName;
    }
    @RequiredStringValidator(trim=true,type=ValidatorType.FIELD,message="Please enter valid channel band")
    public String getChannelBand() {
        return channelBand;
    }

    public void setChannelBand(String channelBand) {
        this.channelBand = channelBand;
    }
    @DoubleRangeFieldValidator(type = ValidatorType.SIMPLE,fieldName = "vcFrequency",minInclusive = "40",maxInclusive = "225",message = "The scale must be between ${minInclusive} and ${maxInclusive} (exclusive)")
    public double getVcFrequency() {
        return vcFrequency;
    }

    public void setVcFrequency(double vcFrequency) {
        this.vcFrequency = vcFrequency;
    }
    @DoubleRangeFieldValidator(type = ValidatorType.SIMPLE,fieldName = "acFrequency",minInclusive = "45",maxInclusive = "230",message = "The scale must be between ${minInclusive} and ${maxInclusive} (exclusive)")
    public double getAcFrequency() {
        return acFrequency;
    }

    public void setAcFrequency(double acFrequency) {
        this.acFrequency = acFrequency;
    }

    public String getChargeType() {
        return chargeType;
    }

    public void setChargeType(String chargeType) {
        this.chargeType = chargeType;
    }


    public String getTransmissionType() {
        return transmissionType;
    }

    public void setTransmissionType(String transmissionType) {
        this.transmissionType = transmissionType;
    }
    @DoubleRangeFieldValidator(type = ValidatorType.SIMPLE,fieldName = "channelCharge",minExclusive = "20.5",maxExclusive = "78.5",message = "The scale must be between ${minExclusive} and ${maxExclusive} (exclusive)")
    public double getChannelCharge() {
        return channelCharge;
    }

    public void setChannelCharge(double channelCharge) {
        this.channelCharge = channelCharge;
    }


    public Set<ChannelPackage> getChannelPackage() {
        return channelPackage;
    }

    public void setChannelPackage(Set<ChannelPackage> channelPackage) {
        this.channelPackage = channelPackage;
    }

    public String toString(){
        return channelId+": "+channelName;
    }

}

我的动作类实现了模型驱动接口。我的问题是:

  • 即使我提交了正确数据的表单,我的表单也会显示错误 消息。

  • 连同错误消息,返回的输入结果是 显示在结果 div 中。这意味着我在 jsp 页面上有两个表单 点击提交按钮后。

  • 如果我删除 jsonValidationWorkflowStack struts.xml 中的拦截器 我的表单被提交了两次。

这是提交后的截图:

请帮我解决这个问题。

【问题讨论】:

    标签: ajax jsp struts2 struts2-jquery struts-validation


    【解决方案1】:

    一个问题有很多问题...顺便说一句,我们开始吧:

    1.即使我提交了正确数据的表单,我的表单也会显示错误消息。

    这是由于您正在使用的拦截器堆栈:

    <interceptor-ref name="jsonValidationWorkflowStack"/>
    

    定义为

    <interceptor-stack name="jsonValidationWorkflowStack">
        <interceptor-ref name="basicStack"/>
        <interceptor-ref name="validation">
            <param name="excludeMethods">input,back,cancel</param>
        </interceptor-ref>
        <interceptor-ref name="jsonValidation"/>
        <interceptor-ref name="workflow"/>
    </interceptor-stack>
    

    basicStack 是

    <interceptor-stack name="basicStack">
        <interceptor-ref name="exception"/>
        <interceptor-ref name="servletConfig"/>
        <interceptor-ref name="prepare"/>
        <interceptor-ref name="checkbox"/>
        <interceptor-ref name="params"/>
        <interceptor-ref name="conversionError"/>
    </interceptor-stack>
    

    如您所见,此处不涉及ModelDriven Interceptor。如果您想继续使用 ModelDriven,您需要:

    • 添加ModelDriven Interceptor
    • 确保它在Validation Interceptor 之前运行,否则在执行验证时将不会(尚未)在模型上设置您发送的参数。 Read this answer 完全理解我的意思。

    这就够了:

    <action name="createChannel" class="channel.ChannelAction" method="createChannel">
        <interceptor-ref name="modelDriven"/>
        <interceptor-ref name="jsonValidationWorkflowStack"/>
        <result  name="success">createSuccess.jsp</result>
        <result  name="input">createChannel.jsp</result>
    </action>
    

    2。与错误消息一起,返回的输入结果显示在结果 div 中。这意味着点击提交按钮后,我在 jsp 页面上有两个表单。

    这很明显,你的设计就是这样,如果是INPUT,它应该如何自动更改?你需要重新考虑你的设计。例如,您可以定位您所在的表单,例如:

    <div id="result">
        <s:form method="get" action="createChannel" >
            <s:textfield name="channelName" label="Channel Name" />
            <s:textfield name="channelBand" label="Channel Band"/>
            <s:textfield name="vcFrequency" label="Video Carrier Frequency"/>
            <s:textfield name="acFrequency" label="Audio Carrier Frequency"/>
            <s:select name="chargeType" label="Charge Type" list="{'PrePaid','PostPaid'}"/>
            <s:select name="transmissionType" label="Transmission Type" list="{'HD','Standard'}"/>
            <s:textfield name="channelCharge" label="Channel Charge"/>
            <sj:submit value="Submit" button="true" validate="true" targets="result"  />
        </s:form>
    </div>
    

    然后在INPUTSUCCESS 的情况下,您返回一条消息,重新填充表单和字段。最好用&lt;s:include/&gt;创建一个用于原始页面的JSP sn-p,以遵循DRY。

    【讨论】:

    • 是的,我做到了。非常感谢......它工作!我包括了模型驱动拦截器
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-06
    • 2019-05-10
    • 1970-01-01
    • 2019-02-11
    相关资源
    最近更新 更多