码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMVC实战(三种映射处理器)

时间:2015-07-14 11:40:13      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:

 

 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实战(三种映射处理器)

标签:

原文地址:http://blog.csdn.net/luckyzhoustar/article/details/46873685

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!