Class UrlBasedViewResolver
- All Implemented Interfaces:
- Aware,- ApplicationContextAware,- Ordered,- ServletContextAware,- ViewResolver
- Direct Known Subclasses:
- AbstractTemplateViewResolver,- InternalResourceViewResolver,- ScriptTemplateViewResolver,- XsltViewResolver
ViewResolver
 interface, allowing for direct resolution of symbolic view names to URLs,
 without explicit mapping definitions. 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. 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.
Furthermore, forward URLs can be specified via the "forward:" prefix. For example: "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 a View for any given view name;
 as a consequence, it should be configured as the last ViewResolver in the chain.
- Since:
- 13.12.2003
- Author:
- Juergen Hoeller, Rob Harrop, Sam Brannen
- See Also:
- 
Nested Class SummaryNested classes/interfaces inherited from class org.springframework.web.servlet.view.AbstractCachingViewResolverAbstractCachingViewResolver.CacheFilter
- 
Field SummaryFieldsModifier and TypeFieldDescriptionstatic final StringPrefix for special view names that specify a forward URL (usually to a controller after a form has been submitted and processed).static 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 org.springframework.web.servlet.view.AbstractCachingViewResolverDEFAULT_CACHE_LIMITFields inherited from class org.springframework.context.support.ApplicationObjectSupportloggerFields inherited from interface org.springframework.core.OrderedHIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionprotected ViewapplyLifecycleMethods(String viewName, AbstractUrlBasedView view) Apply the containingApplicationContext's lifecycle methods to the givenViewinstance, if such a context is available.protected AbstractUrlBasedViewCreates a new View instance of the specified view class and configures it.protected booleanIndicates whether thisViewResolvercan handle the supplied view name.createView(String viewName, Locale locale) Overridden to implement check for "redirect:" prefix.AllowMapaccess to the static attributes for views returned by this resolver, with the option to add or override specific entries.protected ObjectgetCacheKey(String viewName, Locale locale) This implementation returns just the view name, as this ViewResolver doesn't support localized resolution.Return the content type for all views, if any.Return whether views resolved by this resolver should add path variables to the model or not.intgetOrder()Get the order value of this object.protected StringReturn the prefix that gets prepended to view names when building a URL.Return the configured application hosts for redirect purposes.Return the name of the RequestContext attribute 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 voidSubclasses can override this for custom initialization behavior.protected AbstractUrlBasedViewInstantiate the specified view class.protected booleanReturn whether to interpret a given redirect URL that starts with a slash ("/") as relative to the current ServletContext, i.e.protected booleanReturn whether redirects should stay compatible with HTTP 1.0 clients.Delegates tobuildViewfor creating a new instance of the specified view class.protected Class<?>Return the required type of view for this resolver.voidsetAttributes(Properties props) Set static attributes from ajava.util.Propertiesobject, for all views returned by this resolver.voidsetAttributesMap(@Nullable Map<String, ?> attributes) Set static attributes from a Map, for all views returned by this resolver.voidsetContentType(@Nullable String contentType) Set the content type for all views — for example,"text/html;charset=UTF-8".voidsetExposeContextBeansAsAttributes(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.voidsetExposedContextBeanNames(String @Nullable ... exposedContextBeanNames) Specify the names of beans in the context which are supposed to be exposed.voidsetExposePathVariables(@Nullable Boolean exposePathVariables) Specify whether views resolved by this resolver should add path variables to the model or not.voidsetOrder(int order) Specify the order value for this ViewResolver bean.voidSet the prefix that gets prepended to view names when building a URL.voidsetRedirectContextRelative(boolean redirectContextRelative) Set whether to interpret a given redirect URL that starts with a slash ("/") as relative to the current ServletContext, i.e.voidsetRedirectHosts(String @Nullable ... redirectHosts) Configure one or more hosts associated with the application.voidsetRedirectHttp10Compatible(boolean redirectHttp10Compatible) Set whether redirects should stay compatible with HTTP 1.0 clients.voidsetRequestContextAttribute(@Nullable String requestContextAttribute) Set the name of the RequestContext attribute 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 org.springframework.web.servlet.view.AbstractCachingViewResolverclearCache, getCacheFilter, getCacheLimit, isCache, isCacheUnresolved, removeFromCache, resolveViewName, setCache, setCacheFilter, setCacheLimit, setCacheUnresolvedMethods inherited from class org.springframework.web.context.support.WebApplicationObjectSupportgetServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContextMethods inherited from class org.springframework.context.support.ApplicationObjectSupportgetApplicationContext, getMessageSourceAccessor, obtainApplicationContext, requiredContextClass, setApplicationContext
- 
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:
 
- 
FORWARD_URL_PREFIXPrefix for special view names that specify a forward 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- 
setViewClassSet the view class that should be used to create views.- Parameters:
- viewClass- a class that is assignable to the required view class (by default: AbstractUrlBasedView)
- See Also:
 
- 
getViewClassReturn the view class to be used to create views.- See Also:
 
- 
setPrefixSet the prefix that gets prepended to view names when building a URL.
- 
getPrefixReturn the prefix that gets prepended to view names when building a URL.
- 
setSuffixSet the suffix that gets appended to view names when building a URL.
- 
getSuffixReturn the suffix that gets appended to view names when building a URL.
- 
setContentTypeSet the content type for all views — for example,"text/html;charset=UTF-8".May be ignored by view classes if the view itself is assumed to set the content type — for example, in case of JSPs. 
- 
getContentTypeReturn the content type for all views, if any.
- 
setRedirectContextRelativepublic 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. as relative to the web application root.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. For example: "redirect:myAction" 
- 
isRedirectContextRelativeprotected boolean isRedirectContextRelative()Return whether to interpret a given redirect URL that starts with a slash ("/") as relative to the current ServletContext, i.e. as relative to the web application root.
- 
setRedirectHttp10Compatiblepublic void setRedirectHttp10Compatible(boolean redirectHttp10Compatible) Set whether redirects should stay compatible with HTTP 1.0 clients.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. For example: "redirect:myAction" 
- 
isRedirectHttp10Compatibleprotected boolean isRedirectHttp10Compatible()Return whether redirects should stay compatible with HTTP 1.0 clients.
- 
setRedirectHostsConfigure one or more hosts associated with the application. All other hosts will be considered external hosts.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. - Parameters:
- redirectHosts- one or more application hosts
- Since:
- 4.3
 
- 
getRedirectHostsReturn the configured application hosts for redirect purposes.- Since:
- 4.3
 
- 
setRequestContextAttributeSet the name of the RequestContext attribute for all views.- Parameters:
- requestContextAttribute- name of the RequestContext attribute
- See Also:
 
- 
getRequestContextAttributeReturn the name of the RequestContext attribute for all views, if any.
- 
setAttributesSet static attributes from ajava.util.Propertiesobject, 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. 
- 
setAttributesMapSet static attributes from a Map, for all views returned by this resolver. This allows to set any kind of attribute values, for example bean references.Can be populated with a "map" or "props" element in XML bean definitions. - Parameters:
- attributes- a Map with name Strings as keys and attribute objects as values
- See Also:
 
- 
getAttributesMapAllowMapaccess to the static attributes for views returned by this resolver, with the option to add or override specific entries.Useful for specifying entries directly, for example via attributesMap[myKey]. This is particularly useful for adding or overriding entries in child view definitions.
- 
setExposePathVariablesSpecify whether views resolved by this resolver should add path variables to the model or not.The default setting is to let each View decide (see AbstractView.setExposePathVariables(boolean)). However, you can use this property to override that.- Parameters:
- 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 default)
 
- See Also:
 
- 
getExposePathVariablesReturn whether views resolved by this resolver should add path variables to the model or not.
- 
setExposeContextBeansAsAttributespublic 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.This will make all such beans accessible in plain ${...}expressions in a JSP 2.0 page, as well as in JSTL'sc:outvalue expressions.Default is "false". 
- 
getExposeContextBeansAsAttributes
- 
setExposedContextBeanNamesSpecify the names of beans in the context which are supposed to be exposed. If this is non-null, only the specified beans are eligible for exposure as attributes.
- 
getExposedContextBeanNames
- 
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'.
- 
getViewNamesReturn the view names (or name patterns) that can be handled by thisViewResolver.
- 
setOrderpublic void setOrder(int order) Specify the order value for this ViewResolver bean.The default value is Ordered.LOWEST_PRECEDENCE, meaning non-ordered.- See Also:
 
- 
getOrderpublic int getOrder()Description copied from interface:OrderedGet the order value of this object.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-startupvalues).Same order values will result in arbitrary sort positions for the affected objects. 
- 
initApplicationContextprotected void initApplicationContext()Description copied from class:ApplicationObjectSupportSubclasses can override this for custom initialization behavior.The default implementation is empty. Called by ApplicationObjectSupport.initApplicationContext(ApplicationContext).
- 
getCacheKeyThis implementation returns just the view name, as this ViewResolver doesn't support localized resolution.- Overrides:
- getCacheKeyin class- AbstractCachingViewResolver
 
- 
createViewOverridden to implement check for "redirect:" prefix.Not possible in loadView, since overriddenloadViewversions in subclasses might rely on the superclass always creating instances of the required view class.- Overrides:
- createViewin class- AbstractCachingViewResolver
- Parameters:
- viewName- the name of the view to retrieve
- locale- the Locale to retrieve the view for
- Returns:
- the View instance, or nullif not found (optional, to allow for ViewResolver chaining)
- Throws:
- Exception- if the view couldn't be resolved
- See Also:
 
- 
canHandleIndicates whether thisViewResolvercan handle the supplied view name. If not,createView(String, java.util.Locale)will returnnull. 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:
 
- 
loadViewDelegates tobuildViewfor creating a new instance of the specified view class. Applies the following Spring lifecycle methods (as supported by the generic Spring bean factory):- ApplicationContextAware's setApplicationContext
- InitializingBean's afterPropertiesSet
 - Specified by:
- loadViewin class- AbstractCachingViewResolver
- Parameters:
- viewName- the name of the view to retrieve
- locale- the Locale to retrieve the view for
- Returns:
- the View instance
- Throws:
- Exception- if the view couldn't be resolved
- See Also:
 
- ApplicationContextAware's 
- 
buildViewCreates 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; those will be applied by the loadViewmethod after this method returns.Subclasses will typically call super.buildView(viewName)first, before setting further properties themselves.loadViewwill then apply Spring lifecycle methods at the end of this process.- Parameters:
- viewName- the name of the view to build
- Returns:
- the View instance
- Throws:
- Exception- if the view couldn't be resolved
- 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)
- Since:
- 5.0
- See Also:
 
 
-