10.1 原理
首先,在idea搜索thymeleafProperties这个配置类。
通过源代码可以发现,使用的文件后缀是html,文件应该放在templates路径下:
10.1 依赖
直接加入启动器的thymeleaf依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
10.2 实验
在templates文件中创建一个hello.html。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>test</h1>
</body>
</html>
在controller文件夹(和HelloworldApplication类同级)中,创建一个控制类HelloController(注意:要是用@RestCOntroller否则会报错!):
package jiang.com.helloworld.controller;
import org.springframework.web.bind.annotation.*;
@RestController
public class HelloContrller {
@RequestMapping("/hello")
public String hello(){
return "helloWorld!";
}
}
然后启动程序,访问localhost:8080/hello: