SpringMVC映射器和适配器

来源:互联网 发布:小强软件测试 编辑:程序博客网 时间:2024/04/27 02:16

一,非注解的处理器映射器和适配器


非注解映射器

1.第一种处理器映射器

<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/><!-- 配置url和Handler的映射关系 --><bean name="/category.action" class="com.controller.CategoryController"/>

这种映射器有个问题,就是url和handler的映射关系要单独配置..(和映射器分开了)

2.第二种处理器映射器

<bean id="categoryController" class="com.controller.CategoryController"/><!-- 简单url映射器 --> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings">     <props> <!--  key 为客户端请求的url   后面是 handler的id 由id为 categoryController的handler进行处理-->    <prop key="/category1.action">categoryController</prop>    <prop key="/category2.action">categoryController</prop>    </props> </property></bean>

这种映射器的好处是,url和对应的handler可以集中配置(放到了映射器的里面)

两种映射器可以并存,中心控制器会去判断url能让哪个映射器映射就让哪个映射器映射.


非注解映射器

1.第一种适配器(SimpleController)

<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

要求编写的Handler要实现Controller接口、实现接口的handleRequest()方法

2.第二种适配器(HttpRequest)

<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/>

要求编写的Handler实现 HttpRequestHandler 接口
和原始的Servlet类似.

如果不配置默认使用org.springframework.web.servlet.DispatcherServlet.properties 文件中配置的.

二,注解的处理器映射器和适配器

在spring3.1之前使用的

注解映射器

org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping

注解适配器

org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter

使在spring3.1之后

注解映射器

org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping

注解适配器

org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter

1.在springmvc中配置注解的映射器和适配器
注解的映射器和适配器不是默认提供的,必须要配置一下

注解的映射器

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>

注解的适配器

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> 

使用mvc:annotation-driven代替上面两行 写了这个注解驱动 上面两行就不用写了注解驱动还默认加载了很多参数绑定方法,比如json转换解析器就默认加载了 实际开发推荐使用这个

 <mvc:annotation-driven></mvc:annotation-driven>

注解的映射器和适配器要成对使用!!
不能一个用注解的适配器和另一个用非注解的映射器!

2.编写Handler
(1)使用@Controller 标示控制器 就可以不实现 Controller接口或HttpRequestHandler接口了

(2)使用@RequestMapping(“/queryCategory.action”) 配置 url和方法的映射关系 .action可以加也可以不加

3.在springmvc中声明编写的handler

 <bean class="com.controller.CategoryController3"/> 

一个一个声明控制器比较麻烦,可以使用组件扫描,可以扫描控制器、service等,这里扫描控制器 ambiguous 使用了组件扫描后,控制器就不需要在这声明了,如果再声明就报错,含糊的方法

<context:component-scan base-package="com.controller"></context:component-scan>
0 0
原创粉丝点击