软件架构技术(Java)之Spring MVC入门
Spring MVC是Spring提供的一个实现了Web MVC设计模式的轻量级Web框架,属于MVC框架。
在Java EE开发中,系统经典的三层架构包括表现层、业务层和持久层。三层架构中,每一层各司其职:
-
表现层(Web层)负责接收客户端请求,并向客户端响应结果。
-
业务层(Service层)负责业务逻辑处理,和项目需求息息相关。
-
持久层(Dao层)负责和数据库交互,对数据库表进行增删改查。
因此,Spring MVC在三层架构中的位置如下所示:

Spring MVC作用于三层架构中的表现层,用于接收客户端的请求并进行响应。Spring MVC中包含了控制器和视图。
-
控制器接收到客户端的请求后对请求数据进行解析和封装,接着将请求交给业务层处理。
-
业务层会对请求进行处理,最后将处理结果返回给表现层。
-
表现层接收到业务层的处理结果后,再由视图对处理结果进行渲染,渲染完成后响应给客户端。
二、编写第一个Spring MVC程序
1、对于maven项目,在pom.xml文件中添加依赖
<!--Spring MVC的核心--><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.2.8.RELEASE</version></dependency>
2、在resources目录下创建Spring配置文件applicationContext.xml
<?xml version="1.0" encoding="UTF-8" ?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"></beans>
3、在web.xml文件中添加监听器,用于创建ApplicationContext容器
<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>
4、在resources目录中创建Spring MVC配置文件:spring-mvc.xml
<?xml version="1.0" encoding="UTF-8" ?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.3.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.3.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.3.xsd"></beans>
5、在spring-mvc.xml文件中配置处理器映射信息和视图解析器
<!-- 配置 Spring MVC要扫描的包 --><context:component-scanbasepackage="com.itheima.controller"/><!-- 配置视图解析器 --><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><propertyname="prefix"value="/view/"/><propertyname="suffix"value=".jsp"/></bean>
6、在web.xml文件中配置前端控制器(本质上也是一个Servlet),用于拦截客户端的请求并进行转发
<servlet><servlet-name>DispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- 配置初始化参数,读取Spring MVC的配置文件 --><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-mvc.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>DispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping>
7、在项目的controller包中创建处理器,用于处理客户端的请求并指定响应时转跳的页面。
/*** Spring MVC程序开发:* 1、在pom.xml文件中添加Spring 、SpringMVC相关依赖* 2、在resources目录下创建* applicationContext.xml(Spring配置文件) 和 spring-mvc.xml(SpringMVC配置文件)文件* 3、在web.xml文件中配置ApplicationContext监听,创建容器,* 并加载applicationContext.xml文件*4、在web.xml文件中配置SpringMVC前端控制器(Servlet),* 并加载和初始化SpringMVC参数* 5、在spring-mvc.xml文件中配置处理器映射信息(包扫描),视图解析器(配置前缀、后缀)* 6、在控制层创建FirstController控制器(@Controller),并提供一个sayHello处理器,* 返回success.jsp页面,设置请求路径(@RequestMapping())*7、在view目录下 创建 success.jsp视图(页面)* 8、运行并访问*///设置当前类为处理器类@Controllerpublic class FirstController {// 设定当前方法的访问映射地址@RequestMapping("/firstController")// 设置当前方法返回值类型为String,用于指定请求完成后跳转的页面public String sayHello() {System.out.println("访问到FirstController!");// 设定具体跳转的页面return "success";}}
8、创建视图(view)页面。在项目的webapp/view文件夹下创建名称为view的文件夹,并在view文件夹下创建名称为success的jsp文件
<html><body><h2>Spring MVC FirstController!</h2></body></html>
9、启动项目,并访问:http://localhost:8080/say/hello
三、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进行渲染(即将模型数据填充至视图中);
-
视图渲染结果会返回给客户端浏览器显示。
仅用于交流学习!
夜雨聆风