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. java中的toString方法

    对于我这种用惯了C++的人来说,突然见到有人写java程序的时候竟然将整数和String类型的变量使用+连接到一起,感到非常奇怪,追究了下原因. 原来所有的java对象都有toString()方法,而 ...

  2. eclipse mingw cpp开发环境

    Eclipse开发c++ 对比:微软的VC++6.0:太老了,对win7兼容不好, 现在微软的Visual Studio:安装包太大,好几个G,装了一堆你不需要的东西,要钱,教 育版申请麻烦 DOS下 ...

  3. hdu 4864 Task (贪心 技巧)

    题目链接 一道很有技巧的贪心题目. 题意:有n个机器,m个任务.每个机器至多能完成一个任务.对于每个机器,有一个最大运行时间xi和等级yi, 对于每个任务,也有一个运行时间xj和等级yj.只有当xi& ...

  4. 可视化PK纯代码

    简述 其实今天说的内容不仅仅局限于Qt,在很多其它语言或者框架中也适用,那就是 - 用可视化工具or文本编辑器?拖or不拖? 如果有人问我喜欢脱or不脱?我会毫不犹豫地说不脱,因为我比较矜持O(∩_∩ ...

  5. 四种途径将HTML5 web应用变成android应用

    作为下一代的网页语言,HTML5拥有很多让人期待已久的新特性.HTML5的优势之一在于能够实现跨平台游戏编码移植,现在已经有很多公司在移动 设备上使用HTML5技术.随着HTML5跨平台支持的不断增强 ...

  6. ListView 使用

    1. 不使用xml 文件 动态创建 Listview 并且绑定 ArrayList ListView listView = new ListView(this); listView.setAdapte ...

  7. svn 提交失败 更新失败 提示 已经锁定

      出现问题的原因:在上传的时候,由于网络掉线,导致svn提交到一半就没有反应了,这个时候我点击了取消,再之后无论是进行 更新还是提交,都提示 已经锁定 解决方法:在项目的空白地方,点击SVN  清理 ...

  8. bq24075 锂电池 充电电路分析

     bq24075 锂电池 充电电路分析 本文主要是分析bq24075锂电池充电芯片电路,知道其大致是怎么工作的,其中的一些电阻该如何配置. -- 深圳 南山平山村 曾剑锋 一.参考文章: . NTC热 ...

  9. JAVA JDK1.5-1.9新特性

    1.51.自动装箱与拆箱:2.枚举(常用来设计单例模式)3.静态导入4.可变参数5.内省 1.61.Web服务元数据2.脚本语言支持3.JTable的排序和过滤4.更简单,更强大的JAX-WS5.轻量 ...

  10. mysql的几种隐式转化

    1. 表定义是字符型,传入的是Int 2. 字符集不一致.表定义的字段是gbk,传入的是utf8:这种在存储过程中出现得比较多. 数据库的字符集utf8 mysql> show create d ...