【发布时间】:2012-08-17 15:52:03
【问题描述】:
我写了一个拦截器来防止缓存,但是页面仍然缓存。
拦截器:
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>
应用程序运行良好;它执行拦截器,但不会阻止缓存。
【问题讨论】:
-
“它没有清除缓存”是指“它仍在缓存中”吗?
-
使用 Firebug 或 Chrome 的开发者工具查看响应以查看您的 HTTP 标头(Cache-Control、Pragma 和 Expires)是否正在设置。
-
@StevenBenitez :我使用 firefox 开发工具查看实时 HTTp 标头信息,但这些标头(Cache_Control、Pragma 和 Expires)未设置。我在代码中犯了什么错误。???
-
不知道。你为什么不做一些调试?您至少现在知道没有设置标题,这可能是浏览器继续缓存页面的原因。您的拦截器中有几个 System.out.println 行。这些输出线是否进入控制台?尝试设置断点并单步执行代码。
-
@StevenBenitez:非常感谢......我所做的一切......是的 System.out.println 行正在进入控制台。现在我可以设置缓存参数.我发现了我的错误(请参阅此处的答案),但即使在缓存之后,在后退按钮上它仍然可以获取上一页的内容。我不希望在我的应用程序中。 struts2中是否有任何选项或需要留下一个选项--禁用浏览器的后退按钮。
标签: struts2