Spring MVC ControllerClassNameHandlerMapping example
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的更多相关文章
- spring mvc: 参数方法名称解析器(用参数来解析控制器下的方法)MultiActionController/ParameterMethodNameResolver/ControllerClassNameHandlerMapping
spring mvc: 参数方法名称解析器(用参数来解析控制器下的方法)MultiActionController/ParameterMethodNameResolver/ControllerClas ...
- spring mvc: 属性方法名称解析器(多动作控制器)MultiActionController/ControllerClassNameHandlerMapping/PropertiesMethodNameResolver
spring mvc: 属性方法名称解析器(多动作控制器) 加入控制器是StudentContrller.java,里面有3个方法 index,add,remove 那么访问地址是: http://l ...
- Spring mvc源码url路由-我们到底能走多远系列(38)
我们到底能走多远系列38 扯淡: 马航的事,挺震惊的.还是多多珍惜身边的人吧. 主题: Spring mvc 作为表现层的框架,整个流程是比较好理解的,毕竟我们做web开发的,最早也经常接触的就是一个 ...
- Spring MVC 中的基于注解的 Controller【转】
原文地址:http://my.oschina.net/abian/blog/128028 终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 H ...
- Spring MVC MultiActionController example
In Spring MVC application, MultiActionController is used to group related actions into a single cont ...
- 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 ...
- Spring MVC Checkbox And Checkboxes Example
In Spring MVC, <form:checkbox /> is used to render a HTML checkbox field, the checkbox values ...
- Spring MVC中基于注解的 Controller
终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 HandlerMapping 来映射出相应的 handler 并调用相应的方法以响 ...
- spring mvc DispatcherServlet详解之一--request通过HandlerMaping获取控制器Controller过程
整个spring mvc的架构如下图所示: 现在来讲解DispatcherServletDispatcherServlet的第一步:获取控制器. HandlerMapping HandlerMappi ...
随机推荐
- 一些非常有用的html,css,javascript代码片段(持久更新)
1.判断设备是否联网 if (navigator.onLine) { //some code }else{ //others code } 2.获取url的指定参数 function getStrin ...
- Java中常见几种数据库连接方法
1:引入java.sql数据包; import java.sql.*; 2:加载JDBC驱动程序 Class.forName(JDBC驱动包的名字).newInstance(); 3:产生Co ...
- bzoj4048 3928
羞耻,分组赛上考的,竟然没想出来, 对坐标离散化后区间dp即可,竟然还双倍经验 ; ..,..] of longint; v:..] of longint; a,b,h:..] of longint; ...
- C# 创建iis站点以及IIS站点属性,iis不能启动站点
DontLog = False是否将客户端的请求写入日志文件 2011年04月09日 #region CreateWebsite 新增网站 public string CreateWebSite(st ...
- I.MX6 Goodix GT9xx touchscreen driver porting
/************************************************************************ * I.MX6 Goodix GT9xx touch ...
- NavieBayes中的多项式与伯努力模型
1文本分类过程 例如文档:Good good study Day day up可以用一个文本特征向量来表示,x=(Good, good, study, Day, day , up).在文本分类中,假设 ...
- BUFFER CACHE之主要的等待事件
原因:资源紧张,等待其释放. 原因的原因:1. lgwr和DBWn进程写太慢:2. Buffer和latch不可用 原因的原因的原因:全表扫描.library cache latches数太多等. 视 ...
- 查看nginx编译安装
大家是否遇到过去了新公司,公司内的LAMP,LNMP等所有的环境都是配置好的(已经在提供服务了),公司又没有留下部署文档,甚至安装LAMP,LAMP等环境的人已经和你交接完离职了,那么线上服务器(la ...
- <四>面向对象分析之UML核心元素之用例
一:基本概念 --->用例定义了一组用例实例,其中每个实例都是系统所执行一系列操作,这些操作生成特定主角可以观测的值. --->所谓用例,就是一件事情,要完成这 ...
- 安卓dalvik和art区别
Dalvik模式像是一台折叠自行车,每次骑之前都要组装后才能上路.而ART模式就是一个已经装好的自行车,直接就能上车走人.所以ART模式在效率上肯定是要好于Dalvik. 通过以上这种表格,我们可以直 ...