我有以下动作映射
<action name="theAction" ...>
...
<param name="param1">one</param>
<param name="param2">two</param>
...
<param name="paramN">nth-number</param>
...
</action>
我可以用下面一行在拦截器得到的参数图
Map<String, Object> params = ActionContext.getContext().getParameters();
如上只是, 有没有什么办法让在下面的映射定义拦截器的参数 。
<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>
和行动参数在这样定义的,动作参数和拦截器参数应分别访问。
<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>
请注意,我不希望在我的拦截,以申报参数字段
//all fields with their getters and setters
private String param1;
private String param2;
...
private String paramN;
开发中空白的asnwer后,我实现了自己的技术。 它没有工作,所以我在这里分享我的代码。 我使用Struts 2.3.1.2。
图书馆
- ASM-3.3.jar
- ASM-的commons-3.3.jar
- ASM-树3.3.jar
- 公地文件上传-1.2.2.jar
- 公地IO-2.0.1.jar
- 公地郎2.5.jar
- freemarker的-2.3.18.jar
- Javassist进行-3.11.0.GA.jar
- OGNL-3.0.4.jar
- Struts2的核心 - 2.3.1.2.jar
- XWork的核心,2.3.1.2.jar
在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>
拦截器
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);
}
}
当控制台输出 theAction
被击中。
Calling 'intercept' method.