类 ContentNegotiatingViewResolver
- 所有已实现的接口:
cn.taketoday.beans.factory.Aware,cn.taketoday.beans.factory.InitializingBean,cn.taketoday.context.ApplicationContextAware,cn.taketoday.core.Ordered,ViewResolver
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.
- 从以下版本开始:
- 4.0
- 作者:
- Arjen Poutsma, Juergen Hoeller, Rossen Stoyanchev
- 另请参阅:
-
字段概要
字段修饰符和类型字段说明private ContentNegotiationManagerprivate static final Viewprivate intprivate booleanprivate List<ViewResolver>从类继承的字段 cn.taketoday.context.support.ApplicationObjectSupport
applicationContext, log, messageSourceAccessor从接口继承的字段 cn.taketoday.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE -
构造器概要
构造器 -
方法概要
修饰符和类型方法说明voidprivate ViewgetBestView(List<View> candidateViews, List<MediaType> requestedMediaTypes, RequestContext context) getCandidateViews(String viewName, Locale locale, List<MediaType> requestedMediaTypes) Return theContentNegotiationManagerto use to determine requested media types.getMediaTypes(RequestContext context) Determines the list ofMediaTypefor the givenHttpServletRequest.private MediaTypegetMostSpecificMediaType(MediaType acceptType, MediaType produceType) Return the more specific of the acceptable and the producible media types with the q-value of the former.intgetOrder()private MediaType[]getProducibleMediaTypes(RequestContext context) protected voidbooleanWhether to return HTTP Status 406 if no suitable is found.resolveViewName(String viewName, Locale locale) Resolve the given view by name.voidsetContentNegotiationManager(ContentNegotiationManager contentNegotiationManager) Set theContentNegotiationManagerto use to determine requested media types.voidsetDefaultViews(List<View> defaultViews) Set the default views to use when a more specific view can not be obtained from theViewResolverchain.voidsetOrder(int order) voidsetUseNotAcceptableStatusCode(boolean useNotAcceptableStatusCode) Indicate whether a406 Not Acceptablestatus code should be returned if no suitable view can be found.voidsetViewResolvers(List<ViewResolver> viewResolvers) Sets the view resolvers to be wrapped by this view resolver.从类继承的方法 cn.taketoday.context.support.ApplicationObjectSupport
getApplicationContext, getMessageSourceAccessor, initApplicationContext, isContextRequired, obtainApplicationContext, requiredContextClass, setApplicationContext, unwrapContext, unwrapFactory
-
字段详细资料
-
contentNegotiationManager
-
useNotAcceptableStatusCode
private boolean useNotAcceptableStatusCode -
defaultViews
-
viewResolvers
-
order
private int order -
NOT_ACCEPTABLE_VIEW
-
-
构造器详细资料
-
ContentNegotiatingViewResolver
public ContentNegotiatingViewResolver()
-
-
方法详细资料
-
setContentNegotiationManager
public void setContentNegotiationManager(@Nullable ContentNegotiationManager contentNegotiationManager) Set theContentNegotiationManagerto use to determine requested media types.If not set, ContentNegotiationManager's default constructor will be used, applying a
HeaderContentNegotiationStrategy. -
getContentNegotiationManager
Return theContentNegotiationManagerto use to determine requested media types. -
setUseNotAcceptableStatusCode
public void setUseNotAcceptableStatusCode(boolean useNotAcceptableStatusCode) Indicate whether a406 Not Acceptablestatus code should be returned if no suitable view can be found.Default is
false, meaning that this view resolver returnsnullforresolveViewName(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 Acceptableinstead. -
isUseNotAcceptableStatusCode
public boolean isUseNotAcceptableStatusCode()Whether to return HTTP Status 406 if no suitable is found. -
setDefaultViews
Set the default views to use when a more specific view can not be obtained from theViewResolverchain. -
getDefaultViews
-
setViewResolvers
Sets the view resolvers to be wrapped by this view resolver.If this property is not set, view resolvers will be detected automatically.
-
getViewResolvers
-
setOrder
public void setOrder(int order) -
getOrder
public int getOrder()- 指定者:
getOrder在接口中cn.taketoday.core.Ordered
-
initApplicationContext
protected void initApplicationContext()- 覆盖:
initApplicationContext在类中cn.taketoday.context.support.ApplicationObjectSupport
-
afterPropertiesSet
public void afterPropertiesSet()- 指定者:
afterPropertiesSet在接口中cn.taketoday.beans.factory.InitializingBean
-
resolveViewName
从接口复制的说明:ViewResolverResolve the given view by name.Note: To allow for ViewResolver chaining, a ViewResolver should return
nullif 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- the Locale in which to resolve the view. ViewResolvers that support internationalization should respect this.- 返回:
- the View object, or
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)
-
getMediaTypes
Determines the list ofMediaTypefor the givenHttpServletRequest.- 参数:
context- the current request- 返回:
- the list of media types requested, if any
-
getProducibleMediaTypes
-
getMostSpecificMediaType
Return the more specific of the acceptable and the producible media types with the q-value of the former. -
getCandidateViews
private List<View> getCandidateViews(String viewName, Locale locale, List<MediaType> requestedMediaTypes) throws Exception - 抛出:
Exception
-
getBestView
-