1. 程式人生 > >springMvc執行流程和配置檔案作用

springMvc執行流程和配置檔案作用

這裡寫圖片描述
1、使用者傳送請求至前端控制器DispatcherServlet
2、DispatcherServlet收到請求呼叫HandlerMapping處理器對映器。
3、處理器對映器根據請求url找到具體的處理器,生成處理器物件及處理器攔截器(如果有則生成)一併返回給DispatcherServlet。
4、DispatcherServlet通過HandlerAdapter處理器介面卡呼叫處理器
5、執行處理器(Controller,也叫後端控制器)。
6、Controller執行完成返回ModelAndView
7、HandlerAdapter將controller執行結果ModelAndView返回給DispatcherServlet
8、DispatcherServlet將ModelAndView傳給ViewReslover檢視解析器
9、ViewReslover解析後返回具體View
10、DispatcherServlet對View進行渲染檢視(即將模型資料填充至檢視中)。
11、DispatcherServlet響應使用者

Springmvc 主要配置檔案
建立ItemsController
ItemController是一個普通的java類,不需要實現任何介面,只需要在類上新增@Controller註解即可。@RequestMapping註解指定請求的url,其中“.action”可以加也可以不加。在ModelAndView物件中,將檢視設定為“/WEB-INF/jsp/itemList.jsp”
@Controller
publicclass ItemController {

@RequestMapping("/itemList")
public ModelAndView itemList() throws Exception {

    List<Items>itemList = new ArrayList<>();

    //商品列表
    Items items_1 = new Items();
    items_1.setName("聯想筆記本_3");
    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蘋果手機!");

    itemList.add(items_1);
    itemList.add(items_2);
    //建立modelandView物件
    ModelAndView modelAndView = new ModelAndView();
    //新增model
    modelAndView.addObject("itemList", itemList);
    //新增檢視
    modelAndView.setViewName("/WEB-INF/jsp/itemList.jsp");

// modelAndView.setViewName(“itemsList”);
returnmodelAndView;
}

}
ItemController使用modelandview介面
建立springmvc.xml

這裡寫程式碼片
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http:
//www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scanbase-package="cn.itcast.springmvc.controller"/> </beans>

配置前端控制器
在web.xml中新增DispatcherServlet的配置。


springmvc
org.springframework.web.servlet.DispatcherServlet

contextConfigLocation
classpath:springmvc.xml



springmvc
*.action

註解對映器和介面卡
元件掃描器
使用元件掃描器省去在spring容器配置每個controller類的繁瑣。使用自動掃描標記@controller的控制器類,配置如下:

<!-- 掃描controller註解,多個包中間使用半形逗號分隔 -->
    <context:component-scanbase-package="cn.itcast.springmvc.controller.first"/>

RequestMappingHandlerMapping
註解式處理器對映器,對類中標記@ResquestMapping的方法進行對映,根據ResquestMapping定義的url匹配ResquestMapping標記的方法,匹配成功返回HandlerMethod物件給前端控制器,HandlerMethod物件中封裝url對應的方法Method。

從spring3.1版本開始,廢除了DefaultAnnotationHandlerMapping的使用,推薦使用RequestMappingHandlerMapping完成註解式處理器對映。

配置如下:

<!--註解對映器 -->
    <beanclass="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>

註解描述:
@RequestMapping:定義請求url到處理器功能方法的對映

RequestMappingHandlerAdapter
註解式處理器介面卡,對標記@ResquestMapping的方法進行適配。

從spring3.1版本開始,廢除了AnnotationMethodHandlerAdapter的使用,推薦使用RequestMappingHandlerAdapter完成註解式處理器適配。

配置如下:

    <beanclass="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>


springmvc使用自動載入RequestMappingHandlerMapping和RequestMappingHandlerAdapter,可用在springmvc.xml配置檔案中使用替代註解處理器和介面卡的配置。
檢視解析器
在springmvc.xml檔案配置如下:

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

InternalResourceViewResolver:支援JSP檢視解析
viewClass:JstlView表示JSP模板頁面需要使用JSTL標籤庫,所以classpath中必須包含jstl的相關jar 包。此屬性可以不設定,預設為JstlView。
prefix 和suffix:查詢檢視頁面的字首和字尾,最終檢視的址為:
字首+邏輯檢視名+字尾,邏輯檢視名需要在controller中返回ModelAndView指定,比如邏輯檢視名為hello,則最終返回的jsp檢視地址 “WEB-INF/jsp/hello.jsp”