Class AbstractUrlHandlerMapping
- All Implemented Interfaces:
- Aware,- BeanNameAware,- ApplicationContextAware,- Ordered,- ServletContextAware,- MatchableHandlerMapping,- HandlerMapping
- Direct Known Subclasses:
- AbstractDetectingUrlHandlerMapping,- SimpleUrlHandlerMapping
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
- 
Field SummaryFields inherited from class org.springframework.web.servlet.handler.AbstractHandlerMappingmappingsLoggerFields inherited from class org.springframework.context.support.ApplicationObjectSupportloggerFields inherited from interface org.springframework.web.servlet.HandlerMappingBEST_MATCHING_HANDLER_ATTRIBUTE, BEST_MATCHING_PATTERN_ATTRIBUTE, INTROSPECT_TYPE_LEVEL_MAPPING, LOOKUP_PATH, MATRIX_VARIABLES_ATTRIBUTE, PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, URI_TEMPLATE_VARIABLES_ATTRIBUTEFields inherited from interface org.springframework.core.OrderedHIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionprotected ObjectbuildPathExposingHandler(Object rawHandler, String bestMatchingPattern, String pathWithinMapping, Map<String, String> uriTemplateVariables) Build a handler object for the given raw handler, exposing the actual handler, theHandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, as well as theHandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTEbefore executing the handler.protected voidexposePathWithinMapping(String bestMatchingPattern, String pathWithinMapping, HttpServletRequest request) Expose the path within the current mapping as request attribute.protected voidexposeUriTemplateVariables(Map<String, String> uriTemplateVariables, HttpServletRequest request) Expose the URI templates variables as request attribute.protected ObjectgetHandlerInternal(HttpServletRequest request) Look up a handler for the URL path of the given request.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.final Map<PathPattern,Object> Identical togetHandlerMap()but populated when parsed patterns areenabled; otherwise empty.Return the root handler for this handler mapping (registered for "/"), ornullif none.protected ObjectlookupHandler(String lookupPath, HttpServletRequest request) Look up a handler instance for the given URL path.protected ObjectlookupHandler(RequestPath path, String lookupPath, HttpServletRequest request) Look up a handler instance for the given URL path.match(HttpServletRequest request, String pattern) Determine whether the request matches the given pattern.voidregisterHandler(String[] urlPaths, String beanName) Register the specified handler for the given URL paths.voidregisterHandler(String urlPath, Object handler) Register the specified handler for the given URL path.voidsetLazyInitHandlers(boolean lazyInitHandlers) Set whether to lazily initialize handlers.voidsetPatternParser(PathPatternParser patternParser) Set thePathPatternParserto parsepatternswith for URL path matching.voidsetRootHandler(Object rootHandler) Set the root handler for this handler mapping, that is, the handler to be registered for the root path ("/").voidsetUseTrailingSlashMatch(boolean useTrailingSlashMatch) Deprecated.protected booleanIndicates whether this handler mapping support type-level mappings.voidunregisterHandler(String urlPath) Remove the mapping for the handler registered for the given URL path.booleanWhether to match to URLs irrespective of the presence of a trailing slash.protected voidvalidateHandler(Object handler, HttpServletRequest request) Validate the given handler against the current request.Methods inherited from class org.springframework.web.servlet.handler.AbstractHandlerMappingadaptInterceptor, detectMappedInterceptors, extendInterceptors, formatMappingName, getAdaptedInterceptors, getCorsConfiguration, getCorsConfigurationSource, getCorsHandlerExecutionChain, getCorsProcessor, getDefaultHandler, getHandler, getHandlerExecutionChain, getMappedInterceptors, getOrder, getPathMatcher, getPatternParser, getUrlPathHelper, hasCorsConfigurationSource, initApplicationContext, initInterceptors, initLookupPath, setAlwaysUseFullPath, setBeanName, setCorsConfigurations, setCorsConfigurationSource, setCorsProcessor, setDefaultHandler, setInterceptors, setOrder, setPathMatcher, setRemoveSemicolonContent, setUrlDecode, setUrlPathHelper, usesPathPatternsMethods inherited from class org.springframework.web.context.support.WebApplicationObjectSupportgetServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContextMethods inherited from class org.springframework.context.support.ApplicationObjectSupportgetApplicationContext, getMessageSourceAccessor, obtainApplicationContext, requiredContextClass, setApplicationContextMethods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.web.servlet.HandlerMappinggetHandler, usesPathPatternsMethods inherited from interface org.springframework.web.servlet.handler.MatchableHandlerMappinggetPatternParser
- 
Constructor Details- 
AbstractUrlHandlerMappingpublic AbstractUrlHandlerMapping()
 
- 
- 
Method Details- 
setPatternParserDescription copied from class:AbstractHandlerMappingSet thePathPatternParserto parsepatternswith for URL path matching. Parsed patterns provide a more modern and efficient alternative to String path matching viaAntPathMatcher.Note: This property is mutually exclusive with the below properties, all of which are not necessary for parsed patterns and are ignored when a PathPatternParseris available:- AbstractHandlerMapping.setAlwaysUseFullPath(boolean)-- parsed patterns always use the full path and consider the servletPath only when a Servlet is mapped by path prefix.
- AbstractHandlerMapping.setRemoveSemicolonContent(boolean)-- parsed patterns always ignore semicolon content for path matching purposes, but path parameters remain available for use in controllers via- @MatrixVariable.
- AbstractHandlerMapping.setUrlDecode(boolean)-- parsed patterns match one decoded path segment at a time and therefore don't need to decode the full path.
- AbstractHandlerMapping.setUrlPathHelper(org.springframework.web.util.UrlPathHelper)-- for parsed patterns, the request path is parsed once in- DispatcherServletor in- ServletRequestPathFilterusing- ServletRequestPathUtilsand cached in a request attribute.
- AbstractHandlerMapping.setPathMatcher(org.springframework.util.PathMatcher)-- a parsed patterns encapsulates the logic for path matching and does need a- PathMatcher.
 By default, as of 6.0, this is set to a PathPatternParserinstance with default settings and therefore use of parsed patterns is enabled. Set this tonullto switch to String path matching viaAntPathMatcherinstead.- Overrides:
- setPatternParserin class- AbstractHandlerMapping
- Parameters:
- patternParser- the parser to use
 
- 
setRootHandlerSet 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.
- 
getRootHandlerReturn the root handler for this handler mapping (registered for "/"), ornullif none.
- 
setUseTrailingSlashMatchDeprecated.as of 6.0, seePathPatternParser.setMatchOptionalTrailingSeparator(boolean)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.
- 
useTrailingSlashMatchpublic boolean useTrailingSlashMatch()Whether to match to URLs irrespective of the presence of a trailing slash.
- 
setLazyInitHandlerspublic 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. 
- 
registerHandlerpublic 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
 
- 
registerHandlerpublic 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
 
- 
unregisterHandlerRemove the mapping for the handler registered for the given URL path.- Parameters:
- urlPath- the mapping to remove
- Since:
- 6.2
 
- 
getHandlerInternalLook up a handler for the URL path of the given request.- Specified by:
- getHandlerInternalin class- AbstractHandlerMapping
- Parameters:
- request- current HTTP request
- Returns:
- the handler instance, or nullif 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 parsedPathPatterns areenabled.- Parameters:
- path- the parsed RequestPath
- lookupPath- the String lookupPath for checking direct hits
- request- current HTTP request
- Returns:
- a matching handler, or nullif 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 withPathMatcheris in use.- Parameters:
- lookupPath- the path to match patterns against
- request- current HTTP request
- Returns:
- a matching handler, or nullif not found
- Throws:
- Exception
- See Also:
 
- 
validateHandlerValidate 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
 
- 
buildPathExposingHandlerprotected 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, theHandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, as well as theHandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTEbefore executing the handler.The default implementation builds a HandlerExecutionChainwith 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- nullif no variables found
- Returns:
- the final handler object
 
- 
exposePathWithinMappingprotected 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:
 
- 
exposeUriTemplateVariablesprotected 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:
 
- 
matchDescription copied from interface:MatchableHandlerMappingDetermine whether the request matches the given pattern. Use this method whenMatchableHandlerMapping.getPatternParser()returnsnullwhich means that theHandlerMappingis using String pattern matching.- Specified by:
- matchin interface- MatchableHandlerMapping
- Parameters:
- request- the current request
- pattern- the pattern to match
- Returns:
- the result from request matching, or nullif none
 
- 
getHandlerMapReturn 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.
- 
getPathPatternHandlerMapIdentical togetHandlerMap()but populated when parsed patterns areenabled; otherwise empty.- Since:
- 5.3
 
- 
supportsTypeLevelMappingsprotected boolean supportsTypeLevelMappings()Indicates whether this handler mapping support type-level mappings. Default tofalse.
 
- 
PathPatternParser.setMatchOptionalTrailingSeparator(boolean)