目录
MVC是一种软件架构的思想,将软件按照模型、视图、控制器来划分。
M:Model,模型层,就是工程中的JavaBean,作用于处理数据。
JavaBean两类分别为:①、实体类Bean:专门存储业务数据,如user等。②、业务处理Bean:指Server或Dao对象,专门用于处理业务逻辑和数据访问。
V:View,视图层:指工程中的html或jsp等页面,作用是与用户进行交互,展示数据。
C:Controller,控制层:指工程中的servlet,作用是接收请求和响应浏览器。
MVC工作流程:用户通过视图层发送请求到服务器,在服务器中请求被Controller接收,Contrller层调用相应的Model层处理请求,处理完毕将结果返回给Controller层,Controller再根据请求处理的结果找到相对应的View视图,渲染数据后最终响应给浏览器。
SpringMVC是Spring的一个后续产品,是Spring的子项目。
SpringMVC是Spring为表述层开发提供了一整套完备的解决方案。在表述层框架经历了Struct、WebWork、Struct2等产品的历代更迭后,目前业界主要选择SpringMVC作为Java EE项目表述层开发的首选方案。
三层架构主要分为表述层、业务逻辑层、数据访问层,表述层表示前台页面和后台servlet。
1、Spring家族原生产品,与IOC容器等基础设施无缝对接。
2、基于原生的Servlet,通过功能强大的前端控制器DispatcherServlet,对请求和响应进行统一处理。
3、表述层各细分领域需要解决的问题全方位覆盖、提供全面解决方案
4、代码清新简洁、大幅度提升开发效率。
5、内部组件化程度高,可插拔式组件即插即用,根据需要的功能配置相应组件即可。
6、性能卓著,适合现代大型互联网项目要求。
开发工具:idea2019及以后版本都行
构建工具:maven3.8.4
服务器:tomcat9
Spring版本:5.3.1
添加依赖并导入
- <packaging>warpackaging>
-
- <dependencies>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-webmvcartifactId>
- <version>5.3.1version>
- dependency>
-
- <dependency>
- <groupId>ch.qos.logbackgroupId>
- <artifactId>logback-classicartifactId>
- <version>1.2.3version>
- dependency>
-
- <dependency>
- <groupId>javax.servletgroupId>
- <artifactId>javax.servlet-apiartifactId>
- <version>3.1.0version>
- <scope>providedscope>
- dependency>
- <dependency>
- <groupId>org.thymeleafgroupId>
- <artifactId>thymeleaf-spring5artifactId>
- <version>3.0.12.RELEASEversion>
- dependency>
- dependencies>
添加webapp目录

添加web.xml文件


配web.xml的默认方式(只能在WEB-INF目录下)
- "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">
- <servlet>
- <servlet-name>springMVCservlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
- servlet>
- <servlet-mapping>
- <servlet-name>springMVCservlet-name>
-
- <url-pattern>/url-pattern>
- servlet-mapping>
- web-app>
/所匹配的请求可以是/login或.html或.js或.css方式的请求路径但/不能匹配.jsp请求路径的请求,因此就可以避免在访问jsp页面时,该请求被DispatcherServlet处理,从而产生找不到相应页面的错误。/*是能够匹配所有请求,如使用过滤器时,需要对所有请求进行过滤,就需要使用/*。
配web.xml的扩展方式
- "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">
-
- <servlet>
- <servlet-name>springMVCservlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
-
- <init-param>
- <param-name>contextConfigLocationparam-name>
- <param-value>classpath:springMVC.xmlparam-value>
- init-param>
-
- <load-on-startup>1load-on-startup>
- servlet>
- <servlet-mapping>
- <servlet-name>springMVCservlet-name>
-
- <url-pattern>/url-pattern>
- servlet-mapping>
- web-app>

前端控制器对浏览器发送的请求进行统一处理,但具体的请求有不同的处理过程,因此需要创建处理具体请求的类,就是请求控制器。
请求控制器中的每一个处理请求的方法成为控制器方法。
SpringMVC的控制器由一个POJO(普通Java类)担任,因此需要通过@Controller注解将其标识为一个控制层组件,交给Spring的IOC容器管理,此时SpringMVC才能够识别控制器的存在。

- @Controller
- public class HelloController {
-
- }
- "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"
- 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">
-
- <context:component-scan base-package="com.cjc.springmvc.controller">context:component-scan>
-
- <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
- <property name="order" value="1"/>
- <property name="characterEncoding" value="UTF-8"/>
- <property name="templateEngine">
- <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
- <property name="templateResolver">
- <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
-
- <property name="prefix" value="/WEB-INF/templates/"/>
-
- <property name="suffix" value=".html"/>
- <property name="templateMode" value="HTML5"/>
- <property name="characterEncoding" value="UTF-8"/>
- bean>
- property>
- bean>
- property>
- bean>
- beans>
index.html页面
- html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>首页title>
- head>
- <body>
- <h1>首页h1>
- body>
- html>
controller层方法
- @Controller
- public class HelloController {
- @RequestMapping("/")
- public String index(){
- //返回视图名称
- return "index";
- }
- }
配置tomcat


测试访问

index.html页面
- html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>首页title>
- head>
- <body>
- <h1>首页h1>
- <a th:href="@{/target}">访问指定页面targeta>
- body>
- html>
target.html页面
- html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>Titletitle>
- head>
- <body>
- 你好,跳转成功
- body>
- html>
controller层代码
- @Controller
- public class HelloController {
- @RequestMapping("/")
- public String index(){
- //返回视图名称
- return "index";
- }
- @RequestMapping("/target")
- public String getTarget(){
- return "target";
- }
- }
测试访问:

总结:浏览器发送请求,若请求地址符合前端控制器的url-pattern,该请求就会被前端控制器DispatcherServlet处理。前端控制器会读取SpringMVC的核心配置文件,通过扫描组件找到控制器,将请求地址和控制器中的@RequestMapping注解的value属性进行匹配,若匹配成功,该注解所标识的控制器方法就是处理请求的方法。处理请求的方法需要返回一个字符串类型的视图名称,该视图名称会被视图解析器解析,加上前缀和后缀组成视图路径,通过Thymeleaf对视图进行渲染,最终转发到视图所对应页面。