The Spring Framework

org.springframework.web.servlet.mvc.support
Class ControllerClassNameHandlerMapping

java.lang.Object
  extended by org.springframework.context.support.ApplicationObjectSupport
      extended by org.springframework.web.context.support.WebApplicationObjectSupport
          extended by org.springframework.web.servlet.handler.AbstractHandlerMapping
              extended by org.springframework.web.servlet.handler.AbstractUrlHandlerMapping
                  extended by org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping
All Implemented Interfaces:
ApplicationContextAware, Ordered, ServletContextAware, HandlerMapping

public class ControllerClassNameHandlerMapping
extends AbstractUrlHandlerMapping
implements HandlerMapping

Implementation of HandlerMapping that follows a simple convention for generating URL path mappings from the class names of registered Controller and ThrowawayController beans.

For simple Controller implementations (those that handle a single request type), the convention is to take the short name of the Class, remove the 'Controller' suffix if it exists and return the remaining text, lowercased, as the mapping, with a leading /. For example:

For MultiActionControllers then a similar mapping is registered, except that all sub-paths are registed using the trailing wildcard pattern /*. For example:

For MultiActionController it is often useful to use this mapping strategy in conjunction with the InternalPathMethodNameResolver.

Since:
2.0
Author:
Rob Harrop, Juergen Hoeller
See Also:
Controller, ThrowawayController, MultiActionController

Field Summary
 
Fields inherited from class org.springframework.context.support.ApplicationObjectSupport
logger
 
Fields inherited from interface org.springframework.web.servlet.HandlerMapping
PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE
 
Fields inherited from interface org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
 
Constructor Summary
ControllerClassNameHandlerMapping()
           
 
Method Summary
protected  void detectControllers()
          Detect all the Controller and ThrowawayController beans registered in the ApplicationContext and register a URL path mapping for each one based on rules defined here.
protected  String generatePathMapping(Class beanClass)
          Generate the actual URL path for the given controller class.
protected  void initApplicationContext()
          Calls the detectControllers() method in addition to the superclass's initialization.
protected  boolean isEligibleForMapping(String beanName, Class beanClass)
          Determine whether the specified controller is excluded from this mapping.
protected  void registerController(String beanName, Class beanClass)
          Register the controller with the given name, as defined in the current application context.
protected  void registerControllers(Class controllerType)
          Register all controllers of the given type, searching the current DispatcherServlet's ApplicationContext for matching beans.
 void setExcludedClasses(Class[] excludedClasses)
          Specify controller classes that should be excluded from this mapping.
 void setExcludedPackages(String[] excludedPackages)
          Specify Java packages that should be excluded from this mapping.
 
Methods inherited from class org.springframework.web.servlet.handler.AbstractUrlHandlerMapping
exposePathWithinMapping, getHandlerInternal, getHandlerMap, getRootHandler, lookupHandler, registerHandler, registerHandler, setAlwaysUseFullPath, setLazyInitHandlers, setPathMatcher, setRootHandler, setUrlDecode, setUrlPathHelper
 
Methods inherited from class org.springframework.web.servlet.handler.AbstractHandlerMapping
adaptInterceptor, extendInterceptors, getAdaptedInterceptors, getDefaultHandler, getHandler, getHandlerExecutionChain, getOrder, initInterceptors, setDefaultHandler, setInterceptors, setOrder
 
Methods inherited from class org.springframework.web.context.support.WebApplicationObjectSupport
getServletContext, getTempDir, getWebApplicationContext, isContextRequired, setServletContext
 
Methods inherited from class org.springframework.context.support.ApplicationObjectSupport
getApplicationContext, getMessageSourceAccessor, requiredContextClass, setApplicationContext
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.springframework.web.servlet.HandlerMapping
getHandler
 

Constructor Detail

ControllerClassNameHandlerMapping

public ControllerClassNameHandlerMapping()
Method Detail

setExcludedPackages

public void setExcludedPackages(String[] excludedPackages)
Specify Java packages that should be excluded from this mapping. Any classes in such a package (or any of its subpackages) will be ignored by this HandlerMapping.

Default is to exclude the entire "org.springframework.web.servlet.mvc" package, including its subpackages, since none of Spring's out-of-the-box Controller implementations is a reasonable candidate for this mapping strategy. Such controllers are typically handled by a separate HandlerMapping, e.g. a BeanNameUrlHandlerMapping, alongside this ControllerClassNameHandlerMapping for application controllers.


setExcludedClasses

public void setExcludedClasses(Class[] excludedClasses)
Specify controller classes that should be excluded from this mapping. Any such classes will simply be ignored by this HandlerMapping.


initApplicationContext

protected void initApplicationContext()
Calls the detectControllers() method in addition to the superclass's initialization.

Overrides:
initApplicationContext in class AbstractHandlerMapping
See Also:
AbstractHandlerMapping.extendInterceptors(java.util.List), AbstractHandlerMapping.initInterceptors()

detectControllers

protected void detectControllers()
                          throws BeansException
Detect all the Controller and ThrowawayController beans registered in the ApplicationContext and register a URL path mapping for each one based on rules defined here.

Throws:
BeansException - if the controllers couldn't be obtained or registered
See Also:
generatePathMapping(Class)

registerControllers

protected void registerControllers(Class controllerType)
                            throws BeansException
Register all controllers of the given type, searching the current DispatcherServlet's ApplicationContext for matching beans.

Parameters:
controllerType - the type of controller to search for
Throws:
BeansException - if the controllers couldn't be obtained or registered

isEligibleForMapping

protected boolean isEligibleForMapping(String beanName,
                                       Class beanClass)
Determine whether the specified controller is excluded from this mapping.

Parameters:
beanName - the name of the controller bean
beanClass - the concrete class of the controller bean
Returns:
whether the specified class is excluded
See Also:
setExcludedPackages(java.lang.String[]), setExcludedClasses(java.lang.Class[])

registerController

protected void registerController(String beanName,
                                  Class beanClass)
                           throws BeansException,
                                  IllegalStateException
Register the controller with the given name, as defined in the current application context.

Parameters:
beanName - the name of the controller bean
beanClass - the concrete class of the controller bean
Throws:
BeansException - if the controller couldn't be registered
IllegalStateException - if there is a conflicting handler registered
See Also:
ApplicationObjectSupport.getApplicationContext()

generatePathMapping

protected String generatePathMapping(Class beanClass)
Generate the actual URL path for the given controller class.

Subclasses may choose to customize the paths that are generated by overriding this method.

Parameters:
beanClass - the controller bean class to generate a mapping for
Returns:
the URL path mapping for the given controller

The Spring Framework

Copyright © 2002-2007 The Spring Framework.