适配器和映射器

来源:互联网 发布:企业数据备份软件 编辑:程序博客网 时间:2024/04/28 02:54

下面我们来探讨注解的处理器映射器和适配器

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.配置注解映射器和适配器。
在springmvc.xml中写:
[html] view plaincopyprint?
  1. <!-- 注解映射器 -->  
  2. <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>  
  3. <!-- 注解适配器 -->  
  4. <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>  
  5.   
  6. <!-- 使用mvc:annotation-driven代替上边注解映射器和注解适配器配置   
  7. mvc:annotation-driven默认加载很多的参数绑定方法,  
  8. 比如json转换解析器就默认加载了,如果使用mvc:annotation-driven就不用配置上边的  
  9.     RequestMappingHandlerMapping和RequestMappingHandlerAdapter  
  10. 实际开发时使用mvc:annotation-driven-->  
  11. <!--  <mvc:annotation-driven></mvc:annotation-driven>-->  

下面的开发可能就与非注解的产生了翻天覆地的变化了
3.开发注解Handler

使用注解的映射器和注解的适配器。(注解的映射器和注解的适配器必须配对使用)
ItemsController3:
[java] view plaincopyprint?
  1. package cn.edu.hpu.ssm.controller;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6.   
  7. import org.springframework.stereotype.Controller;  
  8. import org.springframework.web.bind.annotation.RequestMapping;  
  9. import org.springframework.web.servlet.ModelAndView;  
  10.   
  11. import cn.edu.hpu.ssn.po.Items;  
  12.   
  13. //注解的Handler类  
  14. //使用@Controller来标识它是一个控制器  
  15. @Controller  
  16. public class ItemsController3 {  
  17.       
  18.     //商品查询列表  
  19.     //@RequestMapping实现 对queryItems方法和url进行映射,一个方法对应一个url  
  20.     //一般建议将url和方法写成一样  
  21.     @RequestMapping("/queryItems")  
  22.     public ModelAndView queryItems()throws Exception{  
  23.           
  24.         //调用Service查找数据库,查询商品列表,这里使用静态数据模拟  
  25.         List<Items> itemsList=new ArrayList<Items>();  
  26.         //向List中填充静态数据  
  27.         Items items_1 = new Items();  
  28.         items_1.setName("联想笔记本22222222");  
  29.         items_1.setPrice(6000f);  
  30.         items_1.setDetail("ThinkPad T430 联想笔记本电脑!");  
  31.           
  32.         Items items_2 = new Items();  
  33.         items_2.setName("苹果手机22222222");  
  34.         items_2.setPrice(5000f);  
  35.         items_2.setDetail("iphone6苹果手机!");  
  36.           
  37.         itemsList.add(items_1);  
  38.         itemsList.add(items_2);  
  39.           
  40.         //返回ModelAndView  
  41.         ModelAndView modelAndView=new ModelAndView();  
  42.         //相当于request的setAttribut,在jsp页面中通过这个来取数据  
  43.         modelAndView.addObject("itemsList",itemsList);  
  44.           
  45.         //指定视图  
  46.         modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");  
  47.   
  48.   
  49.         return modelAndView;  
  50.     }  
  51.       
  52.     //定义其它方法  
  53.     //商品添加  
  54.     //商品删除  
  55. }  

这个注解的Handler的好处还有可以在类中在添加其他的方法,如商品添加、商品删除等。

5.3在spring容器中加载Handler


[html] view plaincopyprint?
  1. <!-- 对于注解的Handler可以单个配置,  
  2.     实际开发中建议使用扫描组件,因为一个一个配bean太麻烦 -->  
  3.     <!--  <bean class="cn.edu.hpu.ssm.controller.ItemsController3"></bean>-->  
  4.   
  5.   
  6.     <!-- 可以扫描controller、service、...   
  7.     这里让扫描controller,指定controller的包-->  
  8.     <context:component-scan base-package="cn.edu.hpu.ssm.controller"></context:component-scan>  


看一下最终的springmvc.xml:
[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  7.         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd   
  8.         http://www.springframework.org/schema/mvc   
  9.         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd   
  10.         http://www.springframework.org/schema/context   
  11.         http://www.springframework.org/schema/context/spring-context-3.2.xsd   
  12.         http://www.springframework.org/schema/aop   
  13.         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd   
  14.         http://www.springframework.org/schema/tx   
  15.         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">  
  16.       
  17.     <!-- 对于注解的Handler可以单个配置,  
  18.     实际开发中建议使用扫描组件,因为一个一个配bean太麻烦 -->  
  19.     <!--  <bean class="cn.edu.hpu.ssm.controller.ItemsController3"></bean>-->  
  20.   
  21.   
  22.     <!-- 可以扫描controller、service、...   
  23.     这里让扫描controller,指定controller的包-->  
  24.     <context:component-scan base-package="cn.edu.hpu.ssm.controller"></context:component-scan>  
  25.       
  26.     <!-- 注解映射器 -->  
  27.     <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>      
  28.     <!-- 注解适配器 -->  
  29.     <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>      
  30.       
  31.     <!-- 使用mvc:annotation-driven代替上边注解映射器和注解适配器配置   
  32.     mvc:annotation-driven默认加载很多的参数绑定方法,  
  33.     比如json转换解析器就默认加载了,如果使用mvc:annotation-driven就不用配置上边的  
  34.     RequestMappingHandlerMapping和RequestMappingHandlerAdapter  
  35.     实际开发时使用mvc:annotation-driven-->  
  36.     <!--  <mvc:annotation-driven></mvc:annotation-driven>-->  
  37.       
  38.     <!-- 视图解析器   
  39.     解析jsp解析,默认使用jstl标签,classpath下的得有jstl的包  
  40.      -->  
  41.     <bean  
  42.         class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  43.     </bean>  
  44. </beans>  
0 0
原创粉丝点击