public class UrlBasedViewResolver extends ViewResolverSupport implements ViewResolver, 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 |
---|---|
private java.lang.String |
prefix |
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).
|
private java.util.function.Function<java.lang.String,RedirectView> |
redirectViewProvider |
private java.lang.String |
requestContextAttribute |
private java.lang.String |
suffix |
private java.lang.Class<?> |
viewClass |
private java.lang.String[] |
viewNames |
DEFAULT_CONTENT_TYPE
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
Constructor and Description |
---|
UrlBasedViewResolver() |
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 |
applyLifecycleMethods(java.lang.String viewName,
AbstractView view) |
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 |
createUrlBasedView(java.lang.String viewName)
Creates a new View instance of the specified view class and configures it.
|
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 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.
|
<any> |
resolveViewName(java.lang.String viewName,
java.util.Locale locale)
Resolve the view name to a View instance.
|
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
createUrlBasedView(String) . |
void |
setViewNames(java.lang.String... viewNames)
Set the view names (or name patterns) that can be handled by this
ViewResolver . |
getApplicationContext, getDefaultCharset, getOrder, getSupportedMediaTypes, setApplicationContext, setDefaultCharset, setOrder, setSupportedMediaTypes
public static final java.lang.String REDIRECT_URL_PREFIX
private java.lang.Class<?> viewClass
private java.lang.String prefix
private java.lang.String suffix
private java.lang.String[] viewNames
private java.util.function.Function<java.lang.String,RedirectView> redirectViewProvider
private java.lang.String requestContextAttribute
public void setViewClass(java.lang.Class<?> viewClass)
createUrlBasedView(String)
.viewClass
- a class that is assignable to the required view class
which by default is AbstractUrlBasedView.protected java.lang.Class<?> getViewClass()
protected java.lang.Class<?> requiredViewClass()
AbstractUrlBasedView
.AbstractUrlBasedView
public void setPrefix(java.lang.String prefix)
protected java.lang.String getPrefix()
public void setSuffix(java.lang.String suffix)
protected java.lang.String getSuffix()
public void setViewNames(java.lang.String... viewNames)
ViewResolver
. View names can contain simple wildcards such that
'my*', '*Report' and '*Repo*' will all match the view name 'myReport'.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(java.lang.String requestContextAttribute)
requestContextAttribute
- name of the RequestContext attributeAbstractView.setRequestContextAttribute(java.lang.String)
protected java.lang.String getRequestContextAttribute()
public void afterPropertiesSet() throws java.lang.Exception
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
java.lang.Exception
- in the event of misconfiguration (such
as failure to set an essential property) or if initialization fails.public <any> 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 createUrlBasedView(java.lang.String viewName)
Spring lifecycle methods as defined by the bean container do not have to
be called here; those will be applied by the loadView
method
after this method returns.
Subclasses will typically call super.buildView(viewName)
first, before setting further properties themselves. loadView
will then apply Spring lifecycle methods at the end of this process.
viewName
- the name of the view to buildprivate View applyLifecycleMethods(java.lang.String viewName, AbstractView view)