添加thymeleaf依赖
<!--thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
配置application.properties
#thymeleaf start
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
#开发时关闭缓存,不然没法看到实时页面
spring.thymeleaf.cache=false
#thymeleaf end
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties这个类里面有默认的配置。
spring-boot很多配置都有默认配置:
比如默认页面映射路径为classpath:/templates/*.html
同样静态文件路径为classpath:/static/
登录页面AdminController
package com.moxi.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/admin")
public class AdminController {
@GetMapping("/login")
public String login(Model model) {
model.addAttribute("projectName", "MOXI");
return "login";
}
@GetMapping("/register")
public String register(Model model) {
model.addAttribute("projectName", "MOXI");
return "register";
}
}
引入文件
如图,引入相应的样式、图片和js文件,引入页面文件:
新建html文件
login.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=d