作為Spring非常重要的一個元件,Spring MVC在java web領域已經是當之無愧的領頭人物,能正确的使用Spring MVC非常重要,今天就一起搭建一個Spring MVC項目的骨架,一起入門下Spring MVC。
軟體
- Spring 4.x
- Tomcat 8.x
- MacOS(不限,Java是跨語言的)
案例
- 建立maven項目,名稱可以任意起。我們的就叫做
me.aihe.learnspring
imageSpring MVC入門案例 - 添加Spring與spring mvc的依賴。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>me.aihe</groupId>
<artifactId>learnspring</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<spring.version>4.3.14.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</project>
- 配置項目為tomcat項目,記得提前下載下傳好tomcat軟體,我們通過idea指定tomcat的位置。
- 通過idea配置項目為web項目
- 配置Java Web項目的web.xml檔案。
<?xml version="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>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
- 配置dispatcher-servlet.xml,tomcat啟動的時候會加載
的檔案。是以我們要在WEB-INF下再添加一個檔案dispatcher-servlet.xml,其内容如下servlet名稱-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="me.aihe" />
</beans>
- 建立一個新的Controller來測試。
package me.aihe;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* description: 該檔案說明
*
* @author aihe
* @version 1.0
* @date 2018/11/6
*/
@Controller
public class FirstController {
@RequestMapping({"/","/index"})
@ResponseBody
public String index(){
return "index";
}
}
- 再次确認下,項目是否為正确的web項目,idea項目啟動的包是否正确,對比下面的圖檔。先按照下面的進行配置,每個包的細節後面再說。
- 啟動項目,可以看到正确的項目輸出。浏覽器上看到頁面是正常的工作。
- 到這裡,我們的Spring MVC基本項目就搭建完成了,再後續開發的時候,需要不斷的添加依賴讓項目變得越來越複雜,但是複雜的項目也是由簡單的項目一步步來的,基礎的東西掌握紮實也很重要。
Spring Boot的web案例
鑒于Spring Boot建構一個web項目非常的簡單快速,這裡順便提及一下,而且近些年Spring Boot越來越火,掌握一下也是很有必要的。
- 基本上現在的IDE都會有Spring Boot的initializer. 我們用的是Idea,如圖。
Spring MVC入門案例 - 勾選web依賴就可以。
- 直接建立一個Controller的java類就可以。
- 啟動項目
是不是非常的簡單,Spring Boot極大的簡化了我們開發的步驟,但是想要提高技術最好還是要明白其内部的原理。
最後
這次關于Spring MVC的環境搭建就說到這,本次主要介紹了使用Tomcat搭建Spring MVC項目與使用Spring Boot搭建web項目。
如果想對Spring MVC有更深入的了解,參考之前寫的一些關于Spring MVC原理分析的文章
探索Spring