Top Description Fields Constructors Methods
org.springframework.web.reactive.result.view

public Class UrlBasedViewResolver

extends ViewResolverSupport
implements ViewResolver, ApplicationContextAware, InitializingBean
Class Inheritance
All Implemented Interfaces
org.springframework.beans.factory.InitializingBean, org.springframework.context.ApplicationContextAware, org.springframework.beans.factory.Aware, org.springframework.web.reactive.result.view.ViewResolver
Known Direct Subclasses
org.springframework.web.reactive.result.view.freemarker.FreeMarkerViewResolver, org.springframework.web.reactive.result.view.script.ScriptTemplateViewResolver
Imports
java.util.Locale, java.util.function.Function, reactor.core.publisher.Mono, org.springframework.beans.BeanUtils, org.springframework.beans.factory.InitializingBean, org.springframework.context.ApplicationContext, .ApplicationContextAware, org.springframework.lang.Nullable, org.springframework.util.Assert, .PatternMatchUtils

A 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 org.springframework.web.reactive.result.view.freemarker.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.

Authors
Rossen Stoyanchev, Sebastien Deleuze, Juergen Hoeller, Sam Brannen
Since
5.0

Field Summary

Modifier and TypeField and Description
private ApplicationContext
private String
public static final 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 Function<String, RedirectView>
private String
private String
private Class<?>
private String[]
Inherited from org.springframework.web.reactive.result.view.ViewResolverSupport:
DEFAULT_CONTENT_TYPE

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
afterPropertiesSet()

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Invoked by the containing 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.

protected View

Returns:

the View instance to use (either the original one or a decorated variant)
applyLifecycleMethods
(String
the name of the view
viewName
,
AbstractUrlBasedView
the freshly created View instance, pre-configured with AbstractUrlBasedView's properties
view
)

Apply the containing ApplicationContext's lifecycle methods to the given View instance, if such a context is available.

protected boolean

Returns:

whether this resolver applies to the specified view
canHandle
(String
the name of the view to retrieve
viewName
,
Locale
the Locale to retrieve the view for
locale
)

Indicates whether this ViewResolver can handle the supplied view name.

protected AbstractUrlBasedView

Returns:

the View instance
createView
(String
the name of the view to build
viewName
)

Creates a new View instance of the specified view class and configures it.

public ApplicationContext
getApplicationContext()

Return the containing ApplicationContext, if any.

protected String
getPrefix()

Return the prefix that gets prepended to view names when building a URL.

protected String
getRequestContextAttribute()

Return the name of the RequestContext attribute for all views, if any.

protected String
getSuffix()

Return the suffix that gets appended to view names when building a URL.

protected Class<?>
getViewClass()

Return the view class to be used to create views.

protected String[]
getViewNames()

Return the view names (or name patterns) that can be handled by this ViewResolver.

protected AbstractUrlBasedView

Returns:

a new instance of the view class
instantiateView
()

Instantiate the specified view class.

protected Class<?>
requiredViewClass()

Return the required type of view for this resolver.

public Mono<View>
resolveViewName(String
the name of the view to resolve
viewName
,
Locale
the locale for the request
locale
)

Implements org.springframework.web.reactive.result.view.ViewResolver.resolveViewName.

Resolve the view name to a View instance.

public void
setApplicationContext(ApplicationContext
the ApplicationContext object to be used by this object
applicationContext
)

Implements org.springframework.context.ApplicationContextAware.setApplicationContext.

Accept the containing ApplicationContext, if any.

public void
setPrefix(String prefix)

Set the prefix that gets prepended to view names when building a URL.

public void
setRedirectViewProvider(Function<String, RedirectView> redirectViewProvider)

URL based RedirectView provider which can be used to provide, for example, redirect views with a custom default status code.

public void
setRequestContextAttribute(String
name of the RequestContext attribute
requestContextAttribute
)

Set the name of the RequestContext attribute for all views.

public void
setSuffix(String suffix)

Set the suffix that gets appended to view names when building a URL.

public void
setViewClass(Class<?>
a class that is assignable to the required view class (by default: AbstractUrlBasedView)
viewClass
)

Set the view class that should be used to create views.

public void
setViewNames(String... viewNames)

Set the view names (or name patterns) that can be handled by this ViewResolver.

Inherited from org.springframework.web.reactive.result.view.ViewResolverSupport:
getDefaultCharsetgetOrdergetSupportedMediaTypessetDefaultCharsetsetOrdersetSupportedMediaTypes

Field Detail

applicationContextback to summary
private ApplicationContext applicationContext
Annotations
@Nullable
prefixback to summary
private String prefix
REDIRECT_URL_PREFIXback to summary
public static final 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). Such view names will not be resolved in the configured default way but rather be treated as special shortcut.

redirectViewProviderback to summary
private Function<String, RedirectView> redirectViewProvider
requestContextAttributeback to summary
private String requestContextAttribute
Annotations
@Nullable
suffixback to summary
private String suffix
viewClassback to summary
private Class<?> viewClass
Annotations
@Nullable
viewNamesback to summary
private String[] viewNames
Annotations
@Nullable

Constructor Detail

UrlBasedViewResolverback to summary
public UrlBasedViewResolver()

Method Detail

afterPropertiesSetback to summary
public void afterPropertiesSet() throws Exception

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Doc from org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Invoked by the containing 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.

Annotations
@Override
Exceptions
Exception:
in the event of misconfiguration (such as failure to set an essential property) or if initialization fails for any other reason
applyLifecycleMethodsback to summary
protected View applyLifecycleMethods(String viewName, AbstractUrlBasedView view)

Apply the containing ApplicationContext's lifecycle methods to the given View instance, if such a context is available.

Parameters
viewName:String

the name of the view

view:AbstractUrlBasedView

the freshly created View instance, pre-configured with AbstractUrlBasedView's properties

Returns:View

the View instance to use (either the original one or a decorated variant)

See Also
getApplicationContext(), ApplicationContext#getAutowireCapableBeanFactory(), org.springframework.beans.factory.config.AutowireCapableBeanFactory#initializeBean
canHandleback to summary
protected boolean canHandle(String viewName, Locale locale)

Indicates whether this ViewResolver can handle the supplied view name. If not, an empty result is returned. The default implementation checks against the configured view names.

Parameters
viewName:String

the name of the view to retrieve

locale:Locale

the Locale to retrieve the view for

Returns:boolean

whether this resolver applies to the specified view

See Also
org.springframework.util.PatternMatchUtils#simpleMatch(String, String)
createViewback to summary
protected AbstractUrlBasedView createView(String viewName)

Creates 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 ApplicationContext is available.

Parameters
viewName:String

the name of the view to build

Returns:AbstractUrlBasedView

the View instance

See Also
getViewClass(), applyLifecycleMethods
getApplicationContextback to summary
public ApplicationContext getApplicationContext()

Return the containing ApplicationContext, if any.

Annotations
@Nullable
See Also
setApplicationContext
getPrefixback to summary
protected String getPrefix()

Return the prefix that gets prepended to view names when building a URL.

getRequestContextAttributeback to summary
protected String getRequestContextAttribute()

Return the name of the RequestContext attribute for all views, if any.

Annotations
@Nullable
getSuffixback to summary
protected String getSuffix()

Return the suffix that gets appended to view names when building a URL.

getViewClassback to summary
protected Class<?> getViewClass()

Return the view class to be used to create views.

Annotations
@Nullable
See Also
setViewClass
getViewNamesback to summary
protected String[] getViewNames()

Return the view names (or name patterns) that can be handled by this ViewResolver.

Annotations
@Nullable
instantiateViewback to summary
protected AbstractUrlBasedView instantiateView()

Instantiate the specified view class.

The default implementation uses reflection to instantiate the class.

Returns:AbstractUrlBasedView

a new instance of the view class

Since
5.3
See Also
setViewClass
requiredViewClassback to summary
protected Class<?> requiredViewClass()

Return the required type of view for this resolver. This implementation returns AbstractUrlBasedView.

See Also
instantiateView(), AbstractUrlBasedView
resolveViewNameback to summary
public Mono<View> resolveViewName(String viewName, Locale locale)

Implements org.springframework.web.reactive.result.view.ViewResolver.resolveViewName.

Doc from org.springframework.web.reactive.result.view.ViewResolver.resolveViewName.

Resolve the view name to a View instance.

Parameters
viewName:String

the name of the view to resolve

locale:Locale

the locale for the request

Returns:Mono<View>

the resolved view or an empty stream

Annotations
@Override
setApplicationContextback to summary
public void setApplicationContext(ApplicationContext applicationContext)

Implements org.springframework.context.ApplicationContextAware.setApplicationContext.

Accept the containing ApplicationContext, if any.

To be used for the initialization of newly created View instances, applying lifecycle callbacks and providing access to the containing environment.

Parameters
applicationContext:ApplicationContext

Doc from org.springframework.context.ApplicationContextAware.setApplicationContext.

the ApplicationContext object to be used by this object

Annotations
@Override
See Also
setViewClass, createView, applyLifecycleMethods
setPrefixback to summary
public void setPrefix(String prefix)

Set the prefix that gets prepended to view names when building a URL.

setRedirectViewProviderback to summary
public void setRedirectViewProvider(Function<String, RedirectView> redirectViewProvider)

URL based RedirectView provider which can be used to provide, for example, redirect views with a custom default status code.

setRequestContextAttributeback to summary
public void setRequestContextAttribute(String requestContextAttribute)

Set the name of the RequestContext attribute for all views.

Parameters
requestContextAttribute:String

name of the RequestContext attribute

See Also
AbstractView#setRequestContextAttribute
setSuffixback to summary
public void setSuffix(String suffix)

Set the suffix that gets appended to view names when building a URL.

setViewClassback to summary
public void setViewClass(Class<?> viewClass)

Set the view class that should be used to create views.

Parameters
viewClass:Class<?>

a class that is assignable to the required view class (by default: AbstractUrlBasedView)

See Also
requiredViewClass(), instantiateView(), AbstractUrlBasedView
setViewNamesback to summary
public void setViewNames(String... viewNames)

Set the view names (or name patterns) that can be handled by this ViewResolver. View names can contain simple wildcards such that 'my*', '*Report' and '*Repo*' will all match the view name 'myReport'.

See Also
canHandle