public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport implements ViewResolver, Ordered, InitializingBean
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 ViewResolver
s. 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 candicates, and
still need have the content type requested (via file extension, parameter, or Accept
header, described above).
You can also set the default content type directly, which will be
returned when the other mechanisms (Accept
header, file extension or parameter) do not result in a match.
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 ContentNegotiationManagerFactoryBean |
cnManagerFactoryBean |
private ContentNegotiationManager |
contentNegotiationManager |
private java.util.List<View> |
defaultViews |
private static Log |
logger |
private static View |
NOT_ACCEPTABLE_VIEW |
private int |
order |
private boolean |
useNotAcceptableStatusCode |
private java.util.List<ViewResolver> |
viewResolvers |
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
Constructor and Description |
---|
ContentNegotiatingViewResolver() |
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet()
Invoked by a BeanFactory after it has set all bean properties supplied
(and satisfied BeanFactoryAware and ApplicationContextAware).
|
private View |
getBestView(java.util.List<View> candidateViews,
java.util.List<MediaType> requestedMediaTypes,
RequestAttributes attrs) |
private java.util.List<View> |
getCandidateViews(java.lang.String viewName,
java.util.Locale locale,
java.util.List<MediaType> requestedMediaTypes) |
protected java.util.List<MediaType> |
getMediaTypes(HttpServletRequest request)
Determines the list of
MediaType for the given HttpServletRequest . |
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.
|
int |
getOrder()
Return the order value of this object, with a
higher value meaning greater in terms of sorting.
|
private java.util.List<MediaType> |
getProducibleMediaTypes(HttpServletRequest request) |
protected void |
initServletContext(ServletContext servletContext)
Subclasses may override this for custom initialization based
on the ServletContext that this application object runs in.
|
View |
resolveViewName(java.lang.String viewName,
java.util.Locale locale)
Resolve the given view by name.
|
void |
setContentNegotiationManager(ContentNegotiationManager contentNegotiationManager)
Set the
ContentNegotiationManager to use to determine requested media types. |
void |
setDefaultContentType(MediaType defaultContentType)
Deprecated.
|
void |
setDefaultViews(java.util.List<View> defaultViews)
Set the default views to use when a more specific view can not be obtained
from the
ViewResolver chain. |
void |
setFavorParameter(boolean favorParameter)
Deprecated.
|
void |
setFavorPathExtension(boolean favorPathExtension)
Deprecated.
|
void |
setIgnoreAcceptHeader(boolean ignoreAcceptHeader)
Deprecated.
|
void |
setMediaTypes(java.util.Map<java.lang.String,java.lang.String> mediaTypes)
Deprecated.
|
void |
setOrder(int order) |
void |
setParameterName(java.lang.String parameterName)
Deprecated.
|
void |
setUseJaf(boolean useJaf)
Deprecated.
|
void |
setUseNotAcceptableStatusCode(boolean useNotAcceptableStatusCode)
Indicate whether a
406 Not Acceptable
status code should be returned if no suitable view can be found. |
void |
setViewResolvers(java.util.List<ViewResolver> viewResolvers)
Sets the view resolvers to be wrapped by this view resolver.
|
getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, isContextRequired, setServletContext
getApplicationContext, getMessageSourceAccessor, initApplicationContext, requiredContextClass, setApplicationContext
private static final Log logger
private int order
private ContentNegotiationManager contentNegotiationManager
private final ContentNegotiationManagerFactoryBean cnManagerFactoryBean
private boolean useNotAcceptableStatusCode
private java.util.List<View> defaultViews
private java.util.List<ViewResolver> viewResolvers
private static final View NOT_ACCEPTABLE_VIEW
public void setOrder(int order)
public int getOrder()
Ordered
Normally starting with 0, with Integer.MAX_VALUE
indicating the greatest value. Same order values will result
in arbitrary positions for the affected objects.
Higher values can be interpreted as lower priority. As a consequence, the object with the lowest value has highest priority (somewhat analogous to Servlet "load-on-startup" values).
public void setContentNegotiationManager(ContentNegotiationManager contentNegotiationManager)
ContentNegotiationManager
to use to determine requested media types.
If not set, ContentNegotiationManager's default constructor will be used,
applying a HeaderContentNegotiationStrategy
.
@Deprecated public void setFavorPathExtension(boolean favorPathExtension)
setContentNegotiationManager(ContentNegotiationManager)
Accept
header. The default value is true
.
For instance, when this flag is true
(the default), a request for /hotels.pdf
will result in an AbstractPdfView
being resolved, while the Accept
header can be the
browser-defined text/html,application/xhtml+xml
.
@Deprecated public void setUseJaf(boolean useJaf)
setContentNegotiationManager(ContentNegotiationManager)
Default is true
, i.e. the Java Activation Framework is used (if available).
@Deprecated public void setFavorParameter(boolean favorParameter)
setContentNegotiationManager(ContentNegotiationManager)
Accept
header. The default value is false
.
For instance, when this flag is true
, a request for /hotels?format=pdf
will result
in an AbstractPdfView
being resolved, while the Accept
header can be the browser-defined
text/html,application/xhtml+xml
.
@Deprecated public void setParameterName(java.lang.String parameterName)
setContentNegotiationManager(ContentNegotiationManager)
setFavorParameter(boolean)
property is true
. The default parameter name is format
.@Deprecated public void setIgnoreAcceptHeader(boolean ignoreAcceptHeader)
setContentNegotiationManager(ContentNegotiationManager)
Accept
header should be ignored. Default is false
.
If set to true
, this view resolver will only refer to the file extension and/or
parameter, as indicated by the favorPathExtension
and
favorParameter
properties.
@Deprecated public void setMediaTypes(java.util.Map<java.lang.String,java.lang.String> mediaTypes)
setContentNegotiationManager(ContentNegotiationManager)
When this mapping is not set or when an extension is not present, this view resolver
will fall back to using a FileTypeMap
when the Java Action Framework is available.
@Deprecated public void setDefaultContentType(MediaType defaultContentType)
setContentNegotiationManager(ContentNegotiationManager)
This content type will be used when file extension, parameter, nor Accept
header define a content-type, either through being disabled or empty.
public void setUseNotAcceptableStatusCode(boolean useNotAcceptableStatusCode)
406 Not Acceptable
status code should be returned if no suitable view can be found.
Default is false
, meaning that this view resolver returns null
for
resolveViewName(String, Locale)
when an acceptable view cannot be found.
This will allow for view resolvers chaining. When this property is set to true
,
resolveViewName(String, Locale)
will respond with a view that sets the
response status to 406 Not Acceptable
instead.
public void setDefaultViews(java.util.List<View> defaultViews)
ViewResolver
chain.public void setViewResolvers(java.util.List<ViewResolver> viewResolvers)
If this property is not set, view resolvers will be detected automatically.
protected void initServletContext(ServletContext servletContext)
WebApplicationObjectSupport
The default implementation is empty. Called by
WebApplicationObjectSupport.initApplicationContext(org.springframework.context.ApplicationContext)
as well as #setServletContext(javax.servlet.ServletContext)
.
initServletContext
in class WebApplicationObjectSupport
servletContext
- the ServletContext that this application object runs in
(never null
)public void afterPropertiesSet()
InitializingBean
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.
afterPropertiesSet
in interface InitializingBean
public View resolveViewName(java.lang.String viewName, java.util.Locale locale) throws java.lang.Exception
ViewResolver
Note: To allow for ViewResolver chaining, a ViewResolver should
return 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 null
(rather throwing an exception when View creation failed).
resolveViewName
in interface ViewResolver
viewName
- name of the view to resolvelocale
- Locale in which to resolve the view.
ViewResolvers that support internationalization should respect this.null
if not found
(optional, to allow for ViewResolver chaining)java.lang.Exception
- if the view cannot be resolved
(typically in case of problems creating an actual View object)protected java.util.List<MediaType> getMediaTypes(HttpServletRequest request)
MediaType
for the given HttpServletRequest
.request
- the current servlet requestprivate java.util.List<MediaType> getProducibleMediaTypes(HttpServletRequest request)
private MediaType getMostSpecificMediaType(MediaType acceptType, MediaType produceType)
private java.util.List<View> getCandidateViews(java.lang.String viewName, java.util.Locale locale, java.util.List<MediaType> requestedMediaTypes) throws java.lang.Exception
java.lang.Exception
private View getBestView(java.util.List<View> candidateViews, java.util.List<MediaType> requestedMediaTypes, RequestAttributes attrs)