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?
There are 2 ways to do that: Through xml configuration and annotations.
XML Configuration: This is defined in action's declaration in struts.xml. You have to use defaultStack interceptor stack because it provides AnnotationValidationInterceptor a.k.a. validation and DefaultWorkflowInterceptor a.k.a. workflow that both handles validation and redirection when you use validation. Both can receive a parameter that indicates the methods that you don't want to be validated. In this example i have an action that i use to handle login actions like log in, log out, and so on. The common behavior is to validate login form to avoid get an empty user and password, so i define a validation like this in mi login action:
@Override
public void validate() {
if (getCodusr().trim().length() == 0) {
addFieldError("codusr", "You must have to enter a user code");
}
if (getClave().trim().length() == 0) {
addFieldError("clave", "You must have to enter a password");
}
}
But, i have the method of logout(), but i don't wanna validation when i call it because i just wannt destroy session and redirect user out of application, so i have to pass it as parameter to the two interceptors (workflow and validate) to avoid to execute validate() when i call logout(), so i define my action in this way:
<action name="login"> <interceptor-ref name="defaultStack" > <param name="workflow.excludeMethods">homePage,logout</param> <param name="validation.excludeMethods">homePage,logout</param> </interceptor-ref> <result name="homePage">/login/login.jsp</result> <result name="input">/login/login.jsp</result> <result name="backToLogin" type="redirectAction">login!homePage.action</result> </action>
As you can see, when i set this i say "Please guys (workflow and validation) don't execute validation when i call homePage() or logout()!!".
Annotations: Second way (and easiest for me) is to specify methods that skip validation through annotations. To do that simply use org.apache.struts2.interceptor.validation.SkipValidation annotation, that does the same that i did on struts.xml but a loot more simple. So, to replicate the same that i did in struts.xml i simply do this:
...//More code
import org.apache.struts2.interceptor.validation.SkipValidation;
...//More code
@SkipValidation
public String logout() {
//Method logic
return "someResult";
}
@SkipValidation
public String homePage() {
//Method logic
return "someResult";
}
... //More code
And that's it. Personally i prefeer the second way (Annotations), but that's a choice of each one. Enjoy it!!








June 22nd, 2010 - 07:41
thank you very much!! I’ve been looking for this all over the net! for 2 days now
hehe glad to finally find a solution!
August 5th, 2010 - 01:54
Thanks too, but i don’t understand some stuff.
I implemented validate(), and add the excludeMethods param to the workflow, but it didn’t work. Why I have to modify validation interceptor too, if i using the simple validation workflow, not the validation interceptor?
In fact, I tried it without excludeMethods in workflow, only in validator, and it works!
Greetings.
August 5th, 2010 - 10:20
Why doesn’t work? Throws an error at application deploy? Also, i recommend you to use @SKipValidation annotation, more clean and easy to use.