搭建SpringBoot的web-mvc項目
一、Spring Boot簡介
Spring Boot是由Pivotal團隊提供的全新架構,其設計目的是用來簡化新Spring應用的初始搭建以及開發過程。該架構使用了特定的方式來進行配置,進而使開發人員不再需要定義樣闆化的配置。通過這種方式,Boot緻力于在蓬勃發展的快速應用開發領域(rapid application development)成為上司者。
本文是一個springboot入門級的web-mvc項目。
二、maven安裝與配置
win8和win10配置Maven
三、用Spring Boot建立web-mvc項目
這裡先介紹一下各種環境的配置資訊:eclipse_neon2(4.6) 和 jdk1.8
因為SpringBoot中是内置tomcat的,是以也就不需要額外的tomcat配置了,現在開始講如何在idea上面搭建SpringBoot web-mvc項目了
步驟一:在ECLIPSE中建立一個正常的maven項目,具體步驟請看看下面的圖示:
注意,要勾選Create a simple progect,這樣就不會建立項目骨架,不會從外網下載下傳一些東西,導緻會卡在那,下載下傳東西的時間,還不如手工去建立一下目錄,分分鐘搞定
然後輸入相應的groupId,artifactId,項目建好後,目錄結構是這樣的:
步驟二:右邊是pom.xml檔案
在resources目錄下建立WEB-INF目錄,這個是web項目都該有的目錄
在resources目錄下建立templates目錄,(可以是velocity的vm模闆放置的地方)
好,接下來修改pom.xml,我直接貼一個最小配置
通過圖上面的幾個步驟,一個基本的maven項目就搭建完成了,接下來就是開始搭建SpringBoot中各種配置檔案資訊了。
pox.xml
<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>ppl.study</groupId>
<artifactId>spring-boot-demo1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-demo1</name>
<description>spring-boot-demo1-BY PPL</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
解釋:
1.在pom.xml中引入spring-boot-start-parent,spring官方的解釋叫什麼stater poms,它可以提供dependency management,也就是說依賴管理,引入以後在申明其它dependency的時候就不需要version了,後面可以看到。
2.因為我們開發的是web工程,是以需要在pom.xml中引入spring-boot-starter-web,spring官方解釋說spring-boot-start-web包含了spring webmvc和tomcat等web開發的特性。
3.如果我們要直接Main啟動spring,那麼以下plugin必須要添加,否則是無法啟動的。如果使用maven 的spring-boot:run的話是不需要此配置的。(我在測試的時候,如果不配置下面的plugin也是直接在Main中運作的。)
步驟三:.配置resources下面的Web資源檔案,這裡我再配置兩個檔案,一個是用來存放靜态檔案夾的static檔案,還有一個就是用來存放HTML的資源檔案夾templates。
這裡需要特别主要的是:static檔案中一般存放css,js,image等靜态資源檔案,而templates檔案中一般存放各種HTML檔案。而且這兩個檔案都是預設存在的,路徑不需要特别的配置就可以直接引用了。
application.properties是個配置檔案,這裡面可以配置SpringBoot的相關資訊。大家需要注意的是這個檔案名千萬不要寫錯,也不要放錯位置,不然都不會生效的。
#修改tomcat的預設的端口号,将8080改為8888
server.port=8888
下面看圖示案例和代碼案例: (修正:com.ppl.app包刪除,把DemoApplication.java移到controller包下)
DemoApplication.Java和HelloController.java具體代碼:
package com.ppl.controller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
package com.ppl.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping("/index")
public String index() {
return "welcome";
}
}
welcome.html具體代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"></meta>
<title>Insert title here</title>
</head>
<body>
<p>welcome page is login.........</p>
</body>
</html>
四、啟動
這樣SpringBoot的Web-mvc項目就已經搭建成功了,具體步驟就是這樣的。run main函數。
還有一點需要主要的是:因為我已經把端口号給修改了,是以通路的時候位址就要寫成 127.0.0.1:8888/index 。
備注:
運作我們的Application了,我們先介紹第一種運作方式。
第一種方式特别簡單:右鍵Run As -> Java Application。之後打開浏覽器輸入位址:http://127.0.0.1:8888/ 就可以看到Hello world!了。
第二種方式右鍵project – Run as – Maven build – 在Goals裡輸入spring-boot:run ,然後Apply,最後點選Run。
浏覽器通路:http://localhost:8888/index
處理靜态檔案
浏覽器頁面使用HTML作為描述語言,那麼必然也脫離不了CSS以及JavaScript。
為了能夠浏覽器能夠正确加載類似/css/style.css, /js/main.js等資源,預設情況下我們隻需要在src/main/resources/static目錄下添加css/style.css和js/main.js檔案後,
Spring MVC能夠自動将他們釋出,通過通路/css/style.css, /js/main.js也就可以正确加載這些資源。