1.前言

上一篇博客,简单的介绍了一下SpringMVC的基础知识,这篇博客来说一下SpringMVC中的几种映射处理器机制.

2.三种映射处理器

2.1 BeanNameUrlHandlerMapping  (默认)

在上一篇实例中,我们通过在springmvc-servlet.xml中就是通过这个默认的映射机制,来找到详细的处理器的请求.这一般是默认设置,然后通过Bean的名称,就能够找到对应的控制器信息.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "> <!-- 配置文件形式要配置的组建:Controller,handlermapping(有默认规则),viewResolver,interceptor -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置从项目根文件夹到一端路径 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 文件后缀名称 -->
<property name="suffix" value=".jsp"/>
</bean> <!-- 配置controller,name为要訪问的控制器的路径-->
<bean id="TestController" name="/hello.do" class="com.url.controller.TestController"></bean>
<!-- BeanNameUrlHandlerMapping 默认配置就是,所以用的时候,就不用配置 -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean> </beans>

2.2 SimpleUrlHandlerMapping(使用简单的URL来映射)

这样的方式能够集中的来为控制器设置訪问时的请求路径.普通情况下,假设採取XML文件方式配置的话,通过採用这样的方式

<?

xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "> <!-- 配置文件形式要配置的组建:Controller,handlermapping(有默认规则),viewResolver,interceptor -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置从项目根文件夹到一端路径 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 文件后缀名称 -->
<property name="suffix" value=".jsp"/>
</bean> <!-- 配置controller,name为要訪问的控制器的路径-->
<bean id="TestController" class="com.url.controller.TestController"></bean>
<!-- 使用简单url来映射 -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello1.do">TestController</prop>
</props>
</property>
</bean> </beans>

2.3 ControllerClassNameHandlerMapping (控制器名称訪问)

这样的方式一般不採用,由于一旦控制器名称更改的话,訪问路径也得跟着更改,变动性较大.

<?

xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "> <!-- 配置文件形式要配置的组建:Controller,handlermapping(有默认规则),viewResolver,interceptor -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置从项目根文件夹到一端路径 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 文件后缀名称 -->
<property name="suffix" value=".jsp"/>
</bean> <!-- 配置controller,name为要訪问的控制器的路径-->
<bean id="TestController" class="com.url.controller.TestController"></bean>
<!-- 控制类的类名控制器,訪问时类名首字母须要小写 -->
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"></bean>
</bean> </beans>

注意:訪问时,必须使用控制器全程的小写,否则会报错.

 3.小结

本篇博客简单的介绍了SpringMVC中的几种处理映射机制,下篇解说一下,SpringMVC中的几种控制器.

SpringMVC实战(三种映射处理器)的更多相关文章

  1. SpringMVC实战(三种控制器方式)

    1.前言 上篇博客着重说了一下SpringMVC中几种处理映射的方式,这篇博客来说一下SpringMVC中几种经常使用的控制器.  2.经常使用控制器 2.1 ParameterizableViewC ...

  2. 关于SpringMVC中两种映射器不能共存的解决

    首先大家都知道SpringMVC有两种映射器: org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping 和 org.spri ...

  3. SpringMVC的三种处理器适配器

    SpringMVC具有三种处理器适配器,他们分别是BeanNameUrlHandlerMapping.SimpleControllerHandlerAdapter.ControllerClassNam ...

  4. 01基于配置文件方式的SpringMVC,三种HandlerMapping,三种控制器

     1 添加Spring MVC所需的jar包. 2 创建一个以下项目结构的springmvc项目 3 web.xml的配置如下: <?xmlversion="1.0"en ...

  5. springMVC--4种映射处理器handlerMapping

    根据controller的name名称来映射寻找controller:BeanNameUrlHandlerMapping  (默认) 1.1开启该映射:默认是开启的 <bean class=&q ...

  6. spring-cloud-square开发实战(三种类型全覆盖)

    欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 本篇概览 前文<五分钟搞懂spring-clou ...

  7. 实战三种方式部署 MySQL5.7

    作者:北京运维 常见的 MySQL 安装方式有如下三种: RPM 包方式:这种方式安装适合对数据库要求不太高的场合,安装速度快: 通用二进制包方式:安装速度相较于源码方式快,可以自定义安装目录. 源码 ...

  8. 三种预处理器px2rem

    CSS单位rem 在W3C规范中是这样描述rem的: font size of the root element. 移动端越来越多人使用rem,推荐淘宝开源框架lib-flexible 今天来介绍一下 ...

  9. ORM框架三种映射在Springboot上的使用

    ORM(对象/关系映射)是数据库层非常重要的一部分,有三种常用的映射关系 1.多对一 tbl_clazz clazz{ id name description grade_id charge_id } ...

随机推荐

  1. java 关键字与保留字

    Java 关键字列表 (依字母排序 共51组),所有的关键字都是小写,在MyEclipse中都会显示不同的颜色: abstract, assert,boolean, break, byte, case ...

  2. JNI学习积累之一 ---- 常用函数大全

    主要资料来源: 百度文库的<JNI常用函数> . 同时对其加以了补充 . 要素  :1. 该函数大全是基于C语言方式的,对于C++方式可以直接转换 ,例如,对于生成一个jstring类型的 ...

  3. POJ 1470 Tarjan算法

    裸的LCA,读入小坑.Tarjan算法大坑,一开始不知道哪儿错了,后来才发现,是vis数组忘了清零了(⊙﹏⊙)b 傻傻的用了邻接矩阵...很慢啊,1100多ms. Closest Common Anc ...

  4. vue-router简单用法

    路由,其实就是指向的意思,当我点击页面上的home按钮时,页面中就要显示home的内容,如果点击页面上的about 按钮,页面中就要显示about 的内容.Home按钮  => home 内容, ...

  5. DataTable如何去除重复的行

    两种方法1 数据库直接去除重复select distinct * from 表名去除了重复行distinct 2 对 DataTable直接进行操作DataTable dt=db.GetDt(&quo ...

  6. Dynamics CRM查询实体共享给哪些人

    在mscrm中,如果想查询一条记录共享给了哪些人,需要用到PrincipalObjectAccess 表,sql如下: select u.FullName,a.RC_name,sup.SystemUs ...

  7. HDU_5724_状态压缩的sg函数

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5724 题目大意:n行20列的棋盘,对于每行,如果当前棋子右边没棋子,那可以直接放到右边,如果有就跳过放 ...

  8. [iOS Reverse]logify日志追踪,锁定注入口-控制台查看

    前言 logify是theos的一个组件,路径是: /opt/theos/bin/logify.pl 我们还是以微信红包为例子,根据[iOS Hacking]运行时分析cycript得到的入口文件: ...

  9. Bem命名

    BEM思想 1. 什么是BEM: BEM:(Block块,Element元素,Modifier修饰符)一种命名规范, 其核心思想是将页面拆分成一个个独立的富有语义的块(blocks),从而使得团队在开 ...

  10. 移动pc常用Meta标签

    移动常用 <meta charset="UTF-8"> <title>{$configInfos['store_title']}</title> ...