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. iOS开发:mac使用svn管理项目

    记录mac下常用的svn命令: 1.检出项目: svn checkout .../svn/projectName --username=xxx --password=xxx //将ip换成svn服务器 ...

  2. ASP.NET MVC 学习1、新增Controller,了解MVC运行机制

    1,turorial ,根据链接教程新建一个MVC项目 http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/ ...

  3. bzoj1834: [ZJOI2010]network 网络扩容

    努力看了很久样例一直过不了...然后各种输出中间过程啊巴拉巴拉弄了1h,没办法了...然后突然想到啊原来的边可以用啊为什么不用...于是A了...感人肺腑 #include<cstdio> ...

  4. linux的HugePage与oracle amm关系

     如果Oracle 是11g以后的版本,那么默认创建的实例会使用Automatic Memory Management (AMM)的特性,该特性与HugePage不兼容. 在设置HugePage之前需 ...

  5. FTP出现211-Extension supported 停止的解决方法

    FTP出问题211-Extension supported 停止的解决方法 FTP出问题211-Extension supported 停止的解决方法 FLASHFXP FTP上传登录时提示Exten ...

  6. Web Api 如何做上传文件的单元测试

    代码如下: //--------上传------------ HttpClient client = new HttpClient(); #region MultipartFormDataConten ...

  7. ionic cordova plugin for ios

    源代码结构目录: payplugin: |_src |_android |_PayPlugin.java |_ios |_CDVPayPlugin.h |_CDVPayPlugin.m |_www | ...

  8. 将多个.a库合并为一个.a库的方法

    如果编译了多个架构的静态库,想将它们合并为一个静态库的时候,可以用如下方法合并: sudo lipo -create /libs/ffmpeg/2.6.3/arm64/lib/libavcodec.a ...

  9. 图解VS2010打包全过程

    原文转自:http://blog.csdn.net/shan9liang/article/details/6957308 最近刚刚打包发布了用VS2010开发的一个收费系统,借此讲一讲打包过程,供大家 ...

  10. Oracle DBA 的常用Unix参考手册(一)

    作为一名Oracle DBA,在所难免要接触Unix,但是Unix本身又是极其复杂的,想要深刻掌握同样很不容易.那么到底我们该怎么入手呢?Donald K Burleson 的<Unix for ...