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



           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.youkuaiyun.com/jiangjunshow

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值