Class AbstractUrlHandlerMapping

All Implemented Interfaces:
Aware, BeanNameAware, ApplicationContextAware, Ordered, ServletContextAware, MatchableHandlerMapping, HandlerMapping
Direct Known Subclasses:
AbstractDetectingUrlHandlerMapping, SimpleUrlHandlerMapping

public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping implements MatchableHandlerMapping
Abstract base class for URL-mapped HandlerMapping implementations.

Supports literal matches and pattern matches such as "/test/*", "/test/**", and others. For details on pattern syntax refer to PathPattern when parsed patterns are enabled or see AntPathMatcher otherwise. The syntax is largely the same but the PathPattern syntax is more tailored for web applications, and its implementation is more efficient.

All path patterns are checked in order to find the most exact match for the current request path where the "most exact" is the longest path pattern that matches the current request path.

Since:
16.04.2003
Author:
Juergen Hoeller, Arjen Poutsma
  • Constructor Details

    • AbstractUrlHandlerMapping

      public AbstractUrlHandlerMapping()
  • Method Details

    • setPatternParser

      public void setPatternParser(@Nullable PathPatternParser patternParser)
      Description copied from class: AbstractHandlerMapping
      Set the PathPatternParser to parse patterns with for URL path matching. Parsed patterns provide a more modern and efficient alternative to String path matching via AntPathMatcher.

      Note: This property is mutually exclusive with the below properties, all of which are not necessary for parsed patterns and are ignored when a PathPatternParser is available:

      By default, as of 6.0, this is set to a PathPatternParser instance with default settings and therefore use of parsed patterns is enabled. Set this to null to switch to String path matching via AntPathMatcher instead.

      Overrides:
      setPatternParser in class AbstractHandlerMapping
      Parameters:
      patternParser - the parser to use
    • setRootHandler

      public void setRootHandler(@Nullable Object rootHandler)
      Set the root handler for this handler mapping, that is, the handler to be registered for the root path ("/").

      Default is null, indicating no root handler.

    • getRootHandler

      @Nullable public Object getRootHandler()
      Return the root handler for this handler mapping (registered for "/"), or null if none.
    • setUseTrailingSlashMatch

      @Deprecated(since="6.0") public void setUseTrailingSlashMatch(boolean useTrailingSlashMatch)
      Whether to match to URLs irrespective of the presence of a trailing slash. If enabled a URL pattern such as "/users" also matches to "/users/".

      The default value is false.

    • useTrailingSlashMatch

      public boolean useTrailingSlashMatch()
      Whether to match to URLs irrespective of the presence of a trailing slash.
    • setLazyInitHandlers

      public void setLazyInitHandlers(boolean lazyInitHandlers)
      Set whether to lazily initialize handlers. Only applicable to singleton handlers, as prototypes are always lazily initialized. Default is "false", as eager initialization allows for more efficiency through referencing the controller objects directly.

      If you want to allow your controllers to be lazily initialized, make them "lazy-init" and set this flag to true. Just making them "lazy-init" will not work, as they are initialized through the references from the handler mapping in this case.

    • registerHandler

      public void registerHandler(String[] urlPaths, String beanName) throws BeansException, IllegalStateException
      Register the specified handler for the given URL paths.
      Parameters:
      urlPaths - the URLs that the bean should be mapped to
      beanName - the name of the handler bean
      Throws:
      BeansException - if the handler couldn't be registered
      IllegalStateException - if there is a conflicting handler registered
      Since:
      6.2
    • registerHandler

      public void registerHandler(String urlPath, Object handler) throws BeansException, IllegalStateException
      Register the specified handler for the given URL path.
      Parameters:
      urlPath - the URL the bean should be mapped to
      handler - the handler instance or handler bean name String (a bean name will automatically be resolved into the corresponding handler bean)
      Throws:
      BeansException - if the handler couldn't be registered
      IllegalStateException - if there is a conflicting handler registered
      Since:
      6.2
    • unregisterHandler

      public void unregisterHandler(String urlPath)
      Remove the mapping for the handler registered for the given URL path.
      Parameters:
      urlPath - the mapping to remove
      Since:
      6.2
    • getHandlerInternal

      @Nullable protected Object getHandlerInternal(HttpServletRequest request) throws Exception
      Look up a handler for the URL path of the given request.
      Specified by:
      getHandlerInternal in class AbstractHandlerMapping
      Parameters:
      request - current HTTP request
      Returns:
      the handler instance, or null if none found
      Throws:
      Exception - if there is an internal error
    • lookupHandler

      @Nullable protected Object lookupHandler(RequestPath path, String lookupPath, HttpServletRequest request) throws Exception
      Look up a handler instance for the given URL path. This method is used when parsed PathPatterns are enabled.
      Parameters:
      path - the parsed RequestPath
      lookupPath - the String lookupPath for checking direct hits
      request - current HTTP request
      Returns:
      a matching handler, or null if not found
      Throws:
      Exception
      Since:
      5.3
    • lookupHandler

      @Nullable protected Object lookupHandler(String lookupPath, HttpServletRequest request) throws Exception
      Look up a handler instance for the given URL path. This method is used when String pattern matching with PathMatcher is in use.
      Parameters:
      lookupPath - the path to match patterns against
      request - current HTTP request
      Returns:
      a matching handler, or null if not found
      Throws:
      Exception
      See Also:
    • validateHandler

      protected void validateHandler(Object handler, HttpServletRequest request) throws Exception
      Validate the given handler against the current request.

      The default implementation is empty. Can be overridden in subclasses, for example to enforce specific preconditions expressed in URL mappings.

      Parameters:
      handler - the handler object to validate
      request - current HTTP request
      Throws:
      Exception - if validation failed
    • buildPathExposingHandler

      protected Object buildPathExposingHandler(Object rawHandler, String bestMatchingPattern, String pathWithinMapping, @Nullable Map<String,String> uriTemplateVariables)
      Build a handler object for the given raw handler, exposing the actual handler, the HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, as well as the HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE before executing the handler.

      The default implementation builds a HandlerExecutionChain with a special interceptor that exposes the path attribute and URI template variables

      Parameters:
      rawHandler - the raw handler to expose
      pathWithinMapping - the path to expose before executing the handler
      uriTemplateVariables - the URI template variables, can be null if no variables found
      Returns:
      the final handler object
    • exposePathWithinMapping

      protected void exposePathWithinMapping(String bestMatchingPattern, String pathWithinMapping, HttpServletRequest request)
      Expose the path within the current mapping as request attribute.
      Parameters:
      pathWithinMapping - the path within the current mapping
      request - the request to expose the path to
      See Also:
    • exposeUriTemplateVariables

      protected void exposeUriTemplateVariables(Map<String,String> uriTemplateVariables, HttpServletRequest request)
      Expose the URI templates variables as request attribute.
      Parameters:
      uriTemplateVariables - the URI template variables
      request - the request to expose the path to
      See Also:
    • match

      @Nullable public RequestMatchResult match(HttpServletRequest request, String pattern)
      Description copied from interface: MatchableHandlerMapping
      Determine whether the request matches the given pattern. Use this method when MatchableHandlerMapping.getPatternParser() returns null which means that the HandlerMapping is using String pattern matching.
      Specified by:
      match in interface MatchableHandlerMapping
      Parameters:
      request - the current request
      pattern - the pattern to match
      Returns:
      the result from request matching, or null if none
    • getHandlerMap

      public final Map<String,Object> getHandlerMap()
      Return the handler mappings as a read-only Map, with the registered path or pattern as key and the handler object (or handler bean name in case of a lazy-init handler), as value.
      See Also:
    • getPathPatternHandlerMap

      public final Map<PathPattern,Object> getPathPatternHandlerMap()
      Identical to getHandlerMap() but populated when parsed patterns are enabled; otherwise empty.
      Since:
      5.3
    • supportsTypeLevelMappings

      protected boolean supportsTypeLevelMappings()
      Indicates whether this handler mapping support type-level mappings. Default to false.