1.創建一個springboot項目
在pom.xml中引入thymeleaf相關的依賴jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
在導入該依賴時一定要看看這個包的相關jar有沒有真的下載下來,我在使用idea進行拉jar包時,顯示拉下來了,其實並沒有,大坑。
可能會有如下錯誤
[Circular view path [login]: would dispatch back to the current handler URL [/login] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)] with root cause
當時我使用的是
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
</parent>
後面把父依賴換爲了
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
使用後面的父依賴順利下載了thymeleaf的依賴
2.不需要給application.yml添加或者修改配置
3.創建controller
@Controller
public class TestController {
@RequestMapping(value = "/login")
public String index() {
//跳轉到login.html頁面
return "login";
}
}
4.在resources目錄下的templates文件夾下創建一個login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
這是一個假的登錄頁面
</body>
</html>