开发者

struts validation occurring when page loads instead of on submit

开发者 https://www.devze.com 2023-03-06 17:22 出处:网络
Please forgive me; I am sort of new to struts.I am having an issue where my validation is occurring when the page loads instead of when i actually submit my form.I have been googling and scouring thro

Please forgive me; I am sort of new to struts. I am having an issue where my validation is occurring when the page loads instead of when i actually submit my form. I have been googling and scouring through forums all day and haven't had any luck. I'm obviously doing something incorrectly that should be very easy to nail down, but i haven't found what my issue is yet.

Here is a snippet of my struts.xml:

<action name="*Test" method="{1}" class="testClass">
            <interceptor-ref name="defaultStack"/>          
            <result name="init">jsp/index.jsp</result>
            <result name="input">jsp/index.jsp</result>
            <result name="submit">jsp/results.jsp</result>
</action>

As you can see, I have one action that I want to submit to 开发者_开发问答results.jsp if the validation is successful. Otherwise, i want it to show my index.jsp again. the pages navigate correctly, as far as i can tell. I can navigate to app/initTest.action and submit my form and it will take me to the results page and show me my expected results. My issue lies with the validation.

It seems as though the validation is working, just at the wrong time. I have a [Action]-validation.xml created in the same location as my class that the action is tied to and when the page loads initially, i see the error message from this xml file on the page already.

Here's what i have in my validation.xml file:

<validators>
    <field name="TestBean.idNumber">
        <field-validator type="requiredstring">
            <message>Please select an item.</message>
        </field-validator>
    </field>
</validators>   

Any assistance you can provide would be greatly appreciated!

thanks!


@nmc, I'm trying to visualize the option of using a separate action. Would it be something like this? If the validation fails, will it redirect back to the original form page?

<action name="*Test" method="{1}" class="testClass">         
        <result name="init">jsp/index.jsp</result>
        <result name="submit">initTestResults</result>
</action>
<action name="*TestResults" method="{1}" class="testClass2">
        <interceptor-ref name="defaultStack"/>          
        <result name="init">jsp/indexResults.jsp</result>
        <result name="input">jsp/indexResults.jsp</result>
</action>

I'm trying to figure out which option is better for my purpose.

Thanks for your input.


As nmc said, validation fires for the action, and that is what is causing the validation to occur. However, you can still do this without splitting it into two different actions.

First, you could fix this by getting rid of the init method, and just using input - so your initial url would be inputTest instead of initTest. The validation interceptor by default does not fire on the input method. This is how it is usually handled, and how I lay out all of my actions.

If you are set on using init, you can actually specify to the validation interceptor which methods to exclude from validation. You could change your xml to look like this:

<action name="*Test" method="{1}" class="testClass">    
        <interceptor-ref name="defaultStack">
            <param name="validation.excludeMethods">init,input</param>
        </interceptor-ref> 
        <result name="init">jsp/index.jsp</result>
        <result name="input">jsp/index.jsp</result>
        <result name="submit">jsp/results.jsp</result>
</action>

Using this xml simply tells the validation interceptor to not fire for the init or input methods.

Just a couple of different solutions to keep you using just one action class.


The validation runs before the action as specified in the validation file name, regardless of the result. So if you have one action to both display and process the form, then validation will run on that action before displaying and also before processing the form.

If this is not the behavior you want, move the display/load of the page to a different action.


Annotate with

@SkipValidation

on method you don't need to validate.

0

精彩评论

暂无评论...
验证码 换一张
取 消