SpringMVC实战(三种映射处理器)
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实战(三种映射处理器)的更多相关文章
- SpringMVC实战(三种控制器方式)
1.前言 上篇博客着重说了一下SpringMVC中几种处理映射的方式,这篇博客来说一下SpringMVC中几种经常使用的控制器. 2.经常使用控制器 2.1 ParameterizableViewC ...
- 关于SpringMVC中两种映射器不能共存的解决
首先大家都知道SpringMVC有两种映射器: org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping 和 org.spri ...
- SpringMVC的三种处理器适配器
SpringMVC具有三种处理器适配器,他们分别是BeanNameUrlHandlerMapping.SimpleControllerHandlerAdapter.ControllerClassNam ...
- 01基于配置文件方式的SpringMVC,三种HandlerMapping,三种控制器
1 添加Spring MVC所需的jar包. 2 创建一个以下项目结构的springmvc项目 3 web.xml的配置如下: <?xmlversion="1.0"en ...
- springMVC--4种映射处理器handlerMapping
根据controller的name名称来映射寻找controller:BeanNameUrlHandlerMapping (默认) 1.1开启该映射:默认是开启的 <bean class=&q ...
- spring-cloud-square开发实战(三种类型全覆盖)
欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 本篇概览 前文<五分钟搞懂spring-clou ...
- 实战三种方式部署 MySQL5.7
作者:北京运维 常见的 MySQL 安装方式有如下三种: RPM 包方式:这种方式安装适合对数据库要求不太高的场合,安装速度快: 通用二进制包方式:安装速度相较于源码方式快,可以自定义安装目录. 源码 ...
- 三种预处理器px2rem
CSS单位rem 在W3C规范中是这样描述rem的: font size of the root element. 移动端越来越多人使用rem,推荐淘宝开源框架lib-flexible 今天来介绍一下 ...
- ORM框架三种映射在Springboot上的使用
ORM(对象/关系映射)是数据库层非常重要的一部分,有三种常用的映射关系 1.多对一 tbl_clazz clazz{ id name description grade_id charge_id } ...
随机推荐
- Redis学习笔记(三)-数据类型之string类型
string是redis最基本的类型,而且string类型是二进制安全的.意思是redis的string可以包含任何数据.比如jpg图片或者序列化的对象.从内部实现来看其实string可以看作byte ...
- 网络开发之使用Web Service和使用WCF服务
判断是否有可用网络连接可以通过NetworkInterface类中的GetIsNetworkAvailable来实现: bool networkIsAvailable = networkInterfa ...
- Java 中静态变量和实例变量区别
Java 中静态变量和实例变量区别 静态变量属于类,该类不生产对象,通过类名就可以调用静态变量. 实例变量属于该类的对象,必须产生该类对象,才能调用实例变量. 在程序运行时的区别: 实例变量属于某个对 ...
- 红黑联盟 php相关资讯
http://www.2cto.com/tag/phpbanben.html
- OpenCV的AdaptiveThreshold函数
摘自于OpenCV Doc2.410,opencv2refman文档. 1.函数原型 adaptiveThreshold //Applies an adaptive threshold to an a ...
- Arduino LM35温度检测
一. 接线原理图 二.实物图 三.代码例子
- pyqt5 做的小程序,可以用来UI做个小demo
#!/usr/bin/python3# -*- coding: utf-8 -*- """ZetCode PyQt5 tutorial This program crea ...
- 在Unity中json文件的解析方式
using System.Collections; using System.Collections.Generic; using UnityEngine; using LitJson; using ...
- Ubuntu下解压(unzip)乱码的解决方法
在Windows上压缩的文件,是以Windows系统默认编码中文来压缩文件.由于zip文件中没有声明其编码,所以linux上的unzip一般以默认编码解压,中文文件名会出现乱码. 通过unzip -- ...
- MySQL创建临时表
drop TEMPORARY table if EXISTS temp_table; create TEMPORARY table temp_table( id int not null, usern ...