/* Set whether to use the HTTP 1.1 cache-control header. Default is "true".
* <p>Note: Cache headers will only get applied if caching is enabled
* (or explicitly prevented) for the current request. */
public final void setUseCacheControlHeader();
/* Return whether the HTTP 1.1 cache-control header is used. */
public final boolean isUseCacheControlHeader();
/* Set whether to use the HTTP 1.1 cache-control header value "no-store"
* when preventing caching. Default is "true". */
public final void setUseCacheControlNoStore(boolean useCacheControlNoStore);
/* Cache content for the given number of seconds. Default is -1,
* indicating no generation of cache-related headers.
* Only if this is set to 0 (no cache) or a positive value (cache for
* this many seconds) will this class generate cache headers.
* The headers can be overwritten by subclasses, before content is generated. */
public final void setCacheSeconds(int seconds);
@EnableWebMvc
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
/* Time, in seconds, to have the browser cache static resources (one week). */
private static final int BROWSER_CACHE_CONTROL = 604800;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/images/**")
.addResourceLocations("/images/")
.setCachePeriod(BROWSER_CACHE_CONTROL);
}
}
# 1 楼答案
org.springframework.web.servlet.support.WebContentGenerator是所有Spring控制器的基类,它有许多处理缓存头的方法:
它们可以在生成内容之前在控制器内调用,也可以在Spring上下文中指定为bean属性
# 2 楼答案
您可以使用处理程序拦截器并使用它提供的postHandle方法:
http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/web/servlet/HandlerInterceptor.html
然后只需在方法中添加一个标题,如下所示:
# 3 楼答案
您可以为此定义一个地址:
@CacheControl(isPublic = true, maxAge = 300, sMaxAge = 300)
,然后使用Spring MVC拦截器将此地址呈现到HTTP头。或者做动态的:可在此处找到含义:优雅的Builder模式
顺便说一句:我刚刚发现Spring MVC内置了对缓存控制的支持: Google WebContent Interceptor或CacheControlHandlerInterceptor或CacheControl,您将找到它
# 4 楼答案
答案很简单:
^{pr1}$ 上面的代码正好显示了您想要实现的目标。你必须做两件事。添加“final HttpServletResponse”作为参数。然后将标头缓存控制设置为无缓存。# 5 楼答案
我只是遇到了同样的问题,并且找到了框架已经提供的一个很好的解决方案。
org.springframework.web.servlet.mvc.WebContentInterceptor
类允许您定义默认缓存行为,以及特定于路径的重写(与其他地方使用的路径匹配器行为相同)。我的步骤是:org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
实例未设置“cacheSeconds”属性李>添加
WebContentInterceptor
的实例:在这些更改之后,/foo下的响应包含阻止缓存的标题,/cache/me下的响应包含鼓励缓存的标题,/cache/agnostic下的响应不包含与缓存相关的标题
如果使用纯Java配置:
另见:http://docs.spring.io/spring-security/site/docs/current/reference/html/headers.html
# 6 楼答案
从Spring 4.2开始,您可以执行以下操作:
CacheControl
对象是一个具有许多配置选项的构建器,请参见JavaDoc