1. 新建一个SpringBoot项目,添加web和thymeleaf的依赖。 在 pom.xml中的代码:html
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
2. 配置属性。Spring Boot官方文档建议在开发时将缓存关闭。 在application.yml中的spring: 下面添加代码web
spring: thymeleaf: prefix: classpath:/templates/ check-template-location: true suffix: .html encoding: UTF-8 mode: LEGACYHTML5 cache: false
3. 编写Controller类。spring
@Api("网站首页和关于页面") @Controller public class HomeController { @ApiIgnore @GetMapping(value = {"", "/index"}) public String index() { return "site/index"; } }
4. 在resources/templates 下面建立一个site文件夹,建立一个index.html文件。缓存
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>使用thymeleaf</title> </head> <body> <h1>Hello, Spring Boot!</h1> </body> </html>
访问 localhost:8080或者localhost:8080/indexapp