什么是SpringMVC
百度:Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。简单来说,SpringMVC就是Spring在基于MVC思想的基础上研发出的框架。
SpringMVC流程
MVC思想:把页面/浏览器发来的请求通过Controller(控制器)调用Model(数据模型和业务模型)进行数据封装和业务处理,然后再把处理后的结果通过View(视图)展现出来。
SpringMVC的流程:
1. 用户发送请求到DispatcherServlet 控制器
2. DispatcherServlet 控制器根据请求路径到HandlerMapping映射器查询具体的handler处理器
3. HandlerMapping映射器根据用户请求查找与之对应的HandlerExecutionChain执行链再回传给DispatcherServlet控制器
4. DispatcherServlet控制器根据handler具体的实现方式调用HandlerAdapter适配器
5. HandlerAdapter适配器调用具体的handler处理器处理业务并返回ModelAndView到DispatcherServlet控制器
6. DispatcherServlet控制器将ModelAndView专递到ViewResolver视图解析器
7. ViewResolver视图解析器 返回具体的视图到DispatcherServlet控制器
8. DispatcherServlet控制器渲染视图后响应给用户
SpringMVC配置文件
配置文件(一般命名:springmvc-servlet.xml)的配置流程就是跟随着SpringMVC的流程进行配置,DispatcherServlet 是“springframework”下的依赖,不用配置,Model(handler)和View是自己代码实现,所以也不用配置,因此只用配置HandlerMapping(映射器)、HandlerAdapter(适配器)、ViewResolver(视图解析器)。又因为目前开发一般都会用到“注解”,就需要在配置文件中添加注解驱动<mvc:annotation-driven />,可以免去HandlerMapping(映射器)、HandlerAdapter(适配器)。
补充:<mvc:annotation-driven />的作用:https://www.cnblogs.com/afeng2010/p/10133797.html
DispatcherServlet 的父类是FrameworkServlet,点进去看到一段注释规定了配置文件的位置和命名规则:默认读取/WEB-INF/{servlet-name}-servlet.xml这个配置文件,一般默认为:springmvc-servlet.xml
springmvc-servlet.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
4 xmlns:context="http://www.springframework.org/schema/context"
5 xmlns:mvc="http://www.springframework.org/schema/mvc"
6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
7 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
9 <!-- 配置注解驱动,替代推荐使用的映射器以及适配器,json转换器 -->
10 <mvc:annotation-driven />
11 <!-- 开启注解扫描 -->
12 <context:component-scan base-package="com.demo.controller"></context:component-scan>
13 <!-- 配置视图解析器 -->
14 <!-- Example: prefix="/WEB-INF/jsp/", suffix=".jsp", viewname="test" -> "/WEB-INF/jsp/test.jsp" -->
15 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
16 <property name="prefix" value="/WEB-INF/views/"></property>
17 <property name="suffix" value=".jsp"></property>
18 </bean>
19 </beans>
项目demo:
项目结构:
pom.xml:
![](https://i-blog.csdnimg.cn/blog_migrate/1a85d6c852940bb900b2f6d6a0d5a595.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/8355b2c441464a7ce955b4faab19e5ad.gif)
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5 <modelVersion>4.0.0</modelVersion>
6
7 <groupId>com.demo.springmvc</groupId>
8 <artifactId>springmvc</artifactId>
9 <version>1.0-SNAPSHOT</version>
10 <packaging>war</packaging>
11
12 <name>springmvc Maven Webapp</name>
13 <!-- FIXME change it to the project's website -->
14 <url>http://www.example.com</url>
15
16 <properties>
17 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18 <maven.compiler.source>1.8</maven.compiler.source>
19 <maven.compiler.target>1.8</maven.compiler.target>
20 </properties>
21
22 <dependencies>
23 <dependency>
24 <groupId>org.springframework</groupId>
25 <artifactId>spring-webmvc</artifactId>
26 <version>5.1.7.RELEASE</version>
27 </dependency>
28 <dependency>
29 <groupId>org.slf4j</groupId>
30 <artifactId>slf4j-log4j12</artifactId>
31 <version>1.7.12</version>
32 </dependency>
33 <!-- JSP相关 -->
34 <dependency>
35 <groupId>jstl</groupId>
36 <artifactId>jstl</artifactId>
37 <version>1.2</version>
38 </dependency>
39 <dependency>
40 <groupId>javax.servlet</groupId>
41 <artifactId>servlet-api</artifactId>
42 <version>2.5</version>
43 </dependency>
44 <dependency>
45 <groupId>javax.servlet</groupId>
46 <artifactId>jsp-api</artifactId>
47 <version>RELEASE</version>
48 </dependency>
49 <dependency>
50 <groupId>junit</groupId>
51 <artifactId>junit</artifactId>
52 <version>4.11</version>
53 </dependency>
54 </dependencies>
55
56 <build>
57 <finalName>springmvc</finalName>
58 <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
59 <plugins>
60 <plugin>
61 <artifactId>maven-clean-plugin</artifactId>
62 <version>3.1.0</version>
63 </plugin>
64 <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
65 <plugin>
66 <artifactId>maven-resources-plugin</artifactId>
67 <version>3.0.2</version>
68 </plugin>
69 <plugin>
70 <artifactId>maven-compiler-plugin</artifactId>
71 <version>3.8.0</version>
72 </plugin>
73 <plugin>
74 <artifactId>maven-surefire-plugin</artifactId>
75 <version>2.22.1</version>
76 </plugin>
77 <plugin>
78 <artifactId>maven-war-plugin</artifactId>
79 <version>3.2.2</version>
80 </plugin>
81 <plugin>
82 <artifactId>maven-install-plugin</artifactId>
83 <version>2.5.2</version>
84 </plugin>
85 <plugin>
86 <artifactId>maven-deploy-plugin</artifactId>
87 <version>2.8.2</version>
88 </plugin>
89 </plugins>
90 </pluginManagement>
91 </build>
92 </project>
Web.xml:
![](https://i-blog.csdnimg.cn/blog_migrate/25f8da4e1749b040d374f63b5c25ca8b.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a2f5c3639017fcec2603ed902a739383.gif)
1 <!DOCTYPE web-app PUBLIC
2 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
3 "http://java.sun.com/dtd/web-app_2_3.dtd" >
4
5 <web-app>
6 <display-name>Archetype Created Web Application</display-name>
7
8 <servlet>
9 <servlet-name>springmvc</servlet-name>
10 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
11 <load-on-startup>1</load-on-startup>
12 </servlet>
13 <servlet-mapping>
14 <servlet-name>springmvc</servlet-name>
15 <!--
16 /*:拦截所有请求,包括jsp
17 / :拦截所有请求,不包含jsp
18 *.do,*.action
19 -->
20 <url-pattern>*.do</url-pattern>
21 </servlet-mapping>
22 <welcome-file-list>
23 <welcome-file>index.html</welcome-file>
24 <welcome-file>index.htm</welcome-file>
25 <welcome-file>index.jsp</welcome-file>
26 <welcome-file>default.html</welcome-file>
27 <welcome-file>default.htm</welcome-file>
28 <welcome-file>default.jsp</welcome-file>
29 </welcome-file-list>
30 </web-app>
index.jsp:
![](https://i-blog.csdnimg.cn/blog_migrate/a87b59989c9755e41231e28a32009977.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/20602cffa178aac289d403c1b9f04438.gif)
1 <%@ page language="java" contentType="text/html; charset=utf-8"
2 pageEncoding="utf-8" %>
3 <html>
4 <head>
5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
6 </head>
7 <body>
8 <h2>Hello World!</h2>
9 <form action="/hello.do">
10 <button type="submit" style="height: auto ; width: auto">跳转</button>
11 </form>
12 </body>
13 </html>
hello.jsp:
![](https://i-blog.csdnimg.cn/blog_migrate/a2d6e3c8570ebc230ad7004c6ce90028.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a7f40030f2e42ed1280b95e4082241db.gif)
1 <%@ page language="java" contentType="text/html; charset=utf-8" isELIgnored="false"
2 pageEncoding="utf-8"%>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4 <html>
5 <head>
6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
7 <title>Insert title here</title>
8 </head>
9 <body>
10 <!-- <span style="font-size:30px; color:red;">第一个springmvc程序</span> -->
11 <span style="font-size:30px; color:red;">${msg }</span>
12 </body>
13 </html>
springmvc-servlet.xml:
![](https://i-blog.csdnimg.cn/blog_migrate/52f8d6a76f89bb25a71a32bb0ddfd4f6.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/61d49087f7fdfc57efe0949aa9ace070.gif)
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
4 xmlns:context="http://www.springframework.org/schema/context"
5 xmlns:mvc="http://www.springframework.org/schema/mvc"
6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
7 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
9 <!-- 配置注解驱动,替代推荐使用的映射器以及适配器,json转换器 -->
10 <mvc:annotation-driven />
11 <!-- 开启注解扫描 -->
12 <context:component-scan base-package="com.demo.controller"></context:component-scan>
13 <!-- 配置视图解析器 -->
14 <!-- Example: prefix="/WEB-INF/jsp/", suffix=".jsp", viewname="test" -> "/WEB-INF/jsp/test.jsp" -->
15 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
16 <property name="prefix" value="/WEB-INF/views/"></property>
17 <property name="suffix" value=".jsp"></property>
18 </bean>
19 </beans>
mycontroller:
![](https://i-blog.csdnimg.cn/blog_migrate/cccdda8bf1c06654e7e426224117637a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/62c8d1162069ef17221e600f5bec157c.gif)
1 @Controller
2 public class mycontroller {
3 @RequestMapping("hello")
4 public ModelAndView test() {
5 ModelAndView mv = new ModelAndView();
6 //设置返回页面的名称
7 mv.setViewName("hello");
8 //设置返回信息
9 mv.addObject("msg", "HelloWorld!");
10 return mv;
11 }
12 }
SpringMVC注解
- Controller:表示该类为一个处理器,相当于 mycontroller implements Controller
- RequestMapping(value="hello"),配置方法的对应的url , 默认的url后缀和<url-pattern>*.do</url-pattern> 一致
- GetMapping:相当于RequestMapping(method = RequestMethod.GET)
- PostMapping:相当于RequestMapping(method = RequestMethod.POST)
- PutMapping:相当于RequestMapping(method = RequestMethod.PUT)
- DeleteMapping:相当于RequestMapping(method = RequestMethod.DELETE)