类 ContentNegotiatingViewResolver
- java.lang.Object
- org.springframework.context.support.ApplicationObjectSupport
- org.springframework.web.context.support.WebApplicationObjectSupport
- org.springframework.web.servlet.view.ContentNegotiatingViewResolver
- 所有已实现的接口:
Aware
,InitializingBean
,ApplicationContextAware
,Ordered
,ServletContextAware
,ViewResolver
public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport implements ViewResolver, Ordered, InitializingBean
Implementation ofViewResolver
that resolves a view based on the request file name orAccept
header.The
ContentNegotiatingViewResolver
does not resolve views itself, but delegates to otherViewResolver
s. By default, these other view resolvers are picked up automatically from the application context, though they can also be set explicitly by using theviewResolvers
property. Note that in order for this view resolver to work properly, theorder
property needs to be set to a higher precedence than the others (the default isOrdered.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 configuredContentNegotiationManager
. Once the requested media type has been determined, this resolver queries each delegate view resolver for aView
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, orAccept
header, described above).For example, if the request path is
/view.html
, this view resolver will look for a view that has thetext/html
content type (based on thehtml
file extension). A request for/view
with atext/html
requestAccept
header has the same result.- 从以下版本开始:
- 3.0
- 作者:
- Arjen Poutsma, Juergen Hoeller, Rossen Stoyanchev
- 另请参阅:
ViewResolver
,InternalResourceViewResolver
,BeanNameViewResolver
字段概要
从类继承的字段 org.springframework.context.support.ApplicationObjectSupport
logger
从接口继承的字段 org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
构造器概要
构造器 构造器 说明 ContentNegotiatingViewResolver()
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 void
afterPropertiesSet()
Invoked by the containingBeanFactory
after it has set all bean properties and satisfiedBeanFactoryAware
,ApplicationContextAware
etc.ContentNegotiationManager
getContentNegotiationManager()
Return theContentNegotiationManager
to use to determine requested media types.List<View>
getDefaultViews()
protected List<MediaType>
getMediaTypes(HttpServletRequest request)
Determines the list ofMediaType
for the givenHttpServletRequest
.int
getOrder()
Get the order value of this object.List<ViewResolver>
getViewResolvers()
protected void
initServletContext(ServletContext servletContext)
Subclasses may override this for custom initialization based on the ServletContext that this application object runs in.boolean
isUseNotAcceptableStatusCode()
Whether to return HTTP Status 406 if no suitable is found.View
resolveViewName(String viewName, Locale locale)
Resolve the given view by name.void
setContentNegotiationManager(ContentNegotiationManager contentNegotiationManager)
Set theContentNegotiationManager
to use to determine requested media types.void
setDefaultViews(List<View> defaultViews)
Set the default views to use when a more specific view can not be obtained from theViewResolver
chain.void
setOrder(int order)
void
setUseNotAcceptableStatusCode(boolean useNotAcceptableStatusCode)
Indicate whether a406 Not Acceptable
status code should be returned if no suitable view can be found.void
setViewResolvers(List<ViewResolver> viewResolvers)
Sets the view resolvers to be wrapped by this view resolver.从类继承的方法 org.springframework.web.context.support.WebApplicationObjectSupport
getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, isContextRequired, setServletContext
从类继承的方法 org.springframework.context.support.ApplicationObjectSupport
getApplicationContext, getMessageSourceAccessor, initApplicationContext, requiredContextClass, setApplicationContext
构造器详细资料
ContentNegotiatingViewResolver
public ContentNegotiatingViewResolver()
方法详细资料
setContentNegotiationManager
public void setContentNegotiationManager(ContentNegotiationManager contentNegotiationManager)
Set theContentNegotiationManager
to use to determine requested media types.If not set, ContentNegotiationManager's default constructor will be used, applying a
HeaderContentNegotiationStrategy
.
getContentNegotiationManager
public ContentNegotiationManager getContentNegotiationManager()
Return theContentNegotiationManager
to use to determine requested media types.- 从以下版本开始:
- 4.1.9
setUseNotAcceptableStatusCode
public void setUseNotAcceptableStatusCode(boolean useNotAcceptableStatusCode)
Indicate whether a406 Not Acceptable
status code should be returned if no suitable view can be found.Default is
false
, meaning that this view resolver returnsnull
forresolveViewName(String, Locale)
when an acceptable view cannot be found. This will allow for view resolvers chaining. When this property is set totrue
,resolveViewName(String, Locale)
will respond with a view that sets the response status to406 Not Acceptable
instead.
isUseNotAcceptableStatusCode
public boolean isUseNotAcceptableStatusCode()
Whether to return HTTP Status 406 if no suitable is found.
setDefaultViews
public void setDefaultViews(List<View> defaultViews)
Set the default views to use when a more specific view can not be obtained from theViewResolver
chain.
getDefaultViews
public List<View> getDefaultViews()
setViewResolvers
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.
getViewResolvers
public List<ViewResolver> getViewResolvers()
setOrder
public void setOrder(int order)
getOrder
public int getOrder()
从接口复制的说明:Ordered
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
load-on-startup
values).Same order values will result in arbitrary sort positions for the affected objects.
- 指定者:
getOrder
在接口中Ordered
- 返回:
- the order value
- 另请参阅:
Ordered.HIGHEST_PRECEDENCE
,Ordered.LOWEST_PRECEDENCE
initServletContext
protected void initServletContext(ServletContext servletContext)
从类复制的说明:WebApplicationObjectSupport
Subclasses may override this for custom initialization based on the ServletContext that this application object runs in.The default implementation is empty. Called by
WebApplicationObjectSupport.initApplicationContext(org.springframework.context.ApplicationContext)
as well asWebApplicationObjectSupport.setServletContext(javax.servlet.ServletContext)
.- 覆盖:
initServletContext
在类中WebApplicationObjectSupport
- 参数:
servletContext
- the ServletContext that this application object runs in (nevernull
)
afterPropertiesSet
public void afterPropertiesSet()
从接口复制的说明:InitializingBean
Invoked by the containingBeanFactory
after it has set all bean properties and satisfiedBeanFactoryAware
,ApplicationContextAware
etc.This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.
- 指定者:
afterPropertiesSet
在接口中InitializingBean
resolveViewName
public View resolveViewName(String viewName, Locale locale) throws Exception
从接口复制的说明:ViewResolver
Resolve the given view by name.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 returnnull
(rather throwing an exception when View creation failed).- 指定者:
resolveViewName
在接口中ViewResolver
- 参数:
viewName
- name of the view to resolvelocale
- Locale in which to resolve the view. ViewResolvers that support internationalization should respect this.- 返回:
- the View object, or
null
if 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)
getMediaTypes
protected List<MediaType> getMediaTypes(HttpServletRequest request)
Determines the list ofMediaType
for the givenHttpServletRequest
.- 参数:
request
- the current servlet request- 返回:
- the list of media types requested, if any