SpringMVC<一> 基本结构与配置

本文详细介绍了SpringMVC的工作原理及请求处理流程,包括DispatcherServlet的作用、如何配置HandlerMapping和HandlerAdapter,以及视图渲染的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

### 5.4 RESTful案例 #### 5.4.1 需求分析 需求:图书列表查询,从后台返回数据,将数据展示在页面上 ![1630508310063](assets/1630508310063.png) 需求二:新增图书,将新增图书的数据传递到后台,并在控制台打印 ![1630508367105](assets/1630508367105.png) **说明:**此次案例的重点是在SpringMVC中如何使用RESTful实现前后台交互,所以本案例并没有和数据库进行交互,所有数据使用`假`数据来完成开发。 步骤分析: > 1.搭建项目导入jar包 > > 2.编写Controller类,提供两个方法,个用来做列表查询,个用来做新增 > > 3.在方法上使用RESTful进行路径设置 > > 4.完成请求、参数的接收和结果的响应 > > 5.使用PostMan进行测试 > > 6.将前端页面拷贝到项目中 > > 7.页面发送ajax请求 > > 8.完成页面数据的展示 #### 5.4.2 环境准备 - 创建个Web的Maven项目 - pom.xml添加Spring依赖 ```xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.itheima</groupId> <artifactId>springmvc_07_rest_case</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.10.RELEASE</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.1</version> <configuration> <port>80</port> <path>/</path> </configuration> </plugin> </plugins> </build> </project> ``` - 创建对应的配置类 ```java public class ServletContainersInitConfig exten
最新发布
04-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值