|
The Spring Framework | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.springframework.context.support.ApplicationObjectSupport
org.springframework.web.context.support.WebApplicationObjectSupport
org.springframework.web.servlet.handler.AbstractHandlerMapping
org.springframework.web.servlet.handler.AbstractUrlHandlerMapping
org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping
public class ControllerClassNameHandlerMapping
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:
WelcomeController -> /welcome*HomeController -> /home*For MultiActionControllers then a similar mapping is registered,
except that all sub-paths are registed using the trailing wildcard pattern /*.
For example:
WelcomeController -> /welcome/*CatalogController -> /catalog/*For MultiActionController it is often useful to use
this mapping strategy in conjunction with the
InternalPathMethodNameResolver.
Thanks to Warren Oliver for suggesting the "caseSensitive", "pathPrefix" and "basePackage" properties which have been added in Spring 2.5.
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 |
setBasePackage(String basePackage)
Set the base package to be used for generating path mappings, including all subpackages underneath this packages as path elements. |
void |
setCaseSensitive(boolean caseSensitive)
Set whether to apply case sensitivity to the generated paths, e.g. turning the class name "BuyForm" into "buyForm". |
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. |
void |
setPathPrefix(String prefixPath)
Specify a prefix to prepend to the path generated from the controller name. |
| Methods inherited from class org.springframework.web.servlet.handler.AbstractUrlHandlerMapping |
|---|
buildPathExposingHandler, 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 |
|---|
public ControllerClassNameHandlerMapping()
| Method Detail |
|---|
public void setExcludedPackages(String[] excludedPackages)
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.
public void setExcludedClasses(Class[] excludedClasses)
public void setCaseSensitive(boolean caseSensitive)
Default is "false", using pure lower case paths, e.g. turning the class name "BuyForm" into "buyform".
public void setPathPrefix(String prefixPath)
Default is a plain slash ("/"). A path like "/mymodule" can be specified in order to have controller path mappings prefixed with that path, e.g. "/mymodule/buyform" instead of "/buyform" for the class name "BuyForm".
public void setBasePackage(String basePackage)
Default is null, using the short class name for the
generated path, with the controller's package not represented in the path.
Specify a base package like "com.mycompany.myapp" to include subpackages
within that base package as path elements, e.g. generating the path
"/mymodule/buyform" for the class name "com.mycompany.myapp.mymodule.BuyForm".
Subpackage hierarchies are represented as individual path elements,
e.g. "/mymodule/mysubmodule/buyform" for the class name
"com.mycompany.myapp.mymodule.mysubmodule.BuyForm".
protected void initApplicationContext()
detectControllers() method in addition to the
superclass's initialization.
initApplicationContext in class AbstractHandlerMappingAbstractHandlerMapping.extendInterceptors(java.util.List),
AbstractHandlerMapping.initInterceptors()
protected void detectControllers()
throws BeansException
Controller and
ThrowawayController
beans registered in the ApplicationContext
and register a URL path mapping for each one based on rules defined here.
BeansException - if the controllers couldn't be obtained or registeredgeneratePathMapping(Class)
protected void registerControllers(Class controllerType)
throws BeansException
controllerType - the type of controller to search for
BeansException - if the controllers couldn't be obtained or registered
protected boolean isEligibleForMapping(String beanName,
Class beanClass)
beanName - the name of the controller beanbeanClass - the concrete class of the controller bean
setExcludedPackages(java.lang.String[]),
setExcludedClasses(java.lang.Class[])
protected void registerController(String beanName,
Class beanClass)
throws BeansException,
IllegalStateException
beanName - the name of the controller beanbeanClass - the concrete class of the controller bean
BeansException - if the controller couldn't be registered
IllegalStateException - if there is a conflicting handler registeredApplicationObjectSupport.getApplicationContext()protected String generatePathMapping(Class beanClass)
Subclasses may choose to customize the paths that are generated by overriding this method.
beanClass - the controller bean class to generate a mapping for
|
The Spring Framework | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||