非注解和注解的处理器映射器和适配器---SpringMVC学习笔记(三)

本文介绍SpringMVC中的处理器映射器和适配器,包括非注解和注解方式的配置及使用。涵盖SimpleUrlHandlerMapping、SimpleControllerHandlerAdapter等组件,并详细解释注解驱动下RequestMappingHandlerMapping及RequestMappingHandlerAdapter的工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

非注解的处理器映射器
  1. 之前的入门Demo中使用的就是非注解的处理器映射器: 
    org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping

  2. 另一种非注解的处理器映射器: 
    org.springframework.web.servlet.handler.SimpleUrlHandlerMapping

    <!-- 配置Handler -->
    <bean id="itemsController1" class="com.huihui.controller.ItemsController1" />

    <!-- 配置简单url处理器映射器 -->
    <bean
        class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/queryItems1.action">itemsController1</prop>
                <prop key="/queryItems2.action">itemsController1</prop>
            </props>
        </property>
    </bean>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

进行这个配置之后,访问 
http://localhost:8080/SpringMVCFirst/queryItems1.action 
和 
http://localhost:8080/SpringMVCFirst/queryItems2.action 
都会进入商品列表的jsp页面。

非注解的处理器适配器
  1. 之前的入门Demo中使用的就是非注解的处理器适配器: 
    org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter, 
    查看该适配器的源码: 
    这里写图片描述 
    可以发现,所有实现了org.springframework.web.servlet.mvc.Controller 接口的Bean通过此适配器进行适配、执行才可以。
  2. 另一种处理器适配器: 
    org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter, 
    查看该适配器的源码: 
    这里写图片描述 
    可以发现,所有实现了org.springframework.web.HttpRequestHandler 接口的Bean通过此适配器进行适配、执行才可以。 
    这种处理器适配器的Handler实现如下:
public class ItemsController1 implements HttpRequestHandler{

    @Override
    public void handleRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //调用service查找数据库,查询商品列表。这里使用静态数据模拟
        List<Items> itemsList = new ArrayList<Items>();
        //向list中添加静态数据
        Items items_1 = new Items();
        items_1.setName("联想笔记本");
        items_1.setPrice(6000f);
        items_1.setDetail("ThinkPad T430 联想笔记本电脑!");

        Items items_2 = new Items();
        items_2.setName("苹果手机");
        items_2.setPrice(5000f);
        items_2.setDetail("iphone6苹果手机!");

        itemsList.add(items_1);
        itemsList.add(items_2);

        //设置模型数据
        request.setAttribute("itemsList", itemsList);
        //设置转发到的视图
        request.getRequestDispatcher("/jsp/items/itemsList.jsp").forward(request, response);
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

从上边可以看出此适配器的handleRequest方法类似于原始的Servlet,通过这种方式可以设置response响应内容,比如返回json数据:

response.setCharacterEncoding("utf-8");
response.setContentType("application/json;charset=utf-8");
response.getWriter().write("json串");
 
 
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3
DispatcherSerlvet.properties

如果不配置springmvc.xml文件,应用程序照样可以正常运行。原因是: 
在web.xml中配置的前端控制器org.springframework.web.servlet.DispatcherServlet的包中存在DispatcherSerlvet.properties。 
这里写图片描述 
前端控制器从上边的文件中加载处理映射器、适配器、视图解析器等组件,如果不在springmvc.xml中配置,使用DispatcherSerlvet.properties文件中配置的默认加载的。

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

在spring3.1之前和spring3.1之后的版本中使用的注解方式的处理器映射器和处理器适配器有所不同:
  1. 注解方式使用的处理器映射器: 
    在spring3.1之前使用org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping注解映射器。 
    在spring3.1之后使用org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping注解映射器。

  2. 注解方式使用的处理器适配器: 
    在spring3.1之前使用org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter注解适配器。 
    在spring3.1之后使用org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter注解适配器。

第一步:配置注解的映射器和适配器
    <!-- 配置注解方式的处理器映射器 -->
    <bean
        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
    <!-- 配置注解方式的处理器适配器 -->
    <bean
        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" />
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

注意: 
springmvc使用<mvc:annotation-driven>自动加载RequestMappingHandlerMapping和RequestMappingHandlerAdapter,可用在springmvc.xml配置文件中使用<mvc:annotation-driven>替代上面配置的注解处理器和适配器的配置。 
<mvc:annotation-driven>默认还加载很多的参数绑定方法,比如json转换解析器就默认加载了。实际开发中我们一般使用<mvc:annotation-driven>

第二步:开发Handler(使用注解的方式)

这里写图片描述 
使用@Controller注解标识它是一个控制器。也就相当于之前没有使用注解开发的Hander中的: 
这里写图片描述

@Controller
public class ItemsController2 {
    //商品查询列表
    //@RequestMapping实现对queryItmes方法和url进行映射,一个方法对应一个url。一般建议将url和方法名写成一样
    @RequestMapping("/queryItems")
    public ModelAndView queryItems() throws Exception{

        //调用service查找数据库,查询商品列表。这里使用静态数据模拟
        List<Items> itemsList = new ArrayList<Items>();
        //向list中添加静态数据
        Items items_1 = new Items();
        items_1.setName("联想笔记本");
        items_1.setPrice(6000f);
        items_1.setDetail("ThinkPad T430 联想笔记本电脑!");

        Items items_2 = new Items();
        items_2.setName("苹果手机");
        items_2.setPrice(5000f);
        items_2.setDetail("iphone6苹果手机!");

        itemsList.add(items_1);
        itemsList.add(items_2);

        //返回ModelAndView
        ModelAndView modelAndView = new ModelAndView();
        //相当于request的setAttribute()方法,以后在jsp页面中可以通过itemsList取数据
        modelAndView.addObject("itemsList", itemsList);

        //指定视图
        modelAndView.setViewName("/jsp/items/itemsList.jsp");

        return modelAndView;
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
第三步:配置Handler

第一种方式: 
可以使用与原来的配置Handler的方式一样,一个一个的在springmvc.xml中配置Handler。

<!-- 配置Handler -->
    <bean class="com.huihui.controller.ItemsController2"/>
 
 
  • 1
  • 2
  • 1
  • 2

但是这种方式存在一个问题:如果开发了很多个Handler,那么就要挨个在springmvc.xml中进行配置,非常的繁琐。下面这种方式解决了这个问题。

第二种方式: 
组件扫描器可以扫描controller、service…… 
使用组件扫描器省去在spring容器配置每个controller类的繁琐。使用<context:component-scan/>自动扫描标记@controller的控制器类,配置如下:

    <!-- 扫描controller注解,base-package指定要扫描的包。多个包中间使用半角逗号分隔 -->
    <context:component-scan base-package="com.huihui.controller"/>
 
 
  • 1
  • 2
  • 1
  • 2
第四步:部署访问

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值