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

查看:505
本文介绍了如何在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天全站免登陆