handler mapping是把url跟控制器关联起来。

In Spring MVC, ControllerClassNameHandlerMapping use convention to map requested URL to Controller (convention over configuration). It takes the Class name, remove the ‘Controller’ suffix if exists and return the remaining text, lower-cased and with a leading “/”.

See following few examples to demonstrate the use of this ControllerClassNameHandlerMapping class.

1. Before and After

By default, Spring MVC is using the BeanNameUrlHandlerMapping handler mapping.

<beans ...>

  <bean name="/welcome.htm"
class="com.mkyong.common.controller.WelcomeController" /> <bean name="/helloGuest.htm"
class="com.mkyong.common.controller.HelloGuestController" /> </beans>

To enable the ControllerClassNameHandlerMapping, declared it in the bean configuration file, and now the controller’s bean’s name is no longer required.

<beans ...>

  <bean
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" /> <bean class="com.mkyong.common.controller.WelcomeController" /> <bean class="com.mkyong.common.controller.HelloGuestController" /> </beans>

Now, Spring MVC is mapping the requested URL by following conventions :

WelcomeController -> /welcome*
HelloGuestController -> /helloguest* /welcome.htm –> WelcomeController.
/welcomeHome.htm –> WelcomeController.
/helloguest.htm –> HelloGuestController.
/helloguest12345.htm –> HelloGuestController.

/helloGuest.htm, failed to map /helloguest*, the “g” case is not match.

2. Case sensitive

To solve the case sensitive issue stated above, declared the “caseSensitive” property and set it to true.

<beans ...>

  <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" >
<property name="caseSensitive" value="true" />
</bean> <bean class="com.mkyong.common.controller.WelcomeController" /> <bean class="com.mkyong.common.controller.HelloGuestController" /> </beans>

Now, Spring MVC is mapping the requested URL by the following conventions :

WelcomeController -> /welcome*
HelloGuestController -> /helloGuest*
/helloGuest.htm –> HelloGuestController.

/helloguest.htm, failed to map “/helloGuest*”, the “G” case is not match.

3. pathPrefix

Additionally, you can specify a prefix to maps the requested URL, declared a “pathPrefix” property.

<beans ...>

  <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" >
<property name="caseSensitive" value="true" />
<property name="pathPrefix" value="/customer" />
</bean> <bean class="com.mkyong.common.controller.WelcomeController" /> <bean class="com.mkyong.common.controller.HelloGuestController" /> </beans>

Now, Spring MVC is mapping the requested URL by the following conventions :

WelcomeController -> /customer/welcome*
HelloGuestController -> /customer/helloGuest*

/customer/welcome.htm –> WelcomeController.

/customer/helloGuest.htm –> HelloGuestController.

/welcome.htm, failed.

/helloGuest.htm, failed.

Spring MVC ControllerClassNameHandlerMapping example的更多相关文章

  1. spring mvc: 参数方法名称解析器(用参数来解析控制器下的方法)MultiActionController/ParameterMethodNameResolver/ControllerClassNameHandlerMapping

    spring mvc: 参数方法名称解析器(用参数来解析控制器下的方法)MultiActionController/ParameterMethodNameResolver/ControllerClas ...

  2. spring mvc: 属性方法名称解析器(多动作控制器)MultiActionController/ControllerClassNameHandlerMapping/PropertiesMethodNameResolver

    spring mvc: 属性方法名称解析器(多动作控制器) 加入控制器是StudentContrller.java,里面有3个方法 index,add,remove 那么访问地址是: http://l ...

  3. Spring mvc源码url路由-我们到底能走多远系列(38)

    我们到底能走多远系列38 扯淡: 马航的事,挺震惊的.还是多多珍惜身边的人吧. 主题: Spring mvc 作为表现层的框架,整个流程是比较好理解的,毕竟我们做web开发的,最早也经常接触的就是一个 ...

  4. Spring MVC 中的基于注解的 Controller【转】

    原文地址:http://my.oschina.net/abian/blog/128028 终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 H ...

  5. Spring MVC MultiActionController example

    In Spring MVC application, MultiActionController is used to group related actions into a single cont ...

  6. Configure the handler mapping priority in Spring MVC

    Often times, you may mix use of multiple handler mappings strategy in Spring MVC development. For ex ...

  7. Spring MVC Checkbox And Checkboxes Example

    In Spring MVC, <form:checkbox /> is used to render a HTML checkbox field, the checkbox values ...

  8. Spring MVC中基于注解的 Controller

         终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 HandlerMapping 来映射出相应的 handler 并调用相应的方法以响 ...

  9. spring mvc DispatcherServlet详解之一--request通过HandlerMaping获取控制器Controller过程

    整个spring mvc的架构如下图所示: 现在来讲解DispatcherServletDispatcherServlet的第一步:获取控制器. HandlerMapping HandlerMappi ...

随机推荐

  1. 高斯消元与xor方程组

    ;i<=n;i++) { ;j<=n;j++) if(a[j]>a[i]) swap(a[i],a[j]); if(!a[i]) break; ;j>=;j--) ) { ;k ...

  2. listview默认选择第一项,点击换子项背景图

    (不是大神,没有几百子项目,去你丫的) private int last_item_position ; @Override public void onItemClick(AdapterView&l ...

  3. ListView 点击某一项换背景图片

    1. layout_search_list_item.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/r ...

  4. liunx上运行mybase

    mybase 是个人知识管理软件,国内用他的人很多,尤其是程序员.我也是mybase的忠实使用者,有大量的积累.    以前一直用Windows,mybase使用完全没有问题,后来转投ubuntu阵营 ...

  5. 最好用的汉字转拼音代码PinYin4Objc(PinYin4J的objc版本)

    转:https://github.com/kimziv/PinYin4Objc 最好用的汉字转拼音代码PinYin4Objc(PinYin4J的objc版本)(更新到v1.1.1,增加block异步处 ...

  6. js函数——倒计时模块+无缝滚动

    倒计时 效果: 代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  7. [Everyday Mathematics]20150108

    设 $f$ 在 $(a,b)$ 上 $n+1$ 次可导, 且 $$\bex \ln\frac{f(b)+f'(b)+\cdots+f^{(n)}(b)}{f(a)+f'(a)+\cdots+f^{(n ...

  8. 开启Ubuntu Linux下VirtualBox访问USB功能

    解决方法如下: 1.增加用户组usbfs sudo groupadd usbfs 2.查看usbfs用户组的gid cat /etc/group | grep usbfs usbfs:x:1002: ...

  9. 编程式事务、XML配置事务、注解实现事务

    Spring2.0框架的事务处理有两大类: 1 编码式事务 , 这个不说. 2 声明式事务 , 就说这个. 声明式事务又有三种实现方法: 1 (第一种) 最早的方法,用TransactionProxy ...

  10. Reading or Writing to Another Processes Memory in C# z

    http://www.jarloo.com/reading-and-writing-to-memory/ Declarations [Flags] public enum ProcessAccessF ...