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中的几种控制器.

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-15 04:40:21

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

SpringMVC中的种映射处理器

在SpringMVC中处理请求到对应的Controller有三种: 一.BeanNameUrlHandlerMapping(默认) 该处理器根据bean的name属性对应到请求上 <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>可有可无 例如: <bean name="/index3" class="com.controlle

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

1.前言 上篇博客着重说了一下SpringMVC中几种处理映射的方式,这篇博客来说一下SpringMVC中几种经常使用的控制器.  2.经常使用控制器 2.1 ParameterizableViewController(參数控制器) <span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"? > <beans xmlns="http

SpringMVC的三种处理器适配器

SpringMVC具有三种处理器适配器,他们分别是BeanNameUrlHandlerMapping.SimpleControllerHandlerAdapter.ControllerClassNameHandlerMapping,可以分别使用这三种处理器适配器进行获取Controller. 程序结构如下: 首先,来介绍BeanNameUrlHandlerMapping映射,如下所示,这种映射使用Bean的name来访问控制器,它根据类名(Mycontroller)类名.do来访问,类名首字母小

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

 1  添加Spring MVC所需的jar包. 2  创建一个以下项目结构的springmvc项目 3  web.xml的配置如下: <?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.or

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

ORM(对象/关系映射)是数据库层非常重要的一部分,有三种常用的映射关系 1.多对一 tbl_clazz clazz{ id name description grade_id charge_id } clazz { id name description grade:{ id: name: ... }, charge:{ id name gender } } 提供多对一的查询   -------班级表对应年级表和班主任表 bean extend ClazzVM.java dao extend

springmvc基础篇—掌握三种处理器

随着springmvc的广泛使用,关于它的很多实用有效的功能应该更多的被大家所熟知,下面就介绍一下springmvc的三种处理器: 一.BeanName处理器(默认) <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/

spring mvc handler的三种方式

springmvc.xml 三种方式不能针对一个controller同时使用 <?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="

spring学习笔记2---MVC处理器映射(handlerMapping)三种方式(附源码)

一.根据Beanname访问controller: 在springmmvc-servlet.xml的配置handlermapping中加入beanname,通过该beanname找到对应的controller实现控制 1 <!-- 配置HandlerMapping 根据beanname找到对应Controller --> 2 <bean 3 class="org.springframework.web.servlet.mvc.support.ControllerBeanName

04springMVC结构,mvc模式,spring-mvc流程,spring-mvc的第一个例子,三种handlerMapping,几种控制器,springmvc基于注解的开发,文件上传,拦截器,s

 1. Spring-mvc介绍 1.1市面上流行的框架 Struts2(比较多) Springmvc(比较多而且属于上升的趋势) Struts1(即将被淘汰) 其他 1.2  spring-mvc结构 DispatcherServlet:中央控制器,把请求给转发到具体的控制类 Controller:具体处理请求的控制器(配置文件方式需要配置,注解方式不用配置) handlerMapping:映射处理器,负责映射中央处理器转发给controller时的映射策略 ModelAndView:服务