org.springframework.web.servlet.mvc.AbstractController
and org.springframework.web.servlet.mvc.WebContentInterceptor .
Can also be used for custom handlers that have their own
org.springframework.web.servlet.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, 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, setUseCacheControlHeader,
setUseCacheControlNoStore or setAlwaysMustRevalidate.
setCacheSeconds, setCacheControl, setRequireSession
| Modifier and Type | Field and Description |
|---|---|
| private String | |
| private boolean | |
| private CacheControl | |
| private int | |
| protected static final String | |
| private static final String | |
| private static final String | |
| public static final String | METHOD_GET
HTTP method "GET". |
| public static final String | METHOD_HEAD
HTTP method "HEAD". |
| public static final String | METHOD_POST
HTTP method "POST". |
| private boolean | |
| private Set | supportedMethods
Set of supported HTTP methods. |
| private boolean | useCacheControlHeader
Use HTTP 1.1 cache-control header? |
| private boolean | useCacheControlNoStore
Use HTTP 1.1 cache-control header value "no-store"? |
| private boolean | useExpiresHeader
Use HTTP 1.0 expires header? |
| private String[] |
| Access | Constructor and Description |
|---|---|
| public | WebContentGenerator()
Create a new WebContentGenerator which supports HTTP methods GET, HEAD and POST by default. |
| public | WebContentGenerator(boolean
true if this
generator should support HTTP methods GET, HEAD and POST by default,
or false if it should be unrestrictedCreate a new WebContentGenerator. |
| public | WebContentGenerator(String...
the supported HTTP methods for this content generator supportedMethods)Create a new WebContentGenerator. |
| Modifier and Type | Method and Description |
|---|---|
| protected final void | applyCacheControl(HttpServletResponse
current HTTP response response, CacheControl the pre-configured cache control settings cacheControl)Set the HTTP Cache-Control header according to the given settings. |
| protected final void | applyCacheSeconds(HttpServletResponse
current HTTP response response, int positive number of seconds into the future that the
response should be cacheable for, 0 to prevent caching 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 final void | applyCacheSeconds(HttpServletResponse
the current HTTP response response, int 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. cacheSeconds, boolean whether the client should revalidate the resource
(typically only necessary for controllers with last-modified support) mustRevalidate)
Deprecated
as of 4.2, in favor of
applyCacheControl
Apply the given cache seconds and generate respective HTTP headers. |
| protected final void | cacheForSeconds(HttpServletResponse
current HTTP response response, int number of seconds into the future that the response
should be cacheable for seconds)
Deprecated
as of 4.2, in favor of
applyCacheControl
Set HTTP headers to allow caching for the given number of seconds. |
| protected final void | cacheForSeconds(HttpServletResponse
the current HTTP response response, int number of seconds into the future that the response
should be cacheable for seconds, boolean whether the client should revalidate the resource
(typically only necessary for controllers with last-modified support) mustRevalidate)
Deprecated
as of 4.2, in favor of
applyCacheControl
Set HTTP headers to allow caching for the given number of seconds. |
| protected final void | checkAndPrepare(HttpServletRequest request, HttpServletResponse response, int cacheSeconds, boolean lastModified)
Deprecated
as of 4.2, since the
lastModified flag is effectively ignored,
with a must-revalidate header only generated if explicitly configured
Check and prepare the given request and response according to the settings of this generator. |
| protected final void | checkRequest(HttpServletRequest
current HTTP request 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 configured |
| public final CacheControl | getCacheControl()
Get the |
| public final int | |
| public final String[] | |
| public final String[] | |
| private Collection | |
| private void | |
| public final boolean | isAlwaysMustRevalidate()
Deprecated
as of 4.2, in favor of
getCacheControl()
Return whether 'must-revalidate' is added to every Cache-Control header. |
| public final boolean | |
| public final boolean | isUseCacheControlHeader()
Deprecated
as of 4.2, in favor of
getCacheControl()
Return whether the HTTP 1.1 cache-control header is used. |
| public final boolean | isUseCacheControlNoStore()
Deprecated
as of 4.2, in favor of
getCacheControl()
Return whether the HTTP 1.1 cache-control header value "no-store" is used. |
| public final boolean | isUseExpiresHeader()
Deprecated
as of 4.2, in favor of
getCacheControl()
Return whether the HTTP 1.0 expires header is used. |
| protected final void | prepareResponse(HttpServletResponse
current HTTP response response)Prepare the given response according to the settings of this generator. |
| protected final void | preventCaching(HttpServletResponse response)
Deprecated
as of 4.2, in favor of
applyCacheControl
Prevent the response from being cached. |
| public final void | setAlwaysMustRevalidate(boolean mustRevalidate)
Deprecated
as of 4.2, in favor of
setCacheControl
An option to add 'must-revalidate' to every Cache-Control header. |
| public final void | setCacheControl(CacheControl cacheControl)
Set the |
| public final void | setCacheSeconds(int seconds)
Cache content for the given number of seconds, by writing cache-related HTTP headers to the response:
|
| public final void | setRequireSession(boolean requireSession)
Set whether a session should be required to handle requests. |
| public final void | setSupportedMethods(String... methods)
Set the HTTP methods that this content generator should support. |
| public final void | setUseCacheControlHeader(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 disappearing
Set whether to use the HTTP 1.1 cache-control header. |
| public final void | setUseCacheControlNoStore(boolean useCacheControlNoStore)
Deprecated
as of 4.2, in favor of
setCacheControl
Set whether to use the HTTP 1.1 cache-control header value "no-store" when preventing caching. |
| public final void | setUseExpiresHeader(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 disappearing
Set whether to use the HTTP 1.0 expires header. |
| public final void | setVaryByRequestHeaders(String...
one or more request header names 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. |
| allowHeader | back to summary |
|---|---|
| private String allowHeader
| |
| alwaysMustRevalidate | back to summary |
|---|---|
| private boolean alwaysMustRevalidate | |
| cacheControl | back to summary |
|---|---|
| private CacheControl cacheControl
| |
| cacheSeconds | back to summary |
|---|---|
| private int cacheSeconds | |
| HEADER_CACHE_CONTROL | back to summary |
|---|---|
| protected static final String HEADER_CACHE_CONTROL | |
| HEADER_EXPIRES | back to summary |
|---|---|
| private static final String HEADER_EXPIRES | |
| HEADER_PRAGMA | back to summary |
|---|---|
| private static final String HEADER_PRAGMA | |
| METHOD_GET | back to summary |
|---|---|
| public static final String METHOD_GET HTTP method "GET". | |
| METHOD_HEAD | back to summary |
|---|---|
| public static final String METHOD_HEAD HTTP method "HEAD". | |
| METHOD_POST | back to summary |
|---|---|
| public static final String METHOD_POST HTTP method "POST". | |
| requireSession | back to summary |
|---|---|
| private boolean requireSession | |
| supportedMethods | back to summary |
|---|---|
| private Set<String> supportedMethods Set of supported HTTP methods. | |
| useCacheControlHeader | back to summary |
|---|---|
| private boolean useCacheControlHeader Use HTTP 1.1 cache-control header? | |
| useCacheControlNoStore | back to summary |
|---|---|
| private boolean useCacheControlNoStore Use HTTP 1.1 cache-control header value "no-store"? | |
| useExpiresHeader | back to summary |
|---|---|
| private boolean useExpiresHeader Use HTTP 1.0 expires header? | |
| varyByRequestHeaders | back to summary |
|---|---|
| private String[] varyByRequestHeaders
| |
| WebContentGenerator | back to summary |
|---|---|
| public WebContentGenerator() Create a new WebContentGenerator which supports HTTP methods GET, HEAD and POST by default. | |
| WebContentGenerator | back to summary |
|---|---|
| public WebContentGenerator(boolean restrictDefaultSupportedMethods) Create a new WebContentGenerator.
| |
| WebContentGenerator | back to summary |
|---|---|
| public WebContentGenerator(String... supportedMethods) Create a new WebContentGenerator.
| |
| applyCacheControl | back to summary |
|---|---|
| protected final void applyCacheControl(HttpServletResponse response, CacheControl cacheControl) Set the HTTP Cache-Control header according to the given settings.
| |
| applyCacheSeconds | back to summary |
|---|---|
| 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.
| |
| applyCacheSeconds | back to summary |
|---|---|
| protected final void applyCacheSeconds(HttpServletResponse response, int cacheSeconds, boolean mustRevalidate)
Deprecated as of 4.2, in favor of 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).
| |
| cacheForSeconds | back to summary |
|---|---|
| protected final void cacheForSeconds(HttpServletResponse response, int seconds)
Deprecated as of 4.2, in favor of Set HTTP headers to allow caching for the given number of seconds. Does not tell the browser to revalidate the resource.
| |
| cacheForSeconds | back to summary |
|---|---|
| protected final void cacheForSeconds(HttpServletResponse response, int seconds, boolean mustRevalidate)
Deprecated as of 4.2, in favor of Set HTTP headers to allow caching for the given number of seconds.
Tells the browser to revalidate the resource if mustRevalidate is
| |
| checkAndPrepare | back to summary |
|---|---|
| protected final void checkAndPrepare(HttpServletRequest request, HttpServletResponse response, int cacheSeconds, boolean lastModified) throws ServletException
Deprecated as of 4.2, since the Check and prepare the given request and response according to the settings of this generator.
| |
| checkRequest | back to summary |
|---|---|
| protected final void checkRequest(HttpServletRequest request) throws ServletException Check the given request for supported methods and a required session, if any.
| |
| getAllowHeader | back to summary |
|---|---|
| protected String getAllowHeader() Return the "Allow" header value to use in response to an HTTP OPTIONS request
based on the configured
| |
| getCacheControl | back to summary |
|---|---|
| public final CacheControl getCacheControl() Get the
| |
| getCacheSeconds | back to summary |
|---|---|
| public final int getCacheSeconds() Return the number of seconds that content is cached. | |
| getSupportedMethods | back to summary |
|---|---|
| public final String[] getSupportedMethods() Return the HTTP methods that this content generator supports.
| |
| getVaryByRequestHeaders | back to summary |
|---|---|
| public final String[] getVaryByRequestHeaders() Return the configured request header names for the "Vary" response header.
| |
| getVaryRequestHeadersToAdd | back to summary |
|---|---|
| private Collection | |
| initAllowHeader | back to summary |
|---|---|
| private void initAllowHeader() | |
| isAlwaysMustRevalidate | back to summary |
|---|---|
| public final boolean isAlwaysMustRevalidate()
Deprecated as of 4.2, in favor of Return whether 'must-revalidate' is added to every Cache-Control header.
| |
| isRequireSession | back to summary |
|---|---|
| public final boolean isRequireSession() Return whether a session is required to handle requests. | |
| isUseCacheControlHeader | back to summary |
|---|---|
| public final boolean isUseCacheControlHeader()
Deprecated as of 4.2, in favor of Return whether the HTTP 1.1 cache-control header is used.
| |
| isUseCacheControlNoStore | back to summary |
|---|---|
| public final boolean isUseCacheControlNoStore()
Deprecated as of 4.2, in favor of Return whether the HTTP 1.1 cache-control header value "no-store" is used.
| |
| isUseExpiresHeader | back to summary |
|---|---|
| public final boolean isUseExpiresHeader()
Deprecated as of 4.2, in favor of Return whether the HTTP 1.0 expires header is used.
| |
| prepareResponse | back to summary |
|---|---|
| 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.
| |
| preventCaching | back to summary |
|---|---|
| protected final void preventCaching(HttpServletResponse response)
Deprecated as of 4.2, in favor of Prevent the response from being cached. Only called in HTTP 1.0 compatibility mode. See
| |
| setAlwaysMustRevalidate | back to summary |
|---|---|
| public final void setAlwaysMustRevalidate(boolean mustRevalidate)
Deprecated as of 4.2, in favor of 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 in
Default is "false".
| |
| setCacheControl | back to summary |
|---|---|
| public final void setCacheControl(CacheControl cacheControl) Set the
| |
| setCacheSeconds | back to summary |
|---|---|
| public final void setCacheSeconds(int seconds) Cache content for the given number of seconds, by writing cache-related HTTP headers to the response:
For more specific needs, a custom
| |
| setRequireSession | back to summary |
|---|---|
| public final void setRequireSession(boolean requireSession) Set whether a session should be required to handle requests. | |
| setSupportedMethods | back to summary |
|---|---|
| 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. | |
| setUseCacheControlHeader | back to summary |
|---|---|
| public final void setUseCacheControlHeader(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 disappearing Set 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.
| |
| setUseCacheControlNoStore | back to summary |
|---|---|
| public final void setUseCacheControlNoStore(boolean useCacheControlNoStore)
Deprecated as of 4.2, in favor of Set whether to use the HTTP 1.1 cache-control header value "no-store" when preventing caching. Default is "true".
| |
| setUseExpiresHeader | back to summary |
|---|---|
| public final void setUseExpiresHeader(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 disappearing Set 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.
| |
| setVaryByRequestHeaders | back to summary |
|---|---|
| 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.
| |