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 中进行网络通信时,通信的程序两端要传输的对象,不仅要序列化,而且这个对象所属的类的名字要完全一样,连包的名字都得一样

    如上图项目目录,这是一个简易的QQ,客户端登录的时候要传输用户信息到服务器验证,所以两端都会用到User类的对象,但一开始我在Server端的包名是com.qq.server.common,两端的报名 ...

  2. python请求服务器图片并下载到本地磁盘

    >>> import os >>> os.getcwd() 'C:\\Python33' >>> os.chdir('E:\\python\\mm ...

  3. C#:设置webBrowser框架与系统相对应的IE内核版本

    通常情况下,我们直接调用C#的webBrowser控件,默认的浏览器内核是IE7.  那么如何修改控件调用的默认浏览器版本呢? /// <summary> /// 修改注册表信息来兼容当前 ...

  4. sql学习--update

    两种修改形式 第一种:静态插入 ,notes='began career selling ...balabala' where jc='johnny ca' 第二种: --注意别名和on后边的表连接不 ...

  5. SQL Server对数据进行添加

    下面介绍一下SQL Server如何往数据库中加入数据. <!-- 添加主人信息 --> <form action="add_Admin.jsp" method= ...

  6. MVC5+EasyUI+EF6+Linq通用权限系统出炉--登录(2)

    1.输入验证码后 自动识别验证码并登录.

  7. C#程序集版本控制文件属性祥解

    using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices ...

  8. 【汇编】MASM6.15几个简单的汇编程序

    /***************通过调用(INT 21H)表中的01h号功能号从键盘输入一个字符并回显到视频显示器上*****************/ DATAS SEGMENT ;此处输入数据段代 ...

  9. Swift - AnyClass,元类型和 .self

    在Swift中能够表示 “任意” 这个概念的除了 Any 和 AnyObject 以外,还有一个AnyClass.我们能够使用AnyClass协议作为任意类型实例的具体类型.AnyClass在Swif ...

  10. 【转载】java读取.properties配置文件的几种方法

    读取.properties配置文件在实际的开发中使用的很多,总结了一下,有以下几种方法(仅仅是我知道的):一.通过jdk提供的java.util.Properties类.此类继承自java.util. ...