老师问我 Spring MVC 的工作流程

开发 架构
Spring MVC是Spring提供的一个实现了Web MVC设计模式的轻量级Web框架。它与Struts2框架一样,都属于MVC框架,但其使用和性能等方面比Struts2更加优异。

[[432210]]

GitHub:https://github.com/nateshao/ssm/tree/master/106-springmvc-hello

1. Spring MVC概述

什么是Spring MVC?

Spring MVC是Spring提供的一个实现了Web MVC设计模式的轻量级Web框架。它与Struts2框架一样,都属于MVC框架,但其使用和性能等方面比Struts2更加优异。

Spring MVC具有以下特点:

  1. 是Spring框架的一部分,可以方便的利用Spring所提供的其他功能。
  2. 灵活性强,易于与其他框架集成。
  3. 提供了一个前端控制器DispatcherServlet,使开发人员无需额外开发控制器对象。
  4. 可自动绑定用户输入,并能正确的转换数据类型。
  5. 内置了常见的校验器,可以校验用户输入。如果校验不能通过,那么就会重定向到输入表单。
  6. 支持国际化。可以根据用户区域显示多国语言。
  7. 支持多种视图技术。它支持JSP、Velocity和FreeMarker等视图技术。
  8. 使用基于XML的配置文件,在编辑后,不需要重新编译应用程序。

2. 第一个Spring MVC应用

在IDEA中,创建一个名称为106-springmvc-hello的Web项目,具体参考:https://github.com/nateshao/ssm/tree/master/106-springmvc-hello

在web.xml中,配置Spring MVC的前端控制器DispatcherServlet。

<?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_4_0.xsd" 
         version="4.0"
    <context-param> 
        <param-name>contextConfigLocation</param-name
        <param-value>/WEB-INF/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> 
        <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
        <servlet-name>dispatcher</servlet-name
<!--        <url-pattern>*.form</url-pattern>--> 
        <url-pattern>/</url-pattern> 
    </servlet-mapping> 
</web-app> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.

 

 

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" 
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"
 
    <context:component-scan base-package="com.nateshao"/> 
    <mvc:default-servlet-handler/> 
    <mvc:annotation-driven/> 
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
          id="internalResourceViewResolver"
        <property name="prefix" value="/WEB-INF/jsp/"/> 
        <property name="suffix" value=".jsp"/> 
    </bean> 
</beans> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

 

 

hello.jsp

<%-- 
  Created by IntelliJ IDEA. 
  User: 邵桐杰 
  Date: 2021/10/16 
  Time: 16:13 
  To change this template use File | Settings | File Templates. 
--%> 
<%@ page contentType="text/html;charset=UTF-8" language="java" %> 
<html> 
<head> 
    <title>Title</title> 
</head> 
<body> 
 <h2>hello springmvc</h2> 
</body> 
</html> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
<%-- 用EL表达式获取后台处理器封装的信息 --%> 
  $END
  • 1.
  • 2.

控制台输出:

浏览器访问:http://localhost:8080/106_springmvc_hello_war_exploded/hello

3. Spring MVC的工作流程(重点!!!)

Spring MVC是如何工作的呢?(面试高频)

“通过入门案例的学习,相信读者对Spring MVC的使用已经有了一个初步的了解。在实际开发中,我们的实际工作主要集中在控制器和视图页面上,但Spring MVC内部完成了很多工作,这些程序在项目中具体是怎么执行的呢?接下来,将通过一张图来展示Spring MVC程序的执行情况。

  • 用户通过浏览器向服务器发送请求,请求会被Spring MVC的前端控制器DispatcherServlet所拦截
  • DispatcherServlet拦截到请求后,会调用HandlerMapping处理器映射器;
  • 处理器映射器根据请求URL找到具体的处理器,生成处理器对象及处理器拦截器(如果有则生成)一并返回给DispatcherServlet;
  • DispatcherServlet会通过返回信息选择合适的HandlerAdapter(处理器适配器);
  • HandlerAdapter会调用并执行Handler(处理器),这里的处理器指的就是程序中编写的Controller类,也被称之为后端控制器;
  • Controller执行完成后,会返回一个ModelAndView对象,该对象中会包含视图名或包含模型和视图名;
  • HandlerAdapter将ModelAndView对象返回给DispatcherServlet;
  • DispatcherServlet会根据ModelAndView对象选择一个 合适的ViewReslover(视图解析器) ;
  • ViewReslover解析后,会向DispatcherServlet中返回具体的View(视图) ;
  • DispatcherServlet对View进行渲染( 即将模型数据填充至视图中) ;

总结

本章首先对Spring MVC框架进行了简单的介绍,

然后讲解了一个Spring MVC入门程序的编写,

最后通过入门案例对Spring MVC的工作流程进行了详细讲解。

 

通过本章的学习,我们能够了解什么是Spring MVC,以及Spring MVC的优点,掌握Spring MVC入门程序的编写,并能够熟悉Spring MVC框架的工作流程。

 

责任编辑:武晓燕 来源: 程序员千羽
相关推荐

2023-05-05 08:29:15

Spring后台服务器

2011-03-31 10:54:01

Cacti工作流程

2010-07-13 16:21:22

FIX协议

2024-03-26 08:08:08

SpringBPMN模型

2010-06-12 17:44:19

ARP协议

2009-06-05 10:26:05

struts工作流程

2022-11-02 15:11:44

LightHouseChrome插件

2010-09-27 10:19:09

DHCP工作流程

2010-06-23 14:46:54

DHCP协议

2010-07-28 17:19:28

ICMP协议

2010-06-24 16:40:16

Bittorrent协

2011-03-29 09:30:12

Cacti

2009-07-27 14:13:15

2011-08-08 15:14:11

PPPOE

2012-02-01 14:02:00

苹果产品开发

2010-07-26 14:55:56

Telnet服务器

2020-10-13 21:25:15

DevOps核心

2021-11-05 11:10:13

MyBatisSQL查询

2023-06-05 08:14:17

RabbitMQ兔子MQ开源

2010-08-30 09:07:12

DHCP工作流程
点赞
收藏

51CTO技术栈公众号