1
我写了一个拦截器来防止缓存但页面仍然缓存。清除缓存拦截器struts2不工作
拦截:
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>
应用正常工作;它执行拦截器,但不防止缓存。
通过“它不清除缓存”你的意思是“它仍然缓存”? – 2012-08-17 15:57:00
yes ..... @ DaveNewton – Srishti123 2012-08-17 16:40:52
使用Firebug或Chrome的开发人员工具查看响应,看看是否正在设置HTTP标头(Cache-Control,Pragma和Expires)。 – 2012-08-17 18:03:47