szympans
szympans

Reputation: 3

Struts2 interceptor parametrs and its lifecycle

we are using Struts2 in our code and I have scenario where I need to add custom parameter to the interceptor. I have defined setter and getter in the interceptor

public Collection<Class<?>> getGroups();
public void setGroups(String groupsString);

I have interceptor defined in the stack:

<interceptor-ref name="beanValidatorInterceptor">
    <param name="excludeMethods">*</param>
</interceptor-ref>

And I am setting additional parametres on action level

<action name="...">
  <interceptor-ref name="loginRequired">
    <param name="beanValidatorInterceptor.includeMethods">execute</param>
    <param name="beanValidatorInterceptor.groups">com.company.MyGroup</param>
  </interceptor-ref>
  <result>some.jsp</result>
</action>

I have one issue and one question

Upvotes: 0

Views: 1119

Answers (1)

Dave Newton
Dave Newton

Reputation: 160170

Yes and no. Interceptors are like servlets--there is only one instance of an interceptor.*

Note, however, that with your current definition, only the "loginRequired" interceptor is defined on that action--none of the other framework interceptors will be run. If you declare an <interceptor-ref> on an action, you must define all the interceptors.

From the config it looks as though you're trying to define what group may access an action: that information should be stored in the action, not the interceptor, since it is the action that's being instrumented.

You have several options. You may use the "staticParams" interceptor to set the information on the action, you may define and implement an interface on the action and include a method that retrieves the permissions for that action, or use annotations on the action and interrogate the action and/or action method within the interceptor.

IMO the interceptor should be generic and not receive these types of parameters: you are adding action metadata to an interceptor. Instead, add the action metadata to the action, and use the interceptor to act on that metadata.

*Per interceptor-ref, actually; there can be multiple instances of an interceptor, but only when explicitly referenced in a stack declaration, and even then, there will be only one instance of the interceptor shared among requests that flow through that stack.

Upvotes: 1

Related Questions