Class UrlBasedViewResolver

java.lang.Object
org.springframework.web.reactive.result.view.ViewResolversupport
org.springframework.web.reactive.result.view.UrlBasedViewResolver
All Implemented Interfaces:
Aware, InitializingBean, ApplicationContextAware, Ordered, ViewResolver
Direct Known subclasses:
FreeMarkerViewResolver, scriptTemplateViewResolver

public class UrlBasedViewResolver extends ViewResolversupport implements ViewResolver, ApplicationContextAware, InitializingBean
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 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. 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.

Note: This class does not support localized resolution, i.e. resolving a symbolic view name to different resources depending on the current locale.

since:
5.0
Author:
Rossen stoyanchev, sebastien Deleuze, Juergen Hoeller, sam Brannen
  • Field Details

    • REDIRECT_URL_PREFIX

      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.
      see Also:
  • Constructor Details

    • UrlBasedViewResolver

      public UrlBasedViewResolver()
  • Method Details

    • setViewClass

      public&nbsp;void&nbsp;setViewClass(@Nullable Class<?>&nbsp;viewClass)
      set 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:
    • getViewClass

      protected&nbsp;@Nullable Class<?>&nbsp;getViewClass()
      Return the view class to be used to create views.
      see Also:
    • setPrefix

      public&nbsp;void&nbsp;setPrefix(@Nullable string&nbsp;prefix)
      set the prefix that gets prepended to view names when building a URL.
    • getPrefix

      protected&nbsp;string&nbsp;getPrefix()
      Return the prefix that gets prepended to view names when building a URL.
    • setsuffix

      public&nbsp;void&nbsp;setsuffix(@Nullable string&nbsp;suffix)
      set the suffix that gets appended to view names when building a URL.
    • getsuffix

      protected&nbsp;string&nbsp;getsuffix()
      Return the suffix that gets appended to view names when building a URL.
    • setViewNames

      public&nbsp;void&nbsp;setViewNames(string @Nullable ...&nbsp;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:
    • getViewNames

      protected&nbsp;string @Nullable []&nbsp;getViewNames()
      Return the view names (or name patterns) that can be handled by this ViewResolver.
    • setRedirectViewProvider

      public&nbsp;void&nbsp;setRedirectViewProvider(Function<string, RedirectView>&nbsp;redirectViewProvider)
      URL based RedirectView provider which can be used to provide, for example, redirect views with a custom default status code.
    • setRequestContextAttribute

      public&nbsp;void&nbsp;setRequestContextAttribute(@Nullable string&nbsp;requestContextAttribute)
      set the name of the RequestContext attribute for all views.
      Parameters:
      requestContextAttribute - name of the RequestContext attribute
      see Also:
    • getRequestContextAttribute

      protected&nbsp;@Nullable string&nbsp;getRequestContextAttribute()
      Return the name of the RequestContext attribute for all views, if any.
    • setApplicationContext

      public&nbsp;void&nbsp;setApplicationContext(@Nullable ApplicationContext&nbsp;applicationContext)
      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.

      specified by:
      setApplicationContext&nbsp;in interface&nbsp;ApplicationContextAware
      Parameters:
      applicationContext - the ApplicationContext object to be used by this object
      see Also:
    • getApplicationContext

      public&nbsp;@Nullable ApplicationContext&nbsp;getApplicationContext()
      Return the containing ApplicationContext, if any.
      see Also:
    • afterPropertiesset

      public&nbsp;void&nbsp;afterPropertiesset() throws Exception
      Description copied from interface:&nbsp;InitializingBean
      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.

      specified by:
      afterPropertiesset&nbsp;in interface&nbsp;InitializingBean
      Throws:
      Exception - in the event of misconfiguration (such as failure to set an essential property) or if initialization fails for any other reason
    • resolveViewName

      public&nbsp;reactor.core.publisher.Mono<View>&nbsp;resolveViewName(string&nbsp;viewName, Locale&nbsp;locale)
      Description copied from interface:&nbsp;ViewResolver
      Resolve the view name to a View instance.
      specified by:
      resolveViewName&nbsp;in interface&nbsp;ViewResolver
      Parameters:
      viewName - the name of the view to resolve
      locale - the locale for the request
      Returns:
      the resolved view or an empty stream
    • canHandle

      protected&nbsp;boolean&nbsp;canHandle(string&nbsp;viewName, Locale&nbsp;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 - 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:
    • requiredViewClass

      protected&nbsp;Class<?>&nbsp;requiredViewClass()
      Return the required type of view for this resolver. This implementation returns AbstractUrlBasedView.
      see Also:
    • instantiateView

      protected&nbsp;AbstractUrlBasedView&nbsp;instantiateView()
      Instantiate 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:
    • createView

      protected&nbsp;AbstractUrlBasedView&nbsp;createView(string&nbsp;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 - the name of the view to build
      Returns:
      the View instance
      see Also:
    • applyLifecycleMethods

      protected&nbsp;View&nbsp;applyLifecycleMethods(string&nbsp;viewName, AbstractUrlBasedView&nbsp;view)
      Apply the containing ApplicationContext's lifecycle methods to the given View instance, 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 View instance to use (either the original one or a decorated variant)
      see Also: