【SpringMVC框架】注解的处理器映射器和适配器配置

刺骨的言语ヽ痛彻心扉 2021-09-25 23:16 563阅读 0赞

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

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 plain copy

  1. <**bean class=”org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping”/>**
  2. <**bean class=”org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter”/>**
  3. <!— 使用mvc:annotation-driven代替上边注解映射器和注解适配器配置
  4. mvc:annotation-driven默认加载很多的参数绑定方法,
  5. 比如json转换解析器就默认加载了,如果使用mvc:annotation-driven就不用配置上边的
  6. RequestMappingHandlerMapping和RequestMappingHandlerAdapter
  7. 实际开发时使用mvc:annotation-driven—>

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

使用注解的映射器和注解的适配器。(注解的映射器和注解的适配器必须配对使用)
ItemsController3:

[java] view plain copy

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

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

5.3在spring容器中加载Handler

[html] view plain copy

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

看一下最终的springmvc.xml:

[html] view plain copy

  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. <!— 对于注解的Handler可以单个配置,
  17. 实际开发中建议使用扫描组件,因为一个一个配bean太麻烦 —>
  18. <!— 可以扫描controller、service、…
  19. 这里让扫描controller,指定controller的包—>
  20. <**context:component-scan base-package=”cn.edu.hpu.ssm.controller”></context:component-scan**>
  21. <**bean class=”org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping”/>**
  22. <**bean class=”org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter”/>**
  23. <!— 使用mvc:annotation-driven代替上边注解映射器和注解适配器配置
  24. mvc:annotation-driven默认加载很多的参数绑定方法,
  25. 比如json转换解析器就默认加载了,如果使用mvc:annotation-driven就不用配置上边的
  26. RequestMappingHandlerMapping和RequestMappingHandlerAdapter
  27. 实际开发时使用mvc:annotation-driven—>
  28. <!— 视图解析器
  29. 解析jsp解析,默认使用jstl标签,classpath下的得有jstl的包
  30. >
  31. <**bean**
  32. class=”org.springframework.web.servlet.view.InternalResourceViewResolver”>
  33. </**bean**>
  34. </**beans**>

5.4部署调试
访问:http://localhost:8080/springmvcfirst1208/queryItems.action

访问结果如图

Center

下一篇总结我们来分析一下springmvc中一些源码的分析

发表评论

表情:
评论列表 (有 0 条评论,563人围观)

还没有评论,来说两句吧...

相关阅读