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

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

时间:2017-07-20 17:23:22      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:请求   data   name   cep   pos   classname   content   stc   方式   

 

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

标签:请求   data   name   cep   pos   classname   content   stc   方式   

原文地址:http://www.cnblogs.com/liguangsunls/p/7211847.html

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