3

I'm using struts 2.3.1 with token interceptor.How can i use token interceptor in annotation(convension) based Action Class.

here is My struts.xml

<action name="tokenAction" class="roseindia.action.TokenAction">

 <interceptor-ref name="token" />

 <interceptor-ref name="basicStack"/>

 <result name="success" >/success.jsp</result>

 <result name="invalid.token">/index.jsp</result>

can any one please tell annotation based for the same.

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
edaklij
  • 4,121
  • 11
  • 31
  • 43

1 Answers1

2

It looks to me like this is fairly clear in the documentation here, you need to do this:

package com.example.actions;

import com.opensymphony.xwork2.ActionSupport; 
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;

@InterceptorRefs({
    @InterceptorRef("token"),
    @InterceptorRef("basicStack")
})
@Results({
    @Result(name="success", location="/success.jsp")
    @Result(name="invalid.token", location="/index.jsp")
})
public class HelloWorld extends ActionSupport {
  @Action(interceptorRefs={
      @InterceptorRef("token"),
      @InterceptorRef("basicStack")
  })
  public String myActionMethod() {
    //do stuff
    return SUCCESS;
  }
}
Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
  • Added then to the example. – Boris the Spider Feb 20 '13 at 16:12
  • 1
    OK thanks.but token Interceptor in above example is invoke entire actions for that action class.how can i invoke token interceptor for a particular action. – edaklij Feb 21 '13 at 06:24
  • Edited to provide an example. – Boris the Spider Feb 21 '13 at 09:39
  • Note that having InterceptorRef defined on the class (or abstract super class) will override the method level ones defined. Not exactly how I would want it - I'd want the class level InterceptorRef on abstract class to act as the general stack, then at the method level to override that (more specific). So I removed from abstract class and just defined it on *all* my @Actions at the method level. – Skystrider Jan 12 '23 at 17:32