Spring MVC入門案例
作為Spring非常重要的一個元件,Spring MVC在java web領域已經是當之無愧的領頭人物,能正確的使用Spring MVC非常重要,今天就一起搭建一個Spring MVC專案的骨架,一起入門下Spring MVC。
軟體
案例
-
新建maven專案,名稱可以任意起。我們的就叫做
me.aihe.learnspring
image
-
新增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的位置。

image
- 通過idea配置專案為web專案

image
- 配置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啟動的時候會載入
servlet名稱-servlet.xml
的檔案。所以我們要在WEB-INF下再新增一個檔案dispatcher-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專案啟動的包是否正確,對比下面的圖片。先按照下面的進行配置,每個包的細節後面再說。

image

image
- 啟動專案,可以看到正確的專案輸出。瀏覽器上看到頁面是正常的工作。

image

image
- 到這裡,我們的Spring MVC基本專案就搭建完成了,再後續開發的時候,需要不斷的新增依賴讓專案變得越來越複雜,但是複雜的專案也是由簡單的專案一步步來的,基礎的東西掌握紮實也很重要。
Spring Boot的web案例
鑑於Spring Boot構建一個web專案非常的簡單快速,這裡順便提及一下,而且近些年Spring Boot越來越火,掌握一下也是很有必要的。
-
基本上現在的IDE都會有Spring Boot的initializer. 我們用的是Idea,如圖。
image
-
勾選web依賴就可以。

image
- 直接建立一個Controller的java類就可以。

image
- 啟動專案

image
是不是非常的簡單,Spring Boot極大的簡化了我們開發的步驟,但是想要提高技術最好還是要明白其內部的原理。
最後
這次關於Spring MVC的環境搭建就說到這,本次主要介紹了使用Tomcat搭建Spring MVC專案與使用Spring Boot搭建web專案。
如果想對Spring MVC有更深入的理解,參考之前寫的一些關於Spring MVC原理分析的文章
ofollow,noindex">探索Spring