你如何在 Spring MVC 中设置缓存头? [英] How do you set cache headers in Spring MVC?

查看:40
本文介绍了你如何在 Spring MVC 中设置缓存头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在基于注解的 Spring MVC 控制器中,为特定路径设置缓存头的首选方法是什么?

In an annotation-based Spring MVC controller, what is the preferred way to set cache headers for a specific path?

推荐答案

org.springframework.web.servlet.support.WebContentGenerator,它是所有 Spring 控制器的基类,有很多处理缓存头的方法:

org.springframework.web.servlet.support.WebContentGenerator, which is the base class for all Spring controllers has quite a few methods dealing with cache headers:

/* 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);

它们既可以在内容生成之前在您的控制器中调用,也可以在 Spring 上下文中指定为 bean 属性.

They can either be invoked within your controller prior to content generation or specified as bean properties in Spring context.

这篇关于你如何在 Spring MVC 中设置缓存头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆