类 WebContentGenerator
- java.lang.Object
- org.springframework.context.support.ApplicationObjectSupport
- org.springframework.web.context.support.WebApplicationObjectSupport
- org.springframework.web.servlet.support.WebContentGenerator
- 所有已实现的接口:
Aware
,ApplicationContextAware
,ServletContextAware
- 直接已知子类:
AbstractController
,AbstractHandlerMethodAdapter
,AnnotationMethodHandlerAdapter
,ResourceHttpRequestHandler
,WebContentInterceptor
public abstract class WebContentGenerator extends WebApplicationObjectSupport
Convenient superclass for any kind of web content generator, likeAbstractController
andWebContentInterceptor
. Can also be used for custom handlers that have their ownHandlerAdapter
.Supports HTTP cache control options. The usage of corresponding HTTP headers can be controlled via the
"cacheSeconds"
and"cacheControl"
properties.NOTE: As of Spring 4.2, this generator's default behavior changed when using only
setCacheSeconds(int)
, sending HTTP response headers that are in line with current browsers and proxies implementations (i.e. no HTTP 1.0 headers anymore) Reverting to the previous behavior can be easily done by using one of the newly deprecated methodssetUseExpiresHeader(boolean)
,setUseCacheControlHeader(boolean)
,setUseCacheControlNoStore(boolean)
orsetAlwaysMustRevalidate(boolean)
.- 作者:
- Rod Johnson, Juergen Hoeller, Brian Clozel, Rossen Stoyanchev
- 另请参阅:
setCacheSeconds(int)
,setCacheControl(org.springframework.http.CacheControl)
,setRequireSession(boolean)
字段概要
字段 修饰符和类型 字段 说明 protected static String
HEADER_CACHE_CONTROL
static String
METHOD_GET
HTTP method "GET"static String
METHOD_HEAD
HTTP method "HEAD"static String
METHOD_POST
HTTP method "POST"从类继承的字段 org.springframework.context.support.ApplicationObjectSupport
logger
构造器概要
构造器 构造器 说明 WebContentGenerator()
Create a new WebContentGenerator which supports HTTP methods GET, HEAD and POST by default.WebContentGenerator(boolean restrictDefaultSupportedMethods)
Create a new WebContentGenerator.WebContentGenerator(String... supportedMethods)
Create a new WebContentGenerator.
方法概要
所有方法 实例方法 具体方法 已过时的方法 修饰符和类型 方法 说明 protected void
applyCacheControl(HttpServletResponse response, CacheControl cacheControl)
Set the HTTP Cache-Control header according to the given settings.protected void
applyCacheSeconds(HttpServletResponse response, int cacheSeconds)
Apply the given cache seconds and generate corresponding HTTP headers, i.e. allow caching for the given number of seconds in case of a positive value, prevent caching if given a 0 value, do nothing else.protected void
applyCacheSeconds(HttpServletResponse response, int cacheSeconds, boolean mustRevalidate)
已过时。protected void
cacheForSeconds(HttpServletResponse response, int seconds)
已过时。protected void
cacheForSeconds(HttpServletResponse response, int seconds, boolean mustRevalidate)
已过时。protected void
checkAndPrepare(HttpServletRequest request, HttpServletResponse response, boolean lastModified)
已过时。as of 4.2, since thelastModified
flag is effectively ignored, with a must-revalidate header only generated if explicitly configuredprotected void
checkAndPrepare(HttpServletRequest request, HttpServletResponse response, int cacheSeconds, boolean lastModified)
已过时。as of 4.2, since thelastModified
flag is effectively ignored, with a must-revalidate header only generated if explicitly configuredprotected void
checkRequest(HttpServletRequest request)
Check the given request for supported methods and a required session, if any.protected String
getAllowHeader()
Return the "Allow" header value to use in response to an HTTP OPTIONS request based on the configuredsupported methods
also automatically adding "OPTIONS" to the list even if not present as a supported method.CacheControl
getCacheControl()
Get theCacheControl
instance that builds the Cache-Control HTTP response header.int
getCacheSeconds()
Return the number of seconds that content is cached.String[]
getSupportedMethods()
Return the HTTP methods that this content generator supports.String[]
getVaryByRequestHeaders()
Return the configured request header names for the "Vary" response header.boolean
isAlwaysMustRevalidate()
已过时。as of 4.2, in favor ofgetCacheControl()
boolean
isRequireSession()
Return whether a session is required to handle requests.boolean
isUseCacheControlHeader()
已过时。as of 4.2, in favor ofgetCacheControl()
boolean
isUseCacheControlNoStore()
已过时。as of 4.2, in favor ofgetCacheControl()
boolean
isUseExpiresHeader()
已过时。as of 4.2, in favor ofgetCacheControl()
protected void
prepareResponse(HttpServletResponse response)
Prepare the given response according to the settings of this generator.protected void
preventCaching(HttpServletResponse response)
已过时。void
setAlwaysMustRevalidate(boolean mustRevalidate)
已过时。as of 4.2, in favor ofsetCacheControl(org.springframework.http.CacheControl)
void
setCacheControl(CacheControl cacheControl)
Set theCacheControl
instance to build the Cache-Control HTTP response header.void
setCacheSeconds(int seconds)
Cache content for the given number of seconds, by writing cache-related HTTP headers to the response: seconds== -1 (default value): no generation cache-related headers seconds== 0: "Cache-Control: no-store" will prevent caching seconds > 0: "Cache-Control: max-age=seconds" will ask to cache contentvoid
setRequireSession(boolean requireSession)
Set whether a session should be required to handle requests.void
setSupportedMethods(String... methods)
Set the HTTP methods that this content generator should support.void
setUseCacheControlHeader(boolean useCacheControlHeader)
已过时。as of 4.2, since going forward, the HTTP 1.1 cache-control header will be required, with the HTTP 1.0 headers disappearingvoid
setUseCacheControlNoStore(boolean useCacheControlNoStore)
已过时。as of 4.2, in favor ofsetCacheControl(org.springframework.http.CacheControl)
void
setUseExpiresHeader(boolean useExpiresHeader)
已过时。as of 4.2, since going forward, the HTTP 1.1 cache-control header will be required, with the HTTP 1.0 headers disappearingvoid
setVaryByRequestHeaders(String... varyByRequestHeaders)
Configure one or more request header names (e.g.从类继承的方法 org.springframework.web.context.support.WebApplicationObjectSupport
getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContext
从类继承的方法 org.springframework.context.support.ApplicationObjectSupport
getApplicationContext, getMessageSourceAccessor, initApplicationContext, requiredContextClass, setApplicationContext
字段详细资料
METHOD_GET
public static final String METHOD_GET
HTTP method "GET"- 另请参阅:
- 常量字段值
METHOD_HEAD
public static final String METHOD_HEAD
HTTP method "HEAD"- 另请参阅:
- 常量字段值
METHOD_POST
public static final String METHOD_POST
HTTP method "POST"- 另请参阅:
- 常量字段值
HEADER_CACHE_CONTROL
protected static final String HEADER_CACHE_CONTROL
- 另请参阅:
- 常量字段值
构造器详细资料
WebContentGenerator
public WebContentGenerator()
Create a new WebContentGenerator which supports HTTP methods GET, HEAD and POST by default.
WebContentGenerator
public WebContentGenerator(boolean restrictDefaultSupportedMethods)
Create a new WebContentGenerator.- 参数:
restrictDefaultSupportedMethods
-true
if this generator should support HTTP methods GET, HEAD and POST by default, orfalse
if it should be unrestricted
WebContentGenerator
public WebContentGenerator(String... supportedMethods)
Create a new WebContentGenerator.- 参数:
supportedMethods
- the supported HTTP methods for this content generator
方法详细资料
setSupportedMethods
public final void setSupportedMethods(String... methods)
Set the HTTP methods that this content generator should support.Default is GET, HEAD and POST for simple form controller types; unrestricted for general controllers and interceptors.
getSupportedMethods
public final String[] getSupportedMethods()
Return the HTTP methods that this content generator supports.
getAllowHeader
protected String getAllowHeader()
Return the "Allow" header value to use in response to an HTTP OPTIONS request based on the configuredsupported methods
also automatically adding "OPTIONS" to the list even if not present as a supported method. This means subclasses don't have to explicitly list "OPTIONS" as a supported method as long as HTTP OPTIONS requests are handled before making a call tocheckRequest(HttpServletRequest)
.- 从以下版本开始:
- 4.3
setRequireSession
public final void setRequireSession(boolean requireSession)
Set whether a session should be required to handle requests.
isRequireSession
public final boolean isRequireSession()
Return whether a session is required to handle requests.
setCacheControl
public final void setCacheControl(CacheControl cacheControl)
Set theCacheControl
instance to build the Cache-Control HTTP response header.- 从以下版本开始:
- 4.2
getCacheControl
public final CacheControl getCacheControl()
Get theCacheControl
instance that builds the Cache-Control HTTP response header.- 从以下版本开始:
- 4.2
setCacheSeconds
public final void setCacheSeconds(int seconds)
Cache content for the given number of seconds, by writing cache-related HTTP headers to the response:- seconds == -1 (default value): no generation cache-related headers
- seconds == 0: "Cache-Control: no-store" will prevent caching
- seconds > 0: "Cache-Control: max-age=seconds" will ask to cache content
For more specific needs, a custom
CacheControl
should be used.
getCacheSeconds
public final int getCacheSeconds()
Return the number of seconds that content is cached.
setVaryByRequestHeaders
public final void setVaryByRequestHeaders(String... varyByRequestHeaders)
Configure one or more request header names (e.g. "Accept-Language") to add to the "Vary" response header to inform clients that the response is subject to content negotiation and variances based on the value of the given request headers. The configured request header names are added only if not already present in the response "Vary" header.Note: This property is only supported on Servlet 3.0+ which allows checking existing response header values.
- 参数:
varyByRequestHeaders
- one or more request header names- 从以下版本开始:
- 4.3
getVaryByRequestHeaders
public final String[] getVaryByRequestHeaders()
Return the configured request header names for the "Vary" response header.- 从以下版本开始:
- 4.3
setUseExpiresHeader
@Deprecated public final void setUseExpiresHeader(boolean useExpiresHeader)
已过时。as of 4.2, since going forward, the HTTP 1.1 cache-control header will be required, with the HTTP 1.0 headers disappearingSet whether to use the HTTP 1.0 expires header. Default is "false", as of 4.2.Note: Cache headers will only get applied if caching is enabled (or explicitly prevented) for the current request.
isUseExpiresHeader
@Deprecated public final boolean isUseExpiresHeader()
已过时。as of 4.2, in favor ofgetCacheControl()
Return whether the HTTP 1.0 expires header is used.
setUseCacheControlHeader
@Deprecated public final void setUseCacheControlHeader(boolean useCacheControlHeader)
已过时。as of 4.2, since going forward, the HTTP 1.1 cache-control header will be required, with the HTTP 1.0 headers disappearingSet whether to use the HTTP 1.1 cache-control header. Default is "true".Note: Cache headers will only get applied if caching is enabled (or explicitly prevented) for the current request.
isUseCacheControlHeader
@Deprecated public final boolean isUseCacheControlHeader()
已过时。as of 4.2, in favor ofgetCacheControl()
Return whether the HTTP 1.1 cache-control header is used.
setUseCacheControlNoStore
@Deprecated public final void setUseCacheControlNoStore(boolean useCacheControlNoStore)
已过时。as of 4.2, in favor ofsetCacheControl(org.springframework.http.CacheControl)
Set whether to use the HTTP 1.1 cache-control header value "no-store" when preventing caching. Default is "true".
isUseCacheControlNoStore
@Deprecated public final boolean isUseCacheControlNoStore()
已过时。as of 4.2, in favor ofgetCacheControl()
Return whether the HTTP 1.1 cache-control header value "no-store" is used.
setAlwaysMustRevalidate
@Deprecated public final void setAlwaysMustRevalidate(boolean mustRevalidate)
已过时。as of 4.2, in favor ofsetCacheControl(org.springframework.http.CacheControl)
An option to add 'must-revalidate' to every Cache-Control header. This may be useful with annotated controller methods, which can programmatically do a last-modified calculation as described inWebRequest.checkNotModified(long)
.Default is "false".
isAlwaysMustRevalidate
@Deprecated public final boolean isAlwaysMustRevalidate()
已过时。as of 4.2, in favor ofgetCacheControl()
Return whether 'must-revalidate' is added to every Cache-Control header.
checkRequest
protected final void checkRequest(HttpServletRequest request) throws ServletException
Check the given request for supported methods and a required session, if any.- 参数:
request
- current HTTP request- 抛出:
ServletException
- if the request cannot be handled because a check failed- 从以下版本开始:
- 4.2
prepareResponse
protected final void prepareResponse(HttpServletResponse response)
Prepare the given response according to the settings of this generator. Applies the number of cache seconds specified for this generator.- 参数:
response
- current HTTP response- 从以下版本开始:
- 4.2
applyCacheControl
protected final void applyCacheControl(HttpServletResponse response, CacheControl cacheControl)
Set the HTTP Cache-Control header according to the given settings.- 参数:
response
- current HTTP responsecacheControl
- the pre-configured cache control settings- 从以下版本开始:
- 4.2
applyCacheSeconds
protected final void applyCacheSeconds(HttpServletResponse response, int cacheSeconds)
Apply the given cache seconds and generate corresponding HTTP headers, i.e. allow caching for the given number of seconds in case of a positive value, prevent caching if given a 0 value, do nothing else. Does not tell the browser to revalidate the resource.- 参数:
response
- current HTTP responsecacheSeconds
- positive number of seconds into the future that the response should be cacheable for, 0 to prevent caching
checkAndPrepare
@Deprecated protected final void checkAndPrepare(HttpServletRequest request, HttpServletResponse response, boolean lastModified) throws ServletException
已过时。as of 4.2, since thelastModified
flag is effectively ignored, with a must-revalidate header only generated if explicitly configured
checkAndPrepare
@Deprecated protected final void checkAndPrepare(HttpServletRequest request, HttpServletResponse response, int cacheSeconds, boolean lastModified) throws ServletException
已过时。as of 4.2, since thelastModified
flag is effectively ignored, with a must-revalidate header only generated if explicitly configured
applyCacheSeconds
@Deprecated protected final void applyCacheSeconds(HttpServletResponse response, int cacheSeconds, boolean mustRevalidate)
已过时。Apply the given cache seconds and generate respective HTTP headers.That is, allow caching for the given number of seconds in the case of a positive value, prevent caching if given a 0 value, else do nothing (i.e. leave caching to the client).
- 参数:
response
- the current HTTP responsecacheSeconds
- the (positive) number of seconds into the future that the response should be cacheable for; 0 to prevent caching; and a negative value to leave caching to the client.mustRevalidate
- whether the client should revalidate the resource (typically only necessary for controllers with last-modified support)
cacheForSeconds
@Deprecated protected final void cacheForSeconds(HttpServletResponse response, int seconds)
已过时。Set HTTP headers to allow caching for the given number of seconds. Does not tell the browser to revalidate the resource.- 参数:
response
- current HTTP responseseconds
- number of seconds into the future that the response should be cacheable for
cacheForSeconds
@Deprecated protected final void cacheForSeconds(HttpServletResponse response, int seconds, boolean mustRevalidate)
已过时。Set HTTP headers to allow caching for the given number of seconds. Tells the browser to revalidate the resource if mustRevalidate istrue
.- 参数:
response
- the current HTTP responseseconds
- number of seconds into the future that the response should be cacheable formustRevalidate
- whether the client should revalidate the resource (typically only necessary for controllers with last-modified support)
preventCaching
@Deprecated protected final void preventCaching(HttpServletResponse response)
已过时。Prevent the response from being cached. Only called in HTTP 1.0 compatibility mode.See
https://www.mnot.net/cache_docs
.