天天看點

springmvc(4)--注解的處理器映射器和擴充卡

1.我們在springmvc.xml中配置的擴充卡和映射器,如果不配置,那麼将會加載預設的映射器和擴充卡。

springmvc(4)--注解的處理器映射器和擴充卡

如圖

但是上面兩種注解方式的擴充卡和映射器,是已經過時的,是spring2.5的。

ps:

在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

在springmvc.xml中我們配置一下

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

而且可以使用mvc的直接驅動代替上面的controller的bean注冊,而且提供了很多參數綁定方法。

<!-- 使用 mvc:annotation-driven代替上邊注解映射器和注解擴充卡配置
    mvc:annotation-driven預設加載很多的參數綁定方法,
    比如json轉換解析器就預設加載了,如果使用mvc:annotation-driven不用配置上邊的RequestMappingHandlerMapping和RequestMappingHandlerAdapter
    實際開發時使用mvc:annotation-driven
     -->
    <!-- <mvc:annotation-driven></mvc:annotation-driven> -->
           

2.開發handler

開發注解的handler必須使用注解的擴充卡和映射器,不能使用非注解的

package com.ddd.ssm.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.ddd.ssm.pojo.Items;

/**
 * 使用注解開發handler
 * 
 * @author Dan
 *
 */

// 辨別它是一個控制器
@Controller
public class ItemsController3 implements HttpRequestHandler {
    // 查詢商品清單
    //@RequestMapping("/queryItems")實作了對queryItems()方法和url進行映射,一個方法對應一個url
    //一般方法和url一樣便于維護
    @RequestMapping("/queryItems33")
    public ModelAndView queryItems() {
        // 調用service查找資料庫,查詢商品清單,這裡使用靜态資料模拟
        List<Items> itemsList = new ArrayList<>();
        // 填充靜态資料
        Items items_1 = new Items();
        items_1.setName("皮鞋");
        items_1.setPrice(f);
        items_1.setDetail("稻草人皮鞋!");

        Items items_2 = new Items();
        items_2.setName("水杯");
        items_2.setPrice(f);
        items_2.setDetail("特百惠水杯!");

        itemsList.add(items_1);
        itemsList.add(items_2);
        // 傳回modelAndView
        ModelAndView modelAndView = new ModelAndView();
        // 相當于request.setAttribute(..,..),就是在jsp頁面中通過itemsList取資料
        modelAndView.addObject("itemsList", itemsList);
        // 指定視圖
        modelAndView.setViewName("/items/itemsList");
        return modelAndView;

    }

}
           

3.配置handler

使用元件掃描的方式

<!-- 對于注解的Handler可以單個配置實際開發中建議使用元件掃描 -->
<!-- <bean class="com.ddd.ssm.controller.ItemsController3"/> -->
<!-- 可以掃描controller、service、...這裡讓掃描controller,指定controller的包 -->
<context:component-scan base-package="com.ddd.ssm.controller">
</context:component-scan>
           

4.測試

springmvc(4)--注解的處理器映射器和擴充卡

測試成功!

5.源碼分析

通過前端控制器源碼分析springmvc的執行過程:

第一步:執行這個方法:

springmvc(4)--注解的處理器映射器和擴充卡

第二步:找到handler

springmvc(4)--注解的處理器映射器和擴充卡

第三步:執行handler,得到執行的結果modelAndView

springmvc(4)--注解的處理器映射器和擴充卡

第四步:調用視圖解析器,進行視圖渲染,将modelAndView填充

得到view:

springmvc(4)--注解的處理器映射器和擴充卡

填充資料model到request域

springmvc(4)--注解的處理器映射器和擴充卡

渲染:

springmvc(4)--注解的處理器映射器和擴充卡

6.小結

使用注解的映射器不用在xml中配置url和handler的映射關系

使用注解的映射器和注解的擴充卡配對使用,也就是說不能使用非注解的映射器和注解的擴充卡使用。

繼續閱讀