ControllerClassNameHandlerMapping

本文介绍ControllerClassNameHandlerMapping的工作原理,该映射通过去除控制器名称中的“Controller”后缀,并将其余部分转换为小写来确定URL请求。例如,WelcomeController映射到/welcome,HomeController映射到/home等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ControllerClassNameHandlerMapping控制器是根据控制器短类名和URL请求匹配,如:

* 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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值