我的目标是编写一个拦截器,添加一些标题作为响应。我现在有以下拦截器
public class CachingInterceptor extends AbstractInterceptor{
@Override
public String intercept(ActionInvocation ai) throws Exception {
HttpServletResponse response = (HttpServletResponse) getActionContext(ai).get(StrutsStatics.HTTP_RESPONSE);
if(null != response) {
response.setHeader("Cache-control","no-store,no-cache");
response.setHeader("Pragma","no-cache");
response.setHeader("Expires","-1");
}
return ai.invoke();
}
}我需要增强它,使其能够在配置文件(struts.xml)中定义头部。
....
<!-- Define and add following interceptor in default interceptor stack -->
<interceptor name="CachingInterceptor" class="demo.CachingInterceptor">
....
<action name="myAction" class="demo.myAction">
....
<param name="Cache-control">no-store,no-cache</param>
<param name="Pragma">no-cache</param>
<param name="Expires">-1</param>
....
</action>现在,我必须在我的拦截器类中定义属性,以获取标头的值。
private String pragma; //with getter, setter
private String expires; //with getter, setter这里我有两个问题。
1.我不能在java中定义属性“缓存-控制”。
2.标头名称不可预测,即任何标头都可以在配置中定义为
<param name="other-header">some-value</param>我有两个问题:
发布于 2013-04-05 14:38:10
通过操作配置,您已经定义了几个通过staticParams拦截器处理的静态参数。这个拦截器应该在堆栈中先进行。然后,您只需从操作上下文中检索它们。
Map<String, Object> params = ActionContext.getContext().getParameters();
response.setHeader("Cache-control", ((String[])params.get("Cache-control"))[0]);
response.setHeader("Pragma", ((String[])params.get("Pragma"))[0]);
response.setHeader("Expires", ((String[])params.get("Expires"))[0]); https://stackoverflow.com/questions/15826918
复制相似问题