1.創(chuàng)建Spring Boot項目,若是初次安裝滋将,未有項目則如下圖所示:
image.png
若是已經打開某個項目屑彻,如下圖所示,new Project
image.png
image.png
image.png
image.png
通過圖上面的幾個步驟润梯,一個基本的maven項目就搭建完成了过牙,接下來就是開始搭建SpringBoot中各種配置文件信息了甥厦。
2.復制以下代碼到pom.xml中,我們的項目使用的thymeleaf的模板
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.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</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>
點擊maven中jar包依賴更新按鈕寇钉,具體操作看下面圖示:
image.png
3.配置resources下面的Web資源文件矫渔,這里我就配置兩個文件,一個是用來存放靜態(tài)文件夾的public文件摧莽,還有一個就是用來存放HTML的資源文件夾templates。
這里需要特別主要的是:public文件中一般存放css顿痪,js镊辕,image等靜態(tài)資源文件,而templates文件中一般存放各種HTML文件蚁袭。而且這兩個文件都是默認存在的征懈,路徑不需要特別的配置就可以直接引用了。
application.yml是個配置文件揩悄,這里面可以配置SpringBoot的相關信息卖哎。大家需要注意的是這個文件名千萬不要寫錯,也不要放錯位置删性,不然都不會生效的亏娜。
image.png
4.代碼實現(xiàn)。
Application.java具體代碼:
package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
NavController.java具體代碼:
package com.Controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* Created by CJF on 2018/3/27.
*/
@Controller
public class NavController {
@RequestMapping("/index")
public String index() { return "index"; }
}
index.css具體代碼:
body {
padding: 0px;
margin: auto;
font-family: "黑體", "仿宋", Arial, "Arial Unicode MS", System;
font-size: 20px;
text-align: left;
}
index.js具體代碼:
console.log("Hello World...")
index.html具體代碼:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns="http://www.w3.org/1999/html">
<head lang="en">
<meta charset="UTF-8"/>
<title>Hello World</title>
<link th:href="@{/css/index.css}" rel="stylesheet"/>
</head>
<body>
Hello World
<script th:src="@{js/index.js}"></script>
</body>
</html>
application.yml具體代碼:
server:
port: ${port:8099}
sessionTimeout: 30
contextPath: /
在Application.java文件上右鍵蹬挺,運行項目:
image.png
訪問http://localhost:8099/index
维贺,頁面運行如下所示。這里的端口在application.yml中進行了修改巴帮。
image.png