前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringMVC<一> 基本结构与配置

SpringMVC<一> 基本结构与配置

作者头像
Rekent
发布2018-09-04 15:31:32
7360
发布2018-09-04 15:31:32
举报
文章被收录于专栏:日常分享日常分享

刚刚踏入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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-08-21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档