org.springframework.web.servlet.handler

Class AbstractUrlHandlerMapping

    • Constructor Detail

      • AbstractUrlHandlerMapping

        public AbstractUrlHandlerMapping()
    • Method Detail

      • setRootHandler

        public void setRootHandler(@Nullable
                                   java.lang.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 java.lang.Object getRootHandler()
        Return the root handler for this handler mapping (registered for "/"), or null if none.
      • setUseTrailingSlashMatch

        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.

      • getHandlerInternal

        @Nullable
        protected java.lang.Object getHandlerInternal(HttpServletRequest request)
                                                         throws java.lang.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:
        java.lang.Exception - if there is an internal error
      • lookupHandler

        @Nullable
        protected java.lang.Object lookupHandler(java.lang.String urlPath,
                                                           HttpServletRequest request)
                                                    throws java.lang.Exception
        Look up a handler instance for the given URL path.

        Supports direct matches, e.g. a registered "/test" matches "/test", and various Ant-style pattern matches, e.g. a registered "/t*" matches both "/test" and "/team". For details, see the AntPathMatcher class.

        Looks for the most exact pattern, where most exact is defined as the longest path pattern.

        Parameters:
        urlPath - the URL the bean is mapped to
        request - current HTTP request (to expose the path within the mapping to)
        Returns:
        the associated handler instance, or null if not found
        Throws:
        java.lang.Exception
        See Also:
        exposePathWithinMapping(java.lang.String, java.lang.String, javax.servlet.http.HttpServletRequest), AntPathMatcher
      • validateHandler

        protected void validateHandler(java.lang.Object handler,
                                       HttpServletRequest request)
                                throws java.lang.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:
        java.lang.Exception - if validation failed
      • buildPathExposingHandler

        protected java.lang.Object buildPathExposingHandler(java.lang.Object rawHandler,
                                                            java.lang.String bestMatchingPattern,
                                                            java.lang.String pathWithinMapping,
                                                            @Nullable
                                                            java.util.Map<java.lang.String,java.lang.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(java.lang.String bestMatchingPattern,
                                               java.lang.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:
        HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE
      • exposeUriTemplateVariables

        protected void exposeUriTemplateVariables(java.util.Map<java.lang.String,java.lang.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:
        HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE
      • registerHandler

        protected void registerHandler(java.lang.String[] urlPaths,
                                       java.lang.String beanName)
                                throws BeansException,
                                       java.lang.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
        java.lang.IllegalStateException - if there is a conflicting handler registered
      • registerHandler

        protected void registerHandler(java.lang.String urlPath,
                                       java.lang.Object handler)
                                throws BeansException,
                                       java.lang.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
        java.lang.IllegalStateException - if there is a conflicting handler registered
      • getHandlerMap

        public final java.util.Map<java.lang.String,java.lang.Object> getHandlerMap()
        Return the registered handlers as an unmodifiable Map, with the registered path as key and the handler object (or handler bean name in case of a lazy-init handler) as value.
        See Also:
        AbstractHandlerMapping.getDefaultHandler()
      • supportsTypeLevelMappings

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