springmvc学习3:处理器映射器和适配器

来源:互联网 发布:使命召唤5枪支数据 编辑:程序博客网 时间:2024/04/26 01:51
一、非注解处理器映射器和适配器
1.非注解的处理器映射器

1)org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping

2)另一个映射器:org.springframework.web.servlet.handler.SimpleUrlHandlerMapping

SimpleUrlHandlerMapping是BeanNameUrlHandlerMapping的增强版本,它可以将url和处理器bean的id进行统一映射配置。

多个映射器可以并存,前端控制器判断url能让哪个映射器映射,就让正确的映射器处理。

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"><property name="mappings"><props><!-- 对itemsController1进行url映射,url是/queryItems1.action --><prop key="/queryItems1.action">itemController1<prop/><prop key="/queryItems2.action">itemController1<prop/></props></property></bean>

2.非注解的处理器适配器

1)org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter:要求编写的Handler实现Controller接口

2)org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter:要求编写的Handler实现HttpRequestHandler接口。

二、注解的处理器映射器和适配器
1)介绍

在spring3.1之前使用org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping注解映射器,

在spring3.1之后使用org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping注解映射器。

在spring3.1之前使用org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter注解适配器,

在spring3.1之后使用org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter注解适配器。

2)配置

a)方式一(不常用)

<!-- 注解映射器 --><bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/><!-- 注解适配器 --><bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>

注解的映射器和注解的适配器必须配对使用。

b)方式二(常用)

<mvc:annotation-driven></mvc:annotation-driven>
mvc:annotation-driven默认加载很多参数绑定方法,比如json转换就默认加载了。

三、开发注解Handler

1)@RequestMapping("/xxx")

2)spring容器中加载Handler。组件扫描的方式:

<context:component-scan base-package="cn.itcast.ssm.controller"/>
四、配置视图解析器的前缀和后缀

<beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/"></property><property name="suffix" value=".jsp"></property></bean>



0 0