org.springframework.web.servlet.view

Class DefaultRequestToViewNameTranslator

  • java.lang.Object
    • org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator
  • All Implemented Interfaces:
    RequestToViewNameTranslator


    public class DefaultRequestToViewNameTranslator
    extends java.lang.Object
    implements RequestToViewNameTranslator
    RequestToViewNameTranslator that simply transforms the URI of the incoming request into a view name.

    Can be explicitly defined as the viewNameTranslator bean in a DispatcherServlet context. Otherwise, a plain default instance will be used.

    The default transformation simply strips leading and trailing slashes as well as the file extension of the URI, and returns the result as the view name with the configured prefix and a suffix added as appropriate.

    The stripping of the leading slash and file extension can be disabled using the stripLeadingSlash and stripExtension properties, respectively.

    Find below some examples of request to view name translation.

    • http://localhost:8080/gamecast/display.php » display
    • http://localhost:8080/gamecast/displayShoppingCart.php » displayShoppingCart
    • http://localhost:8080/gamecast/admin/index.php » admin/index
    Since:
    2.0
    Author:
    Rob Harrop, Juergen Hoeller
    See Also:
    RequestToViewNameTranslator, ViewResolver
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method and Description
      java.lang.String getViewName(HttpServletRequest request)
      Translates the request URI of the incoming HttpServletRequest into the view name based on the configured parameters.
      void setAlwaysUseFullPath(boolean alwaysUseFullPath)
      Shortcut to same property on underlying UrlPathHelper.
      void setPrefix(java.lang.String prefix)
      Set the prefix to prepend to generated view names.
      void setRemoveSemicolonContent(boolean removeSemicolonContent)
      Set if ";" (semicolon) content should be stripped from the request URI.
      void setSeparator(java.lang.String separator)
      Set the value that will replace '/' as the separator in the view name.
      void setStripExtension(boolean stripExtension)
      Set whether or not file extensions should be stripped from the URI when generating the view name.
      void setStripLeadingSlash(boolean stripLeadingSlash)
      Set whether or not leading slashes should be stripped from the URI when generating the view name.
      void setStripTrailingSlash(boolean stripTrailingSlash)
      Set whether or not trailing slashes should be stripped from the URI when generating the view name.
      void setSuffix(java.lang.String suffix)
      Set the suffix to append to generated view names.
      void setUrlDecode(boolean urlDecode)
      Shortcut to same property on underlying UrlPathHelper.
      void setUrlPathHelper(UrlPathHelper urlPathHelper)
      Set the UrlPathHelper to use for the resolution of lookup paths.
      protected java.lang.String transformPath(java.lang.String lookupPath)
      Transform the request URI (in the context of the webapp) stripping slashes and extensions, and replacing the separator as required.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • DefaultRequestToViewNameTranslator

        public DefaultRequestToViewNameTranslator()
    • Method Detail

      • setPrefix

        public void setPrefix(@Nullable
                              java.lang.String prefix)
        Set the prefix to prepend to generated view names.
        Parameters:
        prefix - the prefix to prepend to generated view names
      • setSuffix

        public void setSuffix(@Nullable
                              java.lang.String suffix)
        Set the suffix to append to generated view names.
        Parameters:
        suffix - the suffix to append to generated view names
      • setSeparator

        public void setSeparator(java.lang.String separator)
        Set the value that will replace '/' as the separator in the view name. The default behavior simply leaves '/' as the separator.
      • setStripLeadingSlash

        public void setStripLeadingSlash(boolean stripLeadingSlash)
        Set whether or not leading slashes should be stripped from the URI when generating the view name. Default is "true".
      • setStripTrailingSlash

        public void setStripTrailingSlash(boolean stripTrailingSlash)
        Set whether or not trailing slashes should be stripped from the URI when generating the view name. Default is "true".
      • setStripExtension

        public void setStripExtension(boolean stripExtension)
        Set whether or not file extensions should be stripped from the URI when generating the view name. Default is "true".
      • setUrlPathHelper

        public void setUrlPathHelper(UrlPathHelper urlPathHelper)
        Set the UrlPathHelper to use for the resolution of lookup paths.

        Use this to override the default UrlPathHelper with a custom subclass, or to share common UrlPathHelper settings across multiple web components.

      • transformPath

        @Nullable
        protected java.lang.String transformPath(java.lang.String lookupPath)
        Transform the request URI (in the context of the webapp) stripping slashes and extensions, and replacing the separator as required.
        Parameters:
        lookupPath - the lookup path for the current request, as determined by the UrlPathHelper
        Returns:
        the transformed path, with slashes and extensions stripped if desired