public class UrlBasedViewResolver extends ViewResolverSupport implements ViewResolver, ApplicationContextAware, InitializingBean
ViewResolver
that allow direct resolution of symbolic view names
to URLs without explicit mapping definition. 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. E.g.: "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.
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
REDIRECT_URL_PREFIX
Prefix for special view names that specify a redirect URL (usually
to a controller after a form has been submitted and processed).
|
DEFAULT_CONTENT_TYPE
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
Constructor and Description |
---|
UrlBasedViewResolver() |
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet()
Invoked by the containing
BeanFactory after it has set all bean properties
and satisfied BeanFactoryAware , ApplicationContextAware etc. |
protected View |
applyLifecycleMethods(java.lang.String viewName,
AbstractUrlBasedView view)
Apply the containing
ApplicationContext 's lifecycle methods
to the given View instance, if such a context is available. |
protected boolean |
canHandle(java.lang.String viewName,
java.util.Locale locale)
Indicates whether or not this
ViewResolver can handle the supplied
view name. |
protected AbstractUrlBasedView |
createView(java.lang.String viewName)
Creates a new View instance of the specified view class and configures it.
|
ApplicationContext |
getApplicationContext()
Return the containing
ApplicationContext , if any. |
protected java.lang.String |
getPrefix()
Return the prefix that gets prepended to view names when building a URL.
|
protected java.lang.String |
getRequestContextAttribute()
Return the name of the @link RequestContext} attribute for all views, if any.
|
protected java.lang.String |
getSuffix()
Return the suffix that gets appended to view names when building a URL.
|
protected java.lang.Class<?> |
getViewClass()
Return the view class to be used to create views.
|
protected java.lang.String[] |
getViewNames()
Return the view names (or name patterns) that can be handled by this
ViewResolver . |
protected java.lang.Class<?> |
requiredViewClass()
Return the required type of view for this resolver.
|
reactor.core.publisher.Mono<View> |
resolveViewName(java.lang.String viewName,
java.util.Locale locale)
Resolve the view name to a View instance.
|
void |
setApplicationContext(ApplicationContext applicationContext)
Accept the containing
ApplicationContext , if any. |
void |
setPrefix(java.lang.String prefix)
Set the prefix that gets prepended to view names when building a URL.
|
void |
setRedirectViewProvider(java.util.function.Function<java.lang.String,RedirectView> redirectViewProvider)
URL based
RedirectView provider which can be used to provide, for example,
redirect views with a custom default status code. |
void |
setRequestContextAttribute(java.lang.String requestContextAttribute)
Set the name of the
RequestContext attribute for all views. |
void |
setSuffix(java.lang.String suffix)
Set the suffix that gets appended to view names when building a URL.
|
void |
setViewClass(java.lang.Class<?> viewClass)
Set the view class to instantiate through
createView(String) . |
void |
setViewNames(java.lang.String... viewNames)
Set the view names (or name patterns) that can be handled by this
ViewResolver . |
getDefaultCharset, getOrder, getSupportedMediaTypes, setDefaultCharset, setOrder, setSupportedMediaTypes
public static final java.lang.String REDIRECT_URL_PREFIX
public void setViewClass(@Nullable java.lang.Class<?> viewClass)
createView(String)
.viewClass
- a class that is assignable to the required view class
which by default is AbstractUrlBasedView@Nullable protected java.lang.Class<?> getViewClass()
protected java.lang.Class<?> requiredViewClass()
AbstractUrlBasedView
.AbstractUrlBasedView
public void setPrefix(@Nullable java.lang.String prefix)
protected java.lang.String getPrefix()
public void setSuffix(@Nullable java.lang.String suffix)
protected java.lang.String getSuffix()
public void setViewNames(@Nullable java.lang.String... viewNames)
ViewResolver
. View names can contain simple wildcards such that
'my*', '*Report' and '*Repo*' will all match the view name 'myReport'.@Nullable protected java.lang.String[] getViewNames()
ViewResolver
.public void setRedirectViewProvider(java.util.function.Function<java.lang.String,RedirectView> redirectViewProvider)
RedirectView
provider which can be used to provide, for example,
redirect views with a custom default status code.public void setRequestContextAttribute(@Nullable java.lang.String requestContextAttribute)
RequestContext
attribute for all views.requestContextAttribute
- name of the RequestContext attributeAbstractView.setRequestContextAttribute(java.lang.String)
@Nullable protected java.lang.String getRequestContextAttribute()
public void setApplicationContext(@Nullable ApplicationContext applicationContext)
ApplicationContext
, if any.
To be used for the initialization of newly created View
instances,
applying lifecycle callbacks and providing access to the containing environment.
setApplicationContext
in interface ApplicationContextAware
applicationContext
- the ApplicationContext object to be used by this objectsetViewClass(java.lang.Class<?>)
,
createView(java.lang.String)
,
applyLifecycleMethods(java.lang.String, org.springframework.web.reactive.result.view.AbstractUrlBasedView)
@Nullable public ApplicationContext getApplicationContext()
ApplicationContext
, if any.public void afterPropertiesSet() throws java.lang.Exception
InitializingBean
BeanFactory
after it has set all bean properties
and satisfied BeanFactoryAware
, 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
in interface InitializingBean
java.lang.Exception
- in the event of misconfiguration (such as failure to set an
essential property) or if initialization fails for any other reasonpublic reactor.core.publisher.Mono<View> resolveViewName(java.lang.String viewName, java.util.Locale locale)
ViewResolver
resolveViewName
in interface ViewResolver
viewName
- the name of the view to resolvelocale
- the locale for the requestprotected boolean canHandle(java.lang.String viewName, java.util.Locale locale)
ViewResolver
can handle the supplied
view name. If not, an empty result is returned. The default implementation
checks against the configured view names
.viewName
- the name of the view to retrievelocale
- the Locale to retrieve the view forPatternMatchUtils.simpleMatch(String, String)
protected AbstractUrlBasedView createView(java.lang.String viewName)
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 ApplicationContext
is available.
viewName
- the name of the view to buildgetViewClass()
,
applyLifecycleMethods(java.lang.String, org.springframework.web.reactive.result.view.AbstractUrlBasedView)
protected View applyLifecycleMethods(java.lang.String viewName, AbstractUrlBasedView view)
ApplicationContext
's lifecycle methods
to the given View
instance, if such a context is available.viewName
- the name of the viewview
- the freshly created View instance, pre-configured with
AbstractUrlBasedView
's propertiesView
instance to use (either the original one
or a decorated variant)getApplicationContext()
,
ApplicationContext.getAutowireCapableBeanFactory()
,
AutowireCapableBeanFactory.initializeBean(java.lang.Object, java.lang.String)