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 } ...
随机推荐
- [Luogu 1850] noip16 换教室
[Luogu 1850] noip16 换教室 好久没有更博客了,先唠嗑一会,花了两天的空闲时间大致做完了昨年的noip真题 虽然在经过思考大部分题目都可出解(天天爱跑步除外),但是并不知道考试时候造 ...
- Java上传视频
页面: 上传文件时的关键词:enctype="multipart/form-data" <%@ page language="java" import=& ...
- 省市区县的sql语句——省
/*SQLyog v10.2 MySQL - 5.5.48 : Database - 省市县****************************************************** ...
- Oracle存储过程给变量赋值的方法
截止到目前我发现有三种方法可以在存储过程中给变量进行赋值: 1.直接法 := 如:v_flag := 0; 2.select into 如:假设变量名为v_flag,select count( ...
- JNDI连接池连接Oracle数据库
今天做了一个评论的小功能,要求用JNDI连接池连接Oracle数据库,以前只是测试了是否连接的上,现在没想到一个JNDI连接池连接Oracle数据库,纠结了好久,原来都是Oracle数据库的问题,这是 ...
- more-less-cat-tail-head 命令简单分析
区别:cat一次性把文件内容全部显示出来,管你看不看得清,显示完了cat命令就返回了,不能进行交互式 操作,适合察看内容短小.不超过一屏的文件:more比cat强大一点,支持分页显示,你可以ctrl+ ...
- 针对Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1的解决方案
背景:本项目使用JDK1.8 编译maven工程的时候出现如下错误: Failed to execute goal org.apache.maven.plugins:maven-compiler-pl ...
- sessionStorage和localStorage存储的转换不了json
先说说localStorage与sessionStorage的差别 sessionStorage是存储浏览器的暂时性的数据,当关闭浏览器下次再打开的时候就不能拿到之前存储的缓存了 localStora ...
- react新版本生命周期
给componentWillMount componentWillReceiveProps componentWillUpdate生命周期加上UNSAFE_前缀,表明其不安全性,并将在未来版本将其移除 ...
- python tkinter模块小工具界面
代码 #-*-coding:utf-8-*- import os from tkinter import * root=Tk() root.title('小工具') #清空文本框内容 def clea ...