public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport implements ViewResolver, Ordered, InitializingBean
ViewResolverthat resolves a view based on the request file name or
ContentNegotiatingViewResolver does not resolve views itself, but delegates to
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
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
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,
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
/view with a
Accept header has the same result.
|Constructor and Description|
|Modifier and Type||Method and Description|
Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).
Get the order value of this object.
Subclasses may override this for custom initialization based on the ServletContext that this application object runs in.
Whether to return HTTP Status 406 if no suitable is found.
Resolve the given view by name.
Set the default views to use when a more specific view can not be obtained from the
Indicate whether a
Sets the view resolvers to be wrapped by this view resolver.
getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, isContextRequired, setServletContext
getApplicationContext, getMessageSourceAccessor, initApplicationContext, requiredContextClass, setApplicationContext
public void setOrder(int order)
public int getOrder()
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.
public void setContentNegotiationManager(ContentNegotiationManager contentNegotiationManager)
ContentNegotiationManagerto use to determine requested media types.
If not set, ContentNegotiationManager's default constructor will be used,
public void setUseNotAcceptableStatusCode(boolean useNotAcceptableStatusCode)
406 Not Acceptablestatus code should be returned if no suitable view can be found.
false, meaning that this view resolver returns
resolveViewName(String, Locale) when an acceptable view cannot be found.
This will allow for view resolvers chaining. When this property is set to
resolveViewName(String, Locale) will respond with a view that sets the
response status to
406 Not Acceptable instead.
public boolean isUseNotAcceptableStatusCode()
public void setViewResolvers(List<ViewResolver> viewResolvers)
If this property is not set, view resolvers will be detected automatically.
protected void initServletContext(ServletContext servletContext)
The default implementation is empty. Called by
as well as
public void afterPropertiesSet()
This method allows the bean instance to perform initialization only possible when all bean properties have been set and to throw an exception in the event of misconfiguration.
Note: To allow for ViewResolver chaining, a ViewResolver should
null if a view with the given name is not defined in it.
However, this is not required: Some ViewResolvers will always attempt
to build View objects with the given name, unable to return
(rather throwing an exception when View creation failed).
viewName- name of the view to resolve
locale- Locale in which to resolve the view. ViewResolvers that support internationalization should respect this.
nullif not found (optional, to allow for ViewResolver chaining)
Exception- if the view cannot be resolved (typically in case of problems creating an actual View object)