SpringMVC<一> 基本结构与配置

刚刚踏入SpringMVC的学习,有一定Strust2的使用经验,边看书看博客,边总结,如有不对的地方还希望各位大佬多多指正。


Spring 响应过程与结构

  (1)用户在客户端发送一个HTTP请求,Web服务器接受到该请求,如果在web.xml中匹配DispatcherServlet的请求映射路径,Web容器将该请求转交给DispatcherServlet处理。

  (2)DispatcherServlet接受用户请求后,将根据请求信息以及HandlerMapping的配置找到处理请求的处理器(Controller)。可将HandlerMapping看成路由控制器,Controller看成目标主机。

  (3)当DispatcherServlet根据HandlerMapping得到对应当前请求的Controller后,通过HandlerAdaptor对Controller进行封装,再以统一的适配器接口调用Controller。

     HandlerAdapter是SpringMVC的框架级接口(适配器),使用统一的接口对各种Controller方法进行调用。

  (4)处理器完成业务逻辑的处理后,将返回一个ModelAndView(也支持更多其他的返回类型,String、Map等,若视图逻辑名缺失,默认是转发到HTTP发起的页面 此处更多资讯可以查看SpringMVC Controller 返回值的可选类型)给DispatcherServlet,ModelAndView包含视图逻辑名和模块数据信息。

  (5)DispatcherServlet借助ViewResolver完成逻辑视图名到真实视图对象的解析工作。

  (6)当得到真实视图对象View后,DispatcherServlet就使用该View对象对ModelAndView中的数据模型进行视图渲染

  (7)最终用户在客户端得到的响应信息,可能是一个普通的HTML页面,也可能是一个XML或者JSON串,甚至是一张图片或一个PDF文档等不同的媒体格式。  


  简单的说:DispatcherServlet相当与一个拦截收发站,拦截所有符合配置规则的请求,再转发到响应的Controller进行业务处理,业务处理后的数据交给ViewResovler进行视图渲染,完毕后,再

  交会给DisptacherServlet进行与前端输出。

  当然,我们更多的可能不会用到渲染的部分,作为后端我们更多的是获取数据,并进行业务处理,然后返回一个JSON给当前页面的AJAX进行后续的渲染。


  

                         请求处理流程


XML配置

WEB.XML

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:dispatcher-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

Dispatcher-Servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:beans="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">


    <!--开启注解-->
    <context:component-scan base-package="Controller" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
    </context:component-scan>

    <!--Spring3.1开始的注解 HandlerMapping -->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
    <!--Spring3.1开始的注解 HandlerAdapter -->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>

  <!--图像解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/" /> <property name="suffix" value=".jsp" /> </bean> </beans>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


</beans>

目录结构:

部分图片来源:http://jinnianshilongnian.iteye.com/blog/1594806

原文地址:https://www.cnblogs.com/rekent/p/7405552.html