I have following action mapping
<action name="theAction" ...>
...
<param name="param1">one</param>
<param name="param2">two</param>
...
<param name="paramN">nth-number</param>
...
</action>
I can get parameter map using following line in Interceptor
Map<String, Object> params = ActionContext.getContext().getParameters();
Just as above, is there any way to get interceptor parameters as defined in following mapping.
<action name="theAction" ...>
...
<interceptor-ref name="theInterceptor">
<param name="param1">one</param>
<param name="param2">two</param>
...
<param name="paramN">nth-number</param>
</interceptor-ref>
...
</action>
And action parameters are defined in following way, action parameters and interceptor parameters should be accessible separately.
<action name="theAction" ...>
...
<param name="param1">one</param>
<param name="param2">two</param>
...
<param name="paramN">nth-number</param>
....
<interceptor-ref name="theInterceptor">
<param name="param1">one</param>
<param name="param2">two</param>
...
<param name="paramN">nth-number</param>
</interceptor-ref>
...
</action>
Please note that I don't want to declare parameter fields in my interceptor as
//all fields with their getters and setters
private String param1;
private String param2;
...
private String paramN;
After Dev Blanked's asnwer, I implemented his technique. It did not work so I am sharing my code here. I am using Struts 2.3.1.2.
Libraries
Struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="the-base" namespace="/" extends="struts-default" abstract="true">
<interceptors>
<interceptor name="header" class="demo.interceptors.HttpHeaderInterceptor"></interceptor>
<interceptor-stack name="theStack">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="header"></interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="theStack"></default-interceptor-ref>
</package>
<package name="the-module" extends="the-base">
<action name="theAction">
<result>/the-action.jsp</result>
<interceptor-ref name="theStack">
<param name="header.Cache-control">no-store,no-cache</param>
<param name="header.Pragma">no-cache</param>
<param name="header.Expires">-1</param>
<param name="header.arbitrary">true</param>
</interceptor-ref>
</action>
</package>
</struts>
Interceptor
package demo.interceptors;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.StrutsStatics;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class HttpHeaderInterceptor extends AbstractInterceptor {
private final Map<String, String> interceptorConfigs = new HashMap<String, String>();
@Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("Calling 'intercept' method.");
HttpServletResponse response = (HttpServletResponse) invocation.getInvocationContext().get(StrutsStatics.HTTP_RESPONSE);
for(Entry<String, String> entry: interceptorConfigs.entrySet()) {
String header = entry.getKey();
String value = entry.getValue();
System.out.printf("Adding header: %s=%s\n",header,value);
response.setHeader(header, value);
}
return invocation.invoke();
}
public Map<String, String> getInterceptorConfigs() {
System.out.println("calling method 'getInterceptorConfigs'");
return interceptorConfigs;
}
public void addInterceptorConfig(final String configName, final String configValue) {
System.out.printf("Calling method 'addInterceptorConfig' with params configName = %s, configValue=%.\n",configName, configValue);
interceptorConfigs.put(configName, configValue);
}
}
Console Output when theAction
is hit.
Calling 'intercept' method.
In your custom interceptor you can define a map like below
private final Map<String, String> interceptorConfigs = new HashMap<String, String>();
public Map<String, String> getInterceptorConfigs() {
return interceptorConfigs;
}
public void addInterceptorConfig(final String configName, final String configValue) {
interceptorConfigs.put(configName, configValue);
}
Then in your action mappings you can pass in parameters like below .. these will be stored in the map of the interceptor
<action name="yourAction" class="your.actionClass">
<result name="success">some.jsp</result>
<interceptor-ref name="defaultStack">
<param name="yourInterceptor.interceptorConfigs.key">value</param>
<param name="yourInterceptor.interceptorConfigs.aParamName">paramValue</param> </interceptor-ref>
</action>
"yourInterceptor" refers to the name of the interceptor you have given when adding your interceptor to the struts.xml. When configured like above 'interceptorConfigs' map inside the interceptor will have , key/value pairs.
If you want to make these available to your action, you can just set the map as a context variable in the ActionContext
. This can then be retrieved inside the action.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With