Class WebContentGenerator
java.lang.Object
org.springframework.context.support.ApplicationObjectSupport
org.springframework.web.context.support.WebApplicationObjectSupport
org.springframework.web.servlet.support.WebContentGenerator
- All Implemented Interfaces:
- Aware, ApplicationContextAware, ServletContextAware
- Direct Known Subclasses:
- AbstractController, AbstractHandlerMethodAdapter, ResourceHttpRequestHandler, WebContentInterceptor
Convenient superclass for any kind of web content generator,
like 
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.
- 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 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 voidcheckRequest(HttpServletRequest request) Check the given request for supported methods and a required session, if any.Return 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 @Nullable CacheControlGet theCacheControlinstance that builds the Cache-Control HTTP response header.final intReturn the number of seconds that content is cached.Return the HTTP methods that this content generator supports.Return the configured request header names for the "Vary" response header.final booleanReturn whether a session is required to handle requests.protected final voidprepareResponse(HttpServletResponse response) Prepare the given response according to the settings of this generator.final voidsetCacheControl(@Nullable 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 @Nullable ... methods) Set the HTTP methods that this content generator should support.final voidsetVaryByRequestHeaders(String @Nullable ... varyByRequestHeaders) Configure one or more request header names (for example, "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.Methods inherited from class WebApplicationObjectSupportgetServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContextMethods inherited from class ApplicationObjectSupportgetApplicationContext, getMessageSourceAccessor, initApplicationContext, obtainApplicationContext, requiredContextClass, setApplicationContext
- 
Field Details- 
METHOD_GET
- 
METHOD_HEAD
- 
METHOD_POST
- 
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- 
setSupportedMethods
- 
getSupportedMethods
- 
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.- See Also:
 
- 
getCacheSecondspublic final int getCacheSeconds()Return the number of seconds that content is cached.
- 
setVaryByRequestHeadersConfigure one or more request header names (for example, "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
 
- 
getVaryByRequestHeaders
- 
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
 
 
-