public class UrlBasedViewResolver extends AbstractCachingViewResolver implements Ordered
ViewResolver
interface, allowing for direct resolution of symbolic view names to URLs,
without explicit mapping definition. This is useful if your symbolic names
match the names of your 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 InternalResourceView
and 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="/WEB-INF/jsp/", suffix=".jsp", viewname="test" -> "/WEB-INF/jsp/test.jsp"
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.
Furthermore, forward URLs can be specified via the "forward:" prefix. E.g.: "forward:myAction" will trigger a forward to the given URL, rather than resolution as standard view name. This is typically used for controller URLs; it is not supposed to be used for JSP URLs - use logical view names there.
Note: This class does not support localized resolution, i.e. resolving a symbolic view name to different resources depending on the current locale.
Note: When chaining ViewResolvers, a UrlBasedViewResolver will check whether
the specified resource actually exists
.
However, with InternalResourceView
, it is not generally possible to
determine the existence of the target resource upfront. In such a scenario,
a UrlBasedViewResolver will always return View for any given view name;
as a consequence, it should be configured as the last ViewResolver in the chain.
setViewClass(java.lang.Class<?>)
,
setPrefix(java.lang.String)
,
setSuffix(java.lang.String)
,
setRequestContextAttribute(java.lang.String)
,
REDIRECT_URL_PREFIX
,
AbstractUrlBasedView
,
InternalResourceView
,
FreeMarkerView
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
FORWARD_URL_PREFIX
Prefix for special view names that specify a forward URL (usually
to a controller after a form has been submitted and processed).
|
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_CACHE_LIMIT
logger
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
Constructor and Description |
---|
UrlBasedViewResolver() |
Modifier and Type | Method and Description |
---|---|
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 AbstractUrlBasedView |
buildView(java.lang.String viewName)
Creates a new View instance of the specified view class and configures it.
|
protected boolean |
canHandle(java.lang.String viewName,
java.util.Locale locale)
Indicates whether or not this
ViewResolver can
handle the supplied view name. |
protected View |
createView(java.lang.String viewName,
java.util.Locale locale)
Overridden to implement check for "redirect:" prefix.
|
java.util.Map<java.lang.String,java.lang.Object> |
getAttributesMap()
Allow Map access to the static attributes for views returned by
this resolver, with the option to add or override specific entries.
|
protected java.lang.Object |
getCacheKey(java.lang.String viewName,
java.util.Locale locale)
This implementation returns just the view name,
as this ViewResolver doesn't support localized resolution.
|
protected java.lang.String |
getContentType()
Return the content type for all views, if any.
|
protected java.lang.Boolean |
getExposeContextBeansAsAttributes() |
protected java.lang.String[] |
getExposedContextBeanNames() |
protected java.lang.Boolean |
getExposePathVariables()
Return whether views resolved by this resolver should add path variables to the model or not.
|
int |
getOrder()
Get the order value of this object.
|
protected java.lang.String |
getPrefix()
Return the prefix that gets prepended to view names when building a URL.
|
java.lang.String[] |
getRedirectHosts()
Return the configured application hosts for redirect purposes.
|
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 void |
initApplicationContext()
Subclasses can override this for custom initialization behavior.
|
protected boolean |
isRedirectContextRelative()
Return whether to interpret a given redirect URL that starts with a
slash ("/") as relative to the current ServletContext, i.e.
|
protected boolean |
isRedirectHttp10Compatible()
Return whether redirects should stay compatible with HTTP 1.0 clients.
|
protected View |
loadView(java.lang.String viewName,
java.util.Locale locale)
Delegates to
buildView for creating a new instance of the
specified view class. |
protected java.lang.Class<?> |
requiredViewClass()
Return the required type of view for this resolver.
|
void |
setAttributes(java.util.Properties props)
Set static attributes from a
java.util.Properties object,
for all views returned by this resolver. |
void |
setAttributesMap(java.util.Map<java.lang.String,?> attributes)
Set static attributes from a Map, for all views returned by this resolver.
|
void |
setContentType(java.lang.String contentType)
Set the content type for all views.
|
void |
setExposeContextBeansAsAttributes(boolean exposeContextBeansAsAttributes)
Set whether to make all Spring beans in the application context accessible
as request attributes, through lazy checking once an attribute gets accessed.
|
void |
setExposedContextBeanNames(java.lang.String... exposedContextBeanNames)
Specify the names of beans in the context which are supposed to be exposed.
|
void |
setExposePathVariables(java.lang.Boolean exposePathVariables)
Specify whether views resolved by this resolver should add path variables to the model or not.
|
void |
setOrder(int order)
Specify the order value for this ViewResolver bean.
|
void |
setPrefix(java.lang.String prefix)
Set the prefix that gets prepended to view names when building a URL.
|
void |
setRedirectContextRelative(boolean redirectContextRelative)
Set whether to interpret a given redirect URL that starts with a
slash ("/") as relative to the current ServletContext, i.e.
|
void |
setRedirectHosts(java.lang.String... redirectHosts)
Configure one or more hosts associated with the application.
|
void |
setRedirectHttp10Compatible(boolean redirectHttp10Compatible)
Set whether redirects should stay compatible with HTTP 1.0 clients.
|
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 that should be used to create views.
|
void |
setViewNames(java.lang.String... viewNames)
Set the view names (or name patterns) that can be handled by this
ViewResolver . |
clearCache, getCacheLimit, isCache, isCacheUnresolved, removeFromCache, resolveViewName, setCache, setCacheLimit, setCacheUnresolved
getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContext
getApplicationContext, getMessageSourceAccessor, obtainApplicationContext, requiredContextClass, setApplicationContext
public static final java.lang.String REDIRECT_URL_PREFIX
public static final java.lang.String FORWARD_URL_PREFIX
public void setViewClass(@Nullable java.lang.Class<?> viewClass)
viewClass
- class that is assignable to the required view class
(by default, AbstractUrlBasedView)AbstractUrlBasedView
@Nullable protected java.lang.Class<?> getViewClass()
protected java.lang.Class<?> requiredViewClass()
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 setContentType(@Nullable java.lang.String contentType)
May be ignored by view classes if the view itself is assumed to set the content type, e.g. in case of JSPs.
@Nullable protected java.lang.String getContentType()
public void setRedirectContextRelative(boolean redirectContextRelative)
Default is "true": A redirect URL that starts with a slash will be interpreted as relative to the web application root, i.e. the context path will be prepended to the URL.
Redirect URLs can be specified via the "redirect:" prefix. E.g.: "redirect:myAction"
protected boolean isRedirectContextRelative()
public void setRedirectHttp10Compatible(boolean redirectHttp10Compatible)
In the default implementation, this will enforce HTTP status code 302
in any case, i.e. delegate to HttpServletResponse.sendRedirect
.
Turning this off will send HTTP status code 303, which is the correct
code for HTTP 1.1 clients, but not understood by HTTP 1.0 clients.
Many HTTP 1.1 clients treat 302 just like 303, not making any difference. However, some clients depend on 303 when redirecting after a POST request; turn this flag off in such a scenario.
Redirect URLs can be specified via the "redirect:" prefix. E.g.: "redirect:myAction"
protected boolean isRedirectHttp10Compatible()
public void setRedirectHosts(@Nullable java.lang.String... redirectHosts)
In effect, this property provides a way turn off encoding on redirect
via HttpServletResponse.encodeRedirectURL(java.lang.String)
for URLs that have a
host and that host is not listed as a known host.
If not set (the default) all URLs are encoded through the response.
redirectHosts
- one or more application hosts@Nullable public java.lang.String[] getRedirectHosts()
public void setRequestContextAttribute(@Nullable java.lang.String requestContextAttribute)
requestContextAttribute
- name of the RequestContext attributeAbstractView.setRequestContextAttribute(java.lang.String)
@Nullable protected java.lang.String getRequestContextAttribute()
public void setAttributes(java.util.Properties props)
java.util.Properties
object,
for all views returned by this resolver.
This is the most convenient way to set static attributes. Note that static attributes can be overridden by dynamic attributes, if a value with the same name is included in the model.
Can be populated with a String "value" (parsed via PropertiesEditor) or a "props" element in XML bean definitions.
public void setAttributesMap(@Nullable java.util.Map<java.lang.String,?> attributes)
Can be populated with a "map" or "props" element in XML bean definitions.
attributes
- a Map with name Strings as keys and attribute objects as valuesAbstractView.setAttributesMap(java.util.Map<java.lang.String, ?>)
public java.util.Map<java.lang.String,java.lang.Object> getAttributesMap()
Useful for specifying entries directly, for example via "attributesMap[myKey]". This is particularly useful for adding or overriding entries in child view definitions.
public void setExposePathVariables(@Nullable java.lang.Boolean exposePathVariables)
>The default setting is to let each View decide (see AbstractView.setExposePathVariables(boolean)
.
However, you can use this property to override that.
exposePathVariables
- true
- all Views resolved by this resolver will expose path variables
false
- no Views resolved by this resolver will expose path variables
null
- individual Views can decide for themselves (this is used by the default)
AbstractView.setExposePathVariables(boolean)
@Nullable protected java.lang.Boolean getExposePathVariables()
public void setExposeContextBeansAsAttributes(boolean exposeContextBeansAsAttributes)
This will make all such beans accessible in plain ${...}
expressions in a JSP 2.0 page, as well as in JSTL's c:out
value expressions.
Default is "false".
@Nullable protected java.lang.Boolean getExposeContextBeansAsAttributes()
public void setExposedContextBeanNames(@Nullable java.lang.String... exposedContextBeanNames)
@Nullable protected java.lang.String[] getExposedContextBeanNames()
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 setOrder(int order)
The default value is Ordered.LOWEST_PRECEDENCE
, meaning non-ordered.
Ordered.getOrder()
public int getOrder()
Ordered
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
in interface Ordered
Ordered.HIGHEST_PRECEDENCE
,
Ordered.LOWEST_PRECEDENCE
protected void initApplicationContext()
ApplicationObjectSupport
The default implementation is empty. Called by
ApplicationObjectSupport.initApplicationContext(org.springframework.context.ApplicationContext)
.
protected java.lang.Object getCacheKey(java.lang.String viewName, java.util.Locale locale)
getCacheKey
in class AbstractCachingViewResolver
protected View createView(java.lang.String viewName, java.util.Locale locale) throws java.lang.Exception
Not possible in loadView
, since overridden
loadView
versions in subclasses might rely on the
superclass always creating instances of the required view class.
createView
in class AbstractCachingViewResolver
viewName
- the name of the view to retrievelocale
- the Locale to retrieve the view fornull
if not found
(optional, to allow for ViewResolver chaining)java.lang.Exception
- if the view couldn't be resolvedloadView(java.lang.String, java.util.Locale)
,
requiredViewClass()
protected boolean canHandle(java.lang.String viewName, java.util.Locale locale)
ViewResolver
can
handle the supplied view name. If not, createView(String, java.util.Locale)
will
return null
. 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 View loadView(java.lang.String viewName, java.util.Locale locale) throws java.lang.Exception
buildView
for creating a new instance of the
specified view class. Applies the following Spring lifecycle methods
(as supported by the generic Spring bean factory):
setApplicationContext
afterPropertiesSet
loadView
in class AbstractCachingViewResolver
viewName
- the name of the view to retrievelocale
- the Locale to retrieve the view forjava.lang.Exception
- if the view couldn't be resolvedbuildView(String)
,
ApplicationContextAware.setApplicationContext(org.springframework.context.ApplicationContext)
,
InitializingBean.afterPropertiesSet()
protected AbstractUrlBasedView buildView(java.lang.String viewName) throws java.lang.Exception
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 buildjava.lang.Exception
- if the view couldn't be resolvedloadView(String, java.util.Locale)
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)ApplicationObjectSupport.getApplicationContext()
,
ApplicationContext.getAutowireCapableBeanFactory()
,
AutowireCapableBeanFactory.initializeBean(java.lang.Object, java.lang.String)