Class UrlBasedViewResolver
- All Implemented Interfaces:
- Aware, InitializingBean, ApplicationContextAware, Ordered, ViewResolver
- Direct Known Subclasses:
- FreeMarkerViewResolver, ScriptTemplateViewResolver
ViewResolver that allows direct resolution of symbolic view names
to URLs without explicit mapping definitions. This is useful if symbolic names
match the names of view resources in a straightforward manner (i.e. the
symbolic name is the unique part of the resource's filename), without the need
for a dedicated mapping to be defined for each view.
Supports AbstractUrlBasedView subclasses like
FreeMarkerView.
The view class for all views generated by this resolver can be specified
via the "viewClass" property.
View names can either be resource URLs themselves, or get augmented by a specified prefix and/or suffix. Exporting an attribute that holds the RequestContext to all views is explicitly supported.
Example: prefix="templates/", suffix=".ftl", viewname="test" → "templates/test.ftl"
As a special feature, redirect URLs can be specified via the "redirect:" prefix. For example: "redirect:myAction" will trigger a redirect to the given URL, rather than resolution as standard view name. This is typically used for redirecting to a controller URL after finishing a form workflow.
Note: This class does not support localized resolution, i.e. resolving a symbolic view name to different resources depending on the current locale.
- Since:
- 5.0
- Author:
- Rossen Stoyanchev, Sebastien Deleuze, Juergen Hoeller, Sam Brannen
- 
Field SummaryFieldsModifier and TypeFieldDescriptionstatic final StringPrefix for special view names that specify a redirect URL (usually to a controller after a form has been submitted and processed).Fields inherited from class ViewResolverSupportDEFAULT_CONTENT_TYPEFields inherited from interface OrderedHIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionvoidInvoked by the containingBeanFactoryafter it has set all bean properties and satisfiedBeanFactoryAware,ApplicationContextAwareetc.protected ViewapplyLifecycleMethods(String viewName, AbstractUrlBasedView view) Apply the containingApplicationContext's lifecycle methods to the givenViewinstance, if such a context is available.protected booleanIndicates whether thisViewResolvercan handle the supplied view name.protected AbstractUrlBasedViewcreateView(String viewName) Creates a new View instance of the specified view class and configures it.Return the containingApplicationContext, if any.protected StringReturn the prefix that gets prepended to view names when building a URL.Return the name of theRequestContextattribute for all views, if any.protected StringReturn the suffix that gets appended to view names when building a URL.Return the view class to be used to create views.Return the view names (or name patterns) that can be handled by thisViewResolver.protected AbstractUrlBasedViewInstantiate the specified view class.protected Class<?> Return the required type of view for this resolver.reactor.core.publisher.Mono<View> resolveViewName(String viewName, Locale locale) Resolve the view name to a View instance.voidsetApplicationContext(@Nullable ApplicationContext applicationContext) Accept the containingApplicationContext, if any.voidSet the prefix that gets prepended to view names when building a URL.voidsetRedirectViewProvider(Function<String, RedirectView> redirectViewProvider) URL basedRedirectViewprovider which can be used to provide, for example, redirect views with a custom default status code.voidsetRequestContextAttribute(@Nullable String requestContextAttribute) Set the name of theRequestContextattribute for all views.voidSet the suffix that gets appended to view names when building a URL.voidsetViewClass(@Nullable Class<?> viewClass) Set the view class that should be used to create views.voidsetViewNames(String @Nullable ... viewNames) Set the view names (or name patterns) that can be handled by thisViewResolver.Methods inherited from class ViewResolverSupportgetDefaultCharset, getOrder, getSupportedMediaTypes, setDefaultCharset, setOrder, setSupportedMediaTypes
- 
Field Details- 
REDIRECT_URL_PREFIXPrefix for special view names that specify a redirect URL (usually to a controller after a form has been submitted and processed). Such view names will not be resolved in the configured default way but rather be treated as special shortcut.- See Also:
 
 
- 
- 
Constructor Details- 
UrlBasedViewResolverpublic UrlBasedViewResolver()
 
- 
- 
Method Details- 
setViewClass
- 
getViewClass
- 
setPrefix
- 
getPrefixReturn the prefix that gets prepended to view names when building a URL.
- 
setSuffix
- 
getSuffixReturn the suffix that gets appended to view names when building a URL.
- 
setViewNamesSet the view names (or name patterns) that can be handled by thisViewResolver. View names can contain simple wildcards such that 'my*', '*Report' and '*Repo*' will all match the view name 'myReport'.- See Also:
 
- 
getViewNamesReturn the view names (or name patterns) that can be handled by thisViewResolver.
- 
setRedirectViewProviderURL basedRedirectViewprovider which can be used to provide, for example, redirect views with a custom default status code.
- 
setRequestContextAttributeSet the name of theRequestContextattribute for all views.- Parameters:
- requestContextAttribute- name of the RequestContext attribute
- See Also:
 
- 
getRequestContextAttributeReturn the name of theRequestContextattribute for all views, if any.
- 
setApplicationContextAccept the containingApplicationContext, if any.To be used for the initialization of newly created Viewinstances, applying lifecycle callbacks and providing access to the containing environment.- Specified by:
- setApplicationContextin interface- ApplicationContextAware
- Parameters:
- applicationContext- the ApplicationContext object to be used by this object
- See Also:
 
- 
getApplicationContextReturn the containingApplicationContext, if any.- See Also:
 
- 
afterPropertiesSetDescription copied from interface:InitializingBeanInvoked by the containingBeanFactoryafter it has set all bean properties and satisfiedBeanFactoryAware,ApplicationContextAwareetc.This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set. - Specified by:
- afterPropertiesSetin interface- InitializingBean
- Throws:
- Exception- in the event of misconfiguration (such as failure to set an essential property) or if initialization fails for any other reason
 
- 
resolveViewNameDescription copied from interface:ViewResolverResolve the view name to a View instance.- Specified by:
- resolveViewNamein interface- ViewResolver
- Parameters:
- viewName- the name of the view to resolve
- locale- the locale for the request
- Returns:
- the resolved view or an empty stream
 
- 
canHandleIndicates whether thisViewResolvercan handle the supplied view name. If not, an empty result is returned. The default implementation checks against the configuredview names.- Parameters:
- viewName- the name of the view to retrieve
- locale- the Locale to retrieve the view for
- Returns:
- whether this resolver applies to the specified view
- See Also:
 
- 
requiredViewClassReturn the required type of view for this resolver. This implementation returnsAbstractUrlBasedView.- See Also:
 
- 
instantiateViewInstantiate the specified view class.The default implementation uses reflection to instantiate the class. - Returns:
- a new instance of the view class
- Since:
- 5.3
- See Also:
 
- 
createViewCreates a new View instance of the specified view class and configures it.Does not perform any lookup for pre-defined View instances. Spring lifecycle methods as defined by the bean container do not have to be called here: They will be automatically applied afterwards, provided that an ApplicationContextis available.- Parameters:
- viewName- the name of the view to build
- Returns:
- the View instance
- See Also:
 
- 
applyLifecycleMethodsApply the containingApplicationContext's lifecycle methods to the givenViewinstance, if such a context is available.- Parameters:
- viewName- the name of the view
- view- the freshly created View instance, pre-configured with- AbstractUrlBasedView's properties
- Returns:
- the Viewinstance to use (either the original one or a decorated variant)
- See Also:
 
 
-