我编写了一个拦截器以防止缓存,但页面仍然缓存。
拦截器:
public class ClearCacheInterceptor implements Interceptor {
public String intercept(ActionInvocation invocation)throws Exception{
String result = invocation.invoke();
ActionContext context = (ActionContext) invocation.getInvocationContext();
HttpServletRequest request = (HttpServletRequest) context.get(StrutsStatics.HTTP_REQUEST);
HttpServletResponse response=(HttpServletResponse) context.get(StrutsStatics.HTTP_RESPONSE);
response.setHeader("Cache-Control", "no-store");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
return result;
}
public void destroy() {}
public void init() {}
}
struts.xml中
<struts>
<constant name="struts.devMode" value="true"/>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="caching" class="com.struts.device.interceptor.ClearCacheInterceptor"/>
<interceptor-stack name="cachingStack">
<interceptor-ref name="caching" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<action name="Login" class="struts.device.example.LogIn">
<interceptor-ref name="cachingStack"/>
<result>example/Add.jsp</result>
<result name="error">example/Login.jsp</result>
</action>
</package>
</struts>
申请工作正常;它执行拦截器,但不会阻止缓存。
答案 0 :(得分:1)
我已经解决了我的问题。感谢开发人员帮助我追踪的工具。
我的代码中的轻微序列更改帮助了我:根据the Struts 2 interceptor docs,结果在 invocation.invoke()
返回之前呈现。在将结果呈现回客户端之前设置标头会在返回的结果中设置标头。
即,
public String intercept(ActionInvocation invocation)throws Exception{
HttpServletResponse response = ServletActionContext.getResponse();
response.setHeader("Cache-Control", "no-store");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
return invocation.invoke();
}