导致这种情况的根代码是在Spring MVC中,从WebContentGenerator调用.此类用作MVC / Webflow堆栈中几个类的基类:WebContentInterceptor(MVC拦截器),AbstractController(MVC控制器),AbstractHandlerMethodAdapter(MVC HandlerAdapter),AnnotationMethodHadlerAdapter(MVC HandlerAdapter),FlowHandlerAdapter(Webflow HandlerAdapter),JsfFlowHandlerAdapter(Webflow JSF HandlerAdapter)
CacheControl seconds设置为0会调用preventCaching方法.因此,应用程序似乎默认设置为0.
org.springframework.web.servlet.support.WebContentGenerator
protected final void preventCaching(HttpServletResponse response) {
response.setHeader(HEADER_PRAGMA, "no-cache");
if (this.useExpiresHeader) {
// HTTP 1.0 header
response.setDateHeader(HEADER_EXPIRES, 1L);
}
if (this.useCacheControlHeader) {
// HTTP 1.1 header: "no-cache" is the standard value,
// "no-store" is necessary to prevent caching on FireFox.
response.setHeader(HEADER_CACHE_CONTROL, "no-cache");
if (this.useCacheControlNoStore) {
response.addHeader(HEADER_CACHE_CONTROL, "no-store");
}
}
}
我发现自从我使用JSF Webflow以来,JsfFlowHandlerAdapter首先处理对流/视图的服务器请求.这就是为什么配置拦截器没有帮助的原因,因为JsfFlowHandlerAdapter此时已经设置了Cache-Control和其他HTTP头.事实证明我已经扩展了JsfFlowHandlerAdapter以处理FlowExecutionRestorationFailureException(参见Sping Web Flow Preventing Back Button Use),所以我需要做的就是设置我想要的配置ala WebContentInterceptor(因为配置属于基类WebContentGenerator).
自定义JsfFlowHandlerAdapter
public class MyAppFlowHandlerAdapter extends org.springframework.faces.webflow.JsfFlowHandlerAdapter {
...
}
webmvc-config.xml中
class="com.myapp.MyAppFlowHandlerAdapter">
class="com.myapp.CacheControlInterceptor">
CacheControlInterceptor(设置自己的HTTP标头.在WebContentGenerator中执行它的方法是最终的,所以不能@Override)
public class CacheControlInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//Example below: set your Cache-Control, expires, pragma headers here
response.setHeader("Cache-Control", "private");
return true;
}
}