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>替代注解处理器和适配器的配置。

 

 

 

 

 

 

 

 

 

    

 

原文地址:https://www.cnblogs.com/ZSG-DoBestMe/p/5139155.html