1、Sping MVC 工作原理
旧文提过,不再赘述。请务必通读以下文章:
https://mp.weixin.qq.com/s/z-fhmDa9iBwSG6OJx1x5hw
2、IDEA 创建 web 项目
创建项目
填充信息
原始项目
项目配置:
创建 java 文件夹,放主要代码
创建 resource 文件夹,放配置文件
指定为 web 项目
重要配置
最终项目结构
详细配置见:
https://www.cnblogs.com/shuaishuai1993/p/9795227.html
3、Spring MVC Hello World
IDEA 创建项目并配置:
创建项目并配置
pom 依赖
1 <!-- 1.Spring核心依赖 -->
2 <dependency>
3 <groupId>org.springframework</groupId>
4 <artifactId>spring-core</artifactId>
5 <version>5.0.7.RELEASE</version>
6 </dependency>
7
8 <dependency>
9 <groupId>org.springframework</groupId>
10 <artifactId>spring-beans</artifactId>
11 <version>5.0.7.RELEASE</version>
12 </dependency>
13
14 <dependency>
15 <groupId>org.springframework</groupId>
16 <artifactId>spring-context</artifactId>
17 <version>5.0.7.RELEASE</version>
18 </dependency>
19
20 <dependency>
21 <groupId>org.springframework</groupId>
22 <artifactId>spring-aop</artifactId>
23 <version>5.0.7.RELEASE</version>
24 </dependency>
25
26 <!-- 3.Spring web依赖 -->
27 <dependency>
28 <groupId>org.springframework</groupId>
29 <artifactId>spring-web</artifactId>
30 <version>5.0.7.RELEASE</version>
31 </dependency>
32
33 <dependency>
34 <groupId>org.springframework</groupId>
35 <artifactId>spring-webmvc</artifactId>
36 <version>5.0.7.RELEASE</version>
37 </dependency>
HelloController 类:
1 @Controller
2 @RequestMapping("/hello")
3 public class HelloController {
4
5 @RequestMapping(method = RequestMethod.GET)
6 public String sayHello(ModelMap model) {
7 model.addAttribute("data", "Hello World");
8 return "hello";
9 }
10
11 }
HelloController 指定了映射路径,以及 http 访问方式。并返回一个 ModelMap 和 hello 以供视图解析器解析。
web.xml
1 <web-app id="WebApp_ID" version="2.4"
2 xmlns="http://java.sun.com/xml/ns/j2ee"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
5 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
6
7 <display-name>Spring MVC Application</display-name>
8
9 <servlet>
10 <servlet-name>hello</servlet-name>
11 <servlet-class>
12 org.springframework.web.servlet.DispatcherServlet
13 </servlet-class>
14 <!-- 加载顺序 -->
15 <load-on-startup>1</load-on-startup>
16 </servlet>
17
18 <servlet-mapping>
19 <servlet-name>hello</servlet-name>
20 <url-pattern>/</url-pattern>
21 </servlet-mapping>
22
23 </web-app>
web.xml 定义一个前端控制器 DispatcherServlet ,servlet-name 为 hello 。url-pattern 标签定义这个 servlet 将拦截所有的 url ,而 SpringMVC 默认将会从 webapp/WEB-INF 文件夹下加载 servlet-name 标签指定的名称 + -servlet.xml 格式的 servlet 配置文件。这里加载的 servlet 配置文件正是 hello-servlet.xml 。
hello-servlet.xml:
1 <beans xmlns="http://www.springframework.org/schema/beans"
2 xmlns:context="http://www.springframework.org/schema/context"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="
5 http://www.springframework.org/schema/beans
6 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
7 http://www.springframework.org/schema/context
8 http://www.springframework.org/schema/context/spring-context-3.0.xsd">
9
10 <!-- 自动扫描 -->
11 <context:component-scan base-package="com.nasus.hello" />
12
13 <!-- 视图解析器 -->
14 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
15 <property name="prefix" value="/WEB-INF/jsp/" />
16 <property name="suffix" value=".jsp" />
17 </bean>
18
19 </beans>
做了两件事,一是指定这个 servlet 自动扫描哪些包,并激活带 web 相关注解,@Controller、@RequestMapping 的类。
InternalResourceViewResolver 是视图解析器,它定义了解析视图名称的规则。根据上面定义的规则,hello 的逻辑视图将委托给位于 /WEB-INF/jsp/hello.jsp 这个视图来实现。
hello.jsp
1 <%@ page contentType="text/html; charset=UTF-8" %>
2 <html>
3 <head>
4 <title>Hello World</title>
5 </head>
6 <body>
7 <h2>${data}</h2>
8 </body>
9 </html>
HelloController 返回的视图名称 "hello",匹配到了 hello.jsp 。同时,hello.jsp 还取得返回的 model 中的内容。最后再由 DispatcherServlet 渲染视图,返回浏览器,呈现给用户。
访问 http://127.0.0.1:8080/hello](http://127.0.0.1:8080/hello ,效果如下:
页面效果
4、一些 Bug
IDEA 使用 tomcat 中文乱码问题,解决方法: https://blog.csdn.net/u012611878/article/details/80723491
IDEA 编译时出现,javacTask: 源发行版 1.8 需要目标发行版 1.8,解决方法: https://www.cnblogs.com/wormday/p/8424855.html https://www.cnblogs.com/benjieqiang/p/11298294.html
5、源码地址
https://github.com/turoDog/review_spring
本文主要复盘了 Spring MVC 的工作原理,创建了一个 Spring MVC 项目来辅助理解原理,具体更多 Spring MCV 的用法,请见后续文章。