JAVA How to JAVA Development tutorials and how to

4Jun/103

How to avoid validation on Action call – Struts 2

As you know, like Struts 1, Struts 2 allow us to define server side validations when you call an action. This is very useful because sometimes javascript client side validation is not enought when we want to validate input entries against data from DB or do complex validations.

How to define validations in JAVA Struts 2? First your action must extend ActionSupport class, and override it's method validate that has te following signature:

public void validate() {}

Inside validate() you define your validations and, if you add messages to ActionErrors or FieldErrors, Struts 2 automatically detects that it must redirect to input result defined in action's declaration in struts.xml, like this:

 <action name="login">
            <interceptor-ref name="defaultStack" />

            <result name="homePage">/login/login.jsp</result>
            <result name="input">/login/login.jsp</result>
...
            <result name="backToLoginPer">/login/login.jsp</result>
        </action>

The problem is that sometimes you want that validation applies only on certain action calls, that is, avoid validation on certain action calls. How to do this?