ControllerClassNameHandlerMapping控制器是根据控制器短类名和URL请求匹配,如:
* WelcomeController:对应/welcome*的Url请求
* UserController:对应/user*的url请求
具体来说是将控制器的Controller后缀移除(IndexController=>Index),再将剩余部分转为小写(index)得到的字符串作为请求URL的配置前缀
配置文件代码如下:
如上面的配置对应的URL为
* IndexController 为 /index*
* UserController 为 /user*
13.11.1. The Controller - ControllerClassNameHandlerMapping
The ControllerClassNameHandlerMapping class is a HandlerMapping implementation that uses a convention to determine the mapping between request URLs and the Controller instances that are to handle those requests.
An example; consider the following (simplistic) Controller implementation. Take especial notice of the name of the class.
Here is a snippet from the attendent Spring Web MVC configuration file...
The ControllerClassNameHandlerMapping finds all of the various handler (or Controller) beans defined in its application context and strips 'Controller' off the name to define its handler mappings.
Let's look at some more examples so that the central idea becomes immediately familiar.
*
WelcomeController maps to the '/welcome*' request URL
*
HomeController maps to the '/home*' request URL
*
IndexController maps to the '/index*' request URL
*
RegisterController maps to the '/register*' request URL
*
DisplayShoppingCartController maps to the '/displayshoppingcart*' request URL
(Notice the casing - all lowercase - in the case of camel-cased Controller class names.)
In the case of MultiActionController handler classes, the mappings generated are (ever so slightly) more complex, but hopefully no less understandable. Some examples (all of the Controller names in this next bit are assumed to be MultiActionController implementations).
*
AdminController maps to the '/admin/*' request URL
*
CatalogController maps to the '/catalog/*' request URL
If you follow the pretty standard convention of naming your Controller implementations as xxxController, then the ControllerClassNameHandlerMapping will save you the tedium of having to firstly define and then having to maintain a potentially looooong SimpleUrlHandlerMapping (or suchlike).
The ControllerClassNameHandlerMapping class extends the AbstractHandlerMapping base class so you can define HandlerInterceptor instances and everything else just like you would with many other HandlerMapping implementations.
* WelcomeController:对应/welcome*的Url请求
* UserController:对应/user*的url请求
具体来说是将控制器的Controller后缀移除(IndexController=>Index),再将剩余部分转为小写(index)得到的字符串作为请求URL的配置前缀
配置文件代码如下:
# <!-- 根据控制器短类名和URL请求匹配 -->
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean name="indexController" class="cn.wangjicn.web.IndexController">
<property name="greeting" value="忘记的资料库欢迎你!"/>
</bean>
<bean name="userController" class="cn.wangjicn.web.user.UserController"/>
如上面的配置对应的URL为
* IndexController 为 /index*
* UserController 为 /user*
13.11.1. The Controller - ControllerClassNameHandlerMapping
The ControllerClassNameHandlerMapping class is a HandlerMapping implementation that uses a convention to determine the mapping between request URLs and the Controller instances that are to handle those requests.
An example; consider the following (simplistic) Controller implementation. Take especial notice of the name of the class.
public class ViewShoppingCartController implements Controller {
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {
// the implementation is not hugely important for this example...
}
}
Here is a snippet from the attendent Spring Web MVC configuration file...
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean id="viewShoppingCart" class="x.y.z.ViewShoppingCartController">
<!-- inject dependencies as required... -->
</bean>
The ControllerClassNameHandlerMapping finds all of the various handler (or Controller) beans defined in its application context and strips 'Controller' off the name to define its handler mappings.
Let's look at some more examples so that the central idea becomes immediately familiar.
*
WelcomeController maps to the '/welcome*' request URL
*
HomeController maps to the '/home*' request URL
*
IndexController maps to the '/index*' request URL
*
RegisterController maps to the '/register*' request URL
*
DisplayShoppingCartController maps to the '/displayshoppingcart*' request URL
(Notice the casing - all lowercase - in the case of camel-cased Controller class names.)
In the case of MultiActionController handler classes, the mappings generated are (ever so slightly) more complex, but hopefully no less understandable. Some examples (all of the Controller names in this next bit are assumed to be MultiActionController implementations).
*
AdminController maps to the '/admin/*' request URL
*
CatalogController maps to the '/catalog/*' request URL
If you follow the pretty standard convention of naming your Controller implementations as xxxController, then the ControllerClassNameHandlerMapping will save you the tedium of having to firstly define and then having to maintain a potentially looooong SimpleUrlHandlerMapping (or suchlike).
The ControllerClassNameHandlerMapping class extends the AbstractHandlerMapping base class so you can define HandlerInterceptor instances and everything else just like you would with many other HandlerMapping implementations.