Class WebContentGenerator
- All Implemented Interfaces:
- Aware,- ApplicationContextAware,- ServletContextAware
- Direct Known Subclasses:
- AbstractController,- AbstractHandlerMethodAdapter,- ResourceHttpRequestHandler,- WebContentInterceptor
AbstractController
 and WebContentInterceptor.
 Can also be used for custom handlers that have their own
 HandlerAdapter.
 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 methods setUseExpiresHeader(boolean), setUseCacheControlHeader(boolean),
 setUseCacheControlNoStore(boolean) or setAlwaysMustRevalidate(boolean).
- Author:
- Rod Johnson, Juergen Hoeller, Brian Clozel, Rossen Stoyanchev
- See Also:
- 
Field SummaryFieldsModifier and TypeFieldDescriptionprotected static final Stringstatic final StringHTTP method "GET".static final StringHTTP method "HEAD".static final StringHTTP method "POST".Fields inherited from class org.springframework.context.support.ApplicationObjectSupportlogger
- 
Constructor SummaryConstructorsConstructorDescriptionCreate 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.
- 
Method SummaryModifier and TypeMethodDescriptionprotected final voidapplyCacheControl(HttpServletResponse response, CacheControl cacheControl) Set the HTTP Cache-Control header according to the given settings.protected final voidapplyCacheSeconds(HttpServletResponse response, int cacheSeconds) Apply the given cache seconds and generate corresponding HTTP headers, i.e.protected final voidapplyCacheSeconds(HttpServletResponse response, int cacheSeconds, boolean mustRevalidate) Deprecated.protected final voidcacheForSeconds(HttpServletResponse response, int seconds) Deprecated.protected final voidcacheForSeconds(HttpServletResponse response, int seconds, boolean mustRevalidate) Deprecated.protected final voidcheckAndPrepare(HttpServletRequest request, HttpServletResponse response, int cacheSeconds, boolean lastModified) Deprecated.as of 4.2, since thelastModifiedflag is effectively ignored, with a must-revalidate header only generated if explicitly configuredprotected final voidcheckRequest(HttpServletRequest request) Check the given request for supported methods and a required session, if any.protected StringReturn the "Allow" header value to use in response to an HTTP OPTIONS request based on the configuredsupported methodsalso automatically adding "OPTIONS" to the list even if not present as a supported method.final CacheControlGet theCacheControlinstance that builds the Cache-Control HTTP response header.final intReturn the number of seconds that content is cached.final String[]Return the HTTP methods that this content generator supports.final String[]Return the configured request header names for the "Vary" response header.final booleanDeprecated.as of 4.2, in favor ofgetCacheControl()final booleanReturn whether a session is required to handle requests.final booleanDeprecated.as of 4.2, in favor ofgetCacheControl()final booleanDeprecated.as of 4.2, in favor ofgetCacheControl()final booleanDeprecated.as of 4.2, in favor ofgetCacheControl()protected final voidprepareResponse(HttpServletResponse response) Prepare the given response according to the settings of this generator.protected final voidpreventCaching(HttpServletResponse response) Deprecated.final voidsetAlwaysMustRevalidate(boolean mustRevalidate) Deprecated.as of 4.2, in favor ofsetCacheControl(org.springframework.http.CacheControl)final voidsetCacheControl(CacheControl cacheControl) Set theCacheControlinstance to build the Cache-Control HTTP response header.final voidsetCacheSeconds(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 contentfinal voidsetRequireSession(boolean requireSession) Set whether a session should be required to handle requests.final voidsetSupportedMethods(String... methods) Set the HTTP methods that this content generator should support.final voidsetUseCacheControlHeader(boolean useCacheControlHeader) Deprecated.as of 4.2, since going forward, the HTTP 1.1 cache-control header will be required, with the HTTP 1.0 headers disappearingfinal voidsetUseCacheControlNoStore(boolean useCacheControlNoStore) Deprecated.as of 4.2, in favor ofsetCacheControl(org.springframework.http.CacheControl)final voidsetUseExpiresHeader(boolean useExpiresHeader) Deprecated.as of 4.2, since going forward, the HTTP 1.1 cache-control header will be required, with the HTTP 1.0 headers disappearingfinal voidsetVaryByRequestHeaders(String... varyByRequestHeaders) Configure one or more request header names (e.g.Methods inherited from class org.springframework.web.context.support.WebApplicationObjectSupportgetServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContextMethods inherited from class org.springframework.context.support.ApplicationObjectSupportgetApplicationContext, getMessageSourceAccessor, initApplicationContext, obtainApplicationContext, requiredContextClass, setApplicationContext
- 
Field Details- 
METHOD_GETHTTP method "GET".- See Also:
 
- 
METHOD_HEADHTTP method "HEAD".- See Also:
 
- 
METHOD_POSTHTTP method "POST".- See Also:
 
- 
HEADER_CACHE_CONTROL- See Also:
 
 
- 
- 
Constructor Details- 
WebContentGeneratorpublic WebContentGenerator()Create a new WebContentGenerator which supports HTTP methods GET, HEAD and POST by default.
- 
WebContentGeneratorpublic WebContentGenerator(boolean restrictDefaultSupportedMethods) Create a new WebContentGenerator.- Parameters:
- restrictDefaultSupportedMethods-- trueif this generator should support HTTP methods GET, HEAD and POST by default, or- falseif it should be unrestricted
 
- 
WebContentGeneratorCreate a new WebContentGenerator.- Parameters:
- supportedMethods- the supported HTTP methods for this content generator
 
 
- 
- 
Method Details- 
setSupportedMethodsSet 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. 
- 
getSupportedMethodsReturn the HTTP methods that this content generator supports.
- 
getAllowHeaderReturn the "Allow" header value to use in response to an HTTP OPTIONS request based on the configuredsupported methodsalso 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).- Since:
- 4.3
 
- 
setRequireSessionpublic final void setRequireSession(boolean requireSession) Set whether a session should be required to handle requests.
- 
isRequireSessionpublic final boolean isRequireSession()Return whether a session is required to handle requests.
- 
setCacheControlSet theCacheControlinstance to build the Cache-Control HTTP response header.- Since:
- 4.2
 
- 
getCacheControlGet theCacheControlinstance that builds the Cache-Control HTTP response header.- Since:
- 4.2
 
- 
setCacheSecondspublic 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 CacheControlshould be used.
- 
getCacheSecondspublic final int getCacheSeconds()Return the number of seconds that content is cached.
- 
setVaryByRequestHeadersConfigure 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.- Parameters:
- varyByRequestHeaders- one or more request header names
- Since:
- 4.3
 
- 
getVaryByRequestHeadersReturn the configured request header names for the "Vary" response header.- Since:
- 4.3
 
- 
setUseExpiresHeaderDeprecated.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. 
- 
isUseExpiresHeaderDeprecated.as of 4.2, in favor ofgetCacheControl()Return whether the HTTP 1.0 expires header is used.
- 
setUseCacheControlHeaderDeprecated.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. 
- 
isUseCacheControlHeaderDeprecated.as of 4.2, in favor ofgetCacheControl()Return whether the HTTP 1.1 cache-control header is used.
- 
setUseCacheControlNoStoreDeprecated.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".
- 
isUseCacheControlNoStoreDeprecated.as of 4.2, in favor ofgetCacheControl()Return whether the HTTP 1.1 cache-control header value "no-store" is used.
- 
setAlwaysMustRevalidateDeprecated.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". 
- 
isAlwaysMustRevalidateDeprecated.as of 4.2, in favor ofgetCacheControl()Return whether 'must-revalidate' is added to every Cache-Control header.
- 
checkRequestCheck the given request for supported methods and a required session, if any.- Parameters:
- request- current HTTP request
- Throws:
- ServletException- if the request cannot be handled because a check failed
- Since:
- 4.2
 
- 
prepareResponsePrepare the given response according to the settings of this generator. Applies the number of cache seconds specified for this generator.- Parameters:
- response- current HTTP response
- Since:
- 4.2
 
- 
applyCacheControlSet the HTTP Cache-Control header according to the given settings.- Parameters:
- response- current HTTP response
- cacheControl- the pre-configured cache control settings
- Since:
- 4.2
 
- 
applyCacheSecondsApply 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.- Parameters:
- response- current HTTP response
- cacheSeconds- 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, int cacheSeconds, boolean lastModified) throws ServletException Deprecated.as of 4.2, since thelastModifiedflag is effectively ignored, with a must-revalidate header only generated if explicitly configuredCheck and prepare the given request and response according to the settings of this generator.
- 
applyCacheSeconds@Deprecated protected final void applyCacheSeconds(HttpServletResponse response, int cacheSeconds, boolean mustRevalidate) Deprecated.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). - Parameters:
- response- the current HTTP response
- cacheSeconds- 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)
 
- 
cacheForSecondsDeprecated.Set HTTP headers to allow caching for the given number of seconds. Does not tell the browser to revalidate the resource.- Parameters:
- response- current HTTP response
- seconds- 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) Deprecated.Set HTTP headers to allow caching for the given number of seconds. Tells the browser to revalidate the resource if mustRevalidate istrue.- Parameters:
- response- the current HTTP response
- seconds- number of seconds into the future that the response should be cacheable for
- mustRevalidate- whether the client should revalidate the resource (typically only necessary for controllers with last-modified support)
 
- 
preventCachingDeprecated.Prevent the response from being cached. Only called in HTTP 1.0 compatibility mode.See https://www.mnot.net/cache_docs.
 
- 
applyCacheControl(jakarta.servlet.http.HttpServletResponse, org.springframework.http.CacheControl)