ViewResolver that resolves a view based on the request file name
or Accept header.
The ContentNegotiatingViewResolver does not resolve views itself, but delegates to
other ViewResolvers. By default, these other view resolvers are picked up automatically
from the application context, though they can also be set explicitly by using the
viewResolvers property
Note
that in order for this
view resolver to work properly, the order property needs to be set to a higher
precedence than the others (the default is Ordered#HIGHEST_PRECEDENCE).
This view resolver uses the requested media type to select a suitable
View for a request. The requested media type is determined through the configured
ContentNegotiationManager. Once the requested media type has been determined, this resolver
queries each delegate view resolver for a View and determines if the requested media type
is compatible with the view's
content type). The most compatible view is returned.
Additionally, this view resolver exposes the defaultViews property,
allowing you to override the views provided by the view resolvers. Note that these default views are
offered as candidates, and still need have the content type requested (via file extension, parameter,
or Accept header, described above).
For example, if the request path is /view.html, this view resolver will look for a view
that has the text/html content type (based on the html file extension). A request
for /view with a text/html request Accept header has the same result.
ViewResolver, InternalResourceViewResolver, BeanNameViewResolver
| Modifier and Type | Field and Description |
|---|---|
| private final ContentNegotiationManagerFactoryBean | |
| private ContentNegotiationManager | |
| private List | |
| private static final View | |
| private int | |
| private boolean | |
| private List |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public void | afterPropertiesSet()
Implements org. Invoked by the containing This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set. |
| private View | getBestView(List<View> candidateViews, List<MediaType> requestedMediaTypes, RequestAttributes attrs)
|
| private List | |
| public ContentNegotiationManager | getContentNegotiationManager()
Return the |
| public List | |
| protected List | Returns: the list of media types requested, if anythe current servlet request request)Determines the list of |
| private MediaType | getMostSpecificMediaType(MediaType acceptType, MediaType produceType)
Return the more specific of the acceptable and the producible media types with the q-value of the former. |
| public int | |
| private List | |
| public List | |
| protected void | initServletContext(ServletContext
the ServletContext that this application object runs in
(never servletContext)null)Overrides org. Subclasses may override this for custom initialization based on the ServletContext that this application object runs in. The default implementation is empty. |
| public boolean | |
| public View | resolveViewName(String
name of the view to resolve viewName, Locale the Locale in which to resolve the view.
ViewResolvers that support internationalization should respect this. locale)Implements org. Resolve the given view by name. |
| public void | setContentNegotiationManager(ContentNegotiationManager contentNegotiationManager)
Set the |
| public void | setDefaultViews(List<View> defaultViews)
Set the default views to use when a more specific view can not be obtained
from the |
| public void | |
| public void | setUseNotAcceptableStatusCode(boolean useNotAcceptableStatusCode)
Indicate whether a |
| public void | setViewResolvers(List<ViewResolver> viewResolvers)
Sets the view resolvers to be wrapped by this view resolver. |
| cnmFactoryBean | back to summary |
|---|---|
| private final ContentNegotiationManagerFactoryBean cnmFactoryBean | |
| contentNegotiationManager | back to summary |
|---|---|
| private ContentNegotiationManager contentNegotiationManager
| |
| defaultViews | back to summary |
|---|---|
| private List<View> defaultViews
| |
| NOT_ACCEPTABLE_VIEW | back to summary |
|---|---|
| private static final View NOT_ACCEPTABLE_VIEW | |
| order | back to summary |
|---|---|
| private int order | |
| useNotAcceptableStatusCode | back to summary |
|---|---|
| private boolean useNotAcceptableStatusCode | |
| viewResolvers | back to summary |
|---|---|
| private List<ViewResolver> viewResolvers
| |
| ContentNegotiatingViewResolver | back to summary |
|---|---|
| public ContentNegotiatingViewResolver() | |
| afterPropertiesSet | back to summary |
|---|---|
| public void afterPropertiesSet() Implements org. Doc from org. Invoked by the containing This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.
| |
| getBestView | back to summary |
|---|---|
private View getBestView(List<View> candidateViews, List<MediaType> requestedMediaTypes, RequestAttributes attrs)
| |
| getCandidateViews | back to summary |
|---|---|
| private List | |
| getContentNegotiationManager | back to summary |
|---|---|
| public ContentNegotiationManager getContentNegotiationManager() Return the
| |
| getDefaultViews | back to summary |
|---|---|
| public List | |
| getMediaTypes | back to summary |
|---|---|
| protected List Determines the list of
| |
| getMostSpecificMediaType | back to summary |
|---|---|
| private MediaType getMostSpecificMediaType(MediaType acceptType, MediaType produceType) Return the more specific of the acceptable and the producible media types with the q-value of the former. | |
| getOrder | back to summary |
|---|---|
| public int getOrder() Implements org. Doc from org. Get the order value of this object. Higher values are interpreted as lower priority. As a consequence,
the object with the lowest value has the highest priority (somewhat
analogous to Servlet Same order values will result in arbitrary sort positions for the affected objects.
| |
| getProducibleMediaTypes | back to summary |
|---|---|
private List
| |
| getViewResolvers | back to summary |
|---|---|
| public List | |
| initServletContext | back to summary |
|---|---|
| protected void initServletContext(ServletContext servletContext) Overrides org. Doc from org. Subclasses may override this for custom initialization based on the ServletContext that this application object runs in. The default implementation is empty. Called by
| |
| isUseNotAcceptableStatusCode | back to summary |
|---|---|
| public boolean isUseNotAcceptableStatusCode() Whether to return HTTP Status 406 if no suitable is found. | |
| resolveViewName | back to summary |
|---|---|
| public View resolveViewName(String viewName, Locale locale) throws Exception Implements org. Doc from org. Resolve the given view by name. Note To allow for ViewResolver chaining, a ViewResolver should
return | |
| setContentNegotiationManager | back to summary |
|---|---|
| public void setContentNegotiationManager(ContentNegotiationManager contentNegotiationManager) Set the If not set, ContentNegotiationManager's default constructor will be used,
applying a | |
| setDefaultViews | back to summary |
|---|---|
| public void setDefaultViews(List<View> defaultViews) Set the default views to use when a more specific view can not be obtained
from the | |
| setOrder | back to summary |
|---|---|
| public void setOrder(int order) | |
| setUseNotAcceptableStatusCode | back to summary |
|---|---|
| public void setUseNotAcceptableStatusCode(boolean useNotAcceptableStatusCode) Indicate whether a Default is | |
| setViewResolvers | back to summary |
|---|---|
| public void setViewResolvers(List<ViewResolver> viewResolvers) Sets the view resolvers to be wrapped by this view resolver. If this property is not set, view resolvers will be detected automatically. | |