简体   繁体   中英

Defining different validations based on the action method which is called in a Struts2 action

I'm creating a web application with Struts 2. In a JSP file, I created a form like this :

<s:form action="actionclassname!%{methodToCall}" method="post">
...
</s:form> 

In the action class, I created two methods which will be called depending on the value of the variable methodToCall . That works well. My problem is that the two methods of the action class need some validations. So I used annotations to validate the form if the first method is called :

@Validations(....) 
public String actionMethod1(){
    ...
}

@Validations(...) 
public String actionMehod2(){
    ...
}

The first method works well with validations. My problem is on the second method. It seems like when I define validations (also using annotations) for this second method, the validations I defined on first method are executed again before those I defined for the second method.

How to make validations on the second method to run when it's this method which is called by the JSP?

In the action configuration via annotation you should use a parameter validateAnnotatedMethodOnly for validation interceptor, like in this example

@Action(value="actionclassname", results = {
  @Result(name="input", location = "/actionclassname.jsp")
},interceptorRefs = @InterceptorRef(value="defaultStack", params = {"validation.validateAnnotatedMethodOnly", "true"}))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM