1、Sping MVC 工作原理
舊文提過,不再贅述。請務必通讀以下文章:
https://mp.weixin.qq.com/s/z-fhmDa9iBwSG6OJx1x5hw
2、 IDEA 創建 web 項目
項目配置:
詳細配置見:https://www.cnblogs.com/shuaishuai1993/p/9795227.html
3、Spring MVC Hello World
IDEA 創建項目並配置:
- 創建一個名爲 mvc-hello 的動態 Web 項目,並在創建的項目中的 src 文件夾下創建一個包 com.nasus.hello。
- 加入 web 相關 pom 依賴。
- 在 com.nasus.hello 包下創建一個 HelloController 類。
- 在 webapp/WEB-INF 文件夾下創建 Spring 配置文件 web.xml 和 hello-servlet.xml。
- 在 webapp/WEB-INF 文件夾下創建一個名爲 jsp 的子文件夾。
- 在此子文件夾下創建視圖文件 hello.jsp。
pom 依賴
<!-- 1.Spring核心依賴 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<!-- 3.Spring web依賴 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
HelloController 類:
@Controller
@RequestMapping("/hello")
public class HelloController {
@RequestMapping(method = RequestMethod.GET)
public String sayHello(ModelMap model) {
model.addAttribute("data", "Hello World");
return "hello";
}
}
HelloController 指定了映射路徑,以及 http 訪問方式。並返回一個 ModelMap 和 hello 以供視圖解析器解析。
web.xml
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<!-- 加載順序 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</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:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 自動掃描 -->
<context:component-scan base-package="com.nasus.hello" />
<!-- 視圖解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
做了兩件事,一是指定這個 servlet 自動掃描哪些包,並激活帶 web 相關注解,@Controller、@RequestMapping 的類。
InternalResourceViewResolver 是視圖解析器,它定義瞭解析視圖名稱的規則。根據上面定義的規則,hello 的邏輯視圖將委託給位於 /WEB-INF/jsp/hello.jsp 這個視圖來實現。
hello.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h2>${data}</h2>
</body>
</html>
HelloController 返回的視圖名稱 “hello”,匹配到了 hello.jsp 。同時,hello.jsp 還取得返回的 model 中的內容。最後再由 DispatcherServlet 渲染視圖,返回瀏覽器,呈現給用戶。
訪問 http://127.0.0.1:8080/hello](http://127.0.0.1:8080/hello ,效果如下:
7、一些 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
8、源碼地址
https://github.com/turoDog/review_spring
本文主要覆盤了 Spring MVC 的工作原理,創建了一個 Spring MVC 項目來輔助理解原理,具體更多 Spring MCV 的用法,請見後續文章。
推薦閱讀: