zoukankan      html  css  js  c++  java
  • 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响应用户

     一.HandlerMapping,处理器映射器

    1.BeanNameUrl处理器映射器,根据请求的url与spring容器中定义的bean的name进行匹配,从而从spring容器中找到bean实例。

    <!—beanName Url映射器 -->

    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

     

    2.SimpleUrlHandlerMapping

    simpleUrlHandlerMapping是BeanNameUrlHandlerMapping的增强版本,它可以将url和处理器bean的id进行统一映射配置。

    <!—简单url映射 --

     <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"

           <property name="mappings">

               <props>

                  <prop key="/items1.action">controller的bean id</prop>

                  <prop key="/items2.action">controller的bean id</prop>

               </props>

           </property>

              </bean>

    二.HandlerAdapter,处理器适配器

       HandlerAdapter会根据适配器接口对后端控制器进行包装(适配),包装后即可对处理器进行执行,通过扩展处理器适配器可以执行多种类型的处理器,这里使用了适配器设计模式。

      1.SimpleControllerHandlerAdapter  

    SimpleControllerHandlerAdapter简单控制器处理器适配器,所有实现了org.springframework.web.servlet.mvc.Controller 接口的Bean通过此适配器进行适配、执行。

        适配器配置如下:

        <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />

      2.HttpRequestHandlerAdapter 

        HttpRequestHandlerAdapter,http请求处理器适配器,所有实现了org.springframework.web.HttpRequestHandler 接口的Bean通过此适配器进行适配、执行。

        适配器配置如下:

        <bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/>

         Controller实现如下:

          public class ItemList2 implements HttpRequestHandler{}

        从上边可以看出此适配器器的handleRequest方法没有返回ModelAndView,可通过response修改定义响应内容,比如返回json数据:

    response.setCharacterEncoding("utf-8");

    response.setContentType("application/json;charset=utf-8");

    response.getWriter().write("json串")

    三.注解映射器和适配器

      1.组件扫描器  

        使用组件扫描器省去在spring容器配置每个controller类的繁琐。使用<context:component-scan自动扫描标记@controller的控制器类,配置如下:

        <!-- 扫描controller注解,多个包中间使用半角逗号分隔 -->

           <context:component-scan base-package="cn.itcast.springmvc.controller.first"/>

      2.RequestMappingHandlerMapping

        注解式处理器映射器,对类中标记@ResquestMapping的方法进行映射,根据ResquestMapping定义的url匹配ResquestMapping标记的方法,

        匹配成功返回HandlerMethod对象给前端控制器,HandlerMethod对象中封装url对应的方法Method。

        从spring3.1版本开始,废除了DefaultAnnotationHandlerMapping的使用,推荐使用RequestMappingHandlerMapping完成注解式处理器映射。 

    配置如下:

      <!--注解映射器 -->

        <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>

      3.RequestMappingAdapter

        注解式处理器适配器,对标记@ResquestMapping的方法进行适配。

        从spring3.1版本开始,废除了AnnotationMethodHandlerAdapter的使用,推荐使用RequestMappingHandlerAdapter完成注解式处理器适配。

        配置如下:

          <!--注解适配器 -->

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

      4.<mvc:annotation-driven/>

    springmvc使用<mvc:annotation-driven>自动加载RequestMappingHandlerMapping和RequestMappingHandlerAdapter,

    可用在springmvc.xml配置文件中使用<mvc:annotation-driven>替代注解处理器和适配器的配置。

     

     

     

     

     

     

     

     

     

        

     

  • 相关阅读:
    css单位px,em,rem区别
    html视口单位:vw,vh,rem
    Codeforces Round #270 A. Design Tutorial: Learn from Math【数论/埃氏筛法】
    Codeforces Round #306 (Div. 2) A. Two Substrings【字符串/判断所给的字符串中是否包含不重叠的“BA” “AB”两个字符串】
    Codeforces Round #267 (Div. 2) B. Fedor and New Game【位运算/给你m+1个数让你判断所给数的二进制形式与第m+1个数不相同的位数是不是小于等于k,是的话就累计起来】
    Codeforces Round #369 (Div. 2) A. Bus to Udayland【字符串/二维字符数组求连起来的座位并改为其他字符】
    Codeforces Round #289 (Div. 2, ACM ICPC Rules) A. Maximum in Table【递推】
    Codeforces Round #191 (Div. 2) A. Flipping Game【*枚举/DP/每次操作可将区间[i,j](1=<i<=j<=n)内牌的状态翻转(即0变1,1变0),求一次翻转操作后,1的个数尽量多】
    Codeforces Round #377 (Div. 2) A. Buy a Shovel【暴力/口袋里面有无限枚 10 元和一枚 r 面值的硬币,问最少可以买多少把价值为 k 的铁铲】
    Good Bye 2016 A. New Year and Hurry【贪心/做题目每道题花费时间按步长为5等差增长,求剩余时间够做几道题】
  • 原文地址:https://www.cnblogs.com/ZSG-DoBestMe/p/5139155.html
Copyright © 2011-2022 走看看