struts validation occurring when page loads instead of on submit

前端 未结 3 958
梦如初夏
梦如初夏 2021-01-23 06:07

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

相关标签:
3条回答
  • 2021-01-23 06:12

    Annotate with

    @SkipValidation

    on method you don't need to validate.

    0 讨论(0)
  • 2021-01-23 06:25

    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.

    0 讨论(0)
  • 2021-01-23 06:30

    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.

    0 讨论(0)
提交回复
热议问题