所有表单数据的AJAX验证都失败

时间:2014-12-05 21:27:27

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

我正在使用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的拦截器我的表单已经提交了两次。

这是提交后的截图:

请帮助我做到这一点。

enter image description here

1 个答案:

答案 0 :(得分:2)

在一个问题上有这么多问题......顺便说一下,我们走了:

  

<强> 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的情况下,您返回一条消息,表单和字段重新填充。最好使用<s:include/>创建要在原始页面中使用的JSP片段,以跟随DRY。