Springboot自定义返回指定页面的thymeleaf模板
时间: 2024-01-21 18:03:36 浏览: 209
要在Spring Boot中自定义返回指定页面的Thymeleaf模板,需要按照以下步骤进行操作:
1.在pom.xml文件中添加Thymeleaf依赖项:
```
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
```
2.在application.properties文件中配置Thymeleaf模板的路径:
```
spring.thymeleaf.prefix=classpath:/templates/
```
3.创建一个控制器类,设置@RequestMapping注解并返回指定的Thymeleaf模板:
```
@Controller
public class MyController {
@RequestMapping("/")
public String home(Model model) {
model.addAttribute("message", "Hello, Thymeleaf!");
return "home";
}
}
```
4.创建一个名为“home.html”的Thymeleaf模板文件,用于显示控制器方法中添加的消息。
```
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Home</title>
</head>
<body>
<h1 th:text="${message}"></h1>
</body>
</html>
```
5.运行应用程序,并访问“http://localhost:8080/”页面,应该会显示消息“Hello, Thymeleaf!”。
以上步骤将允许您自定义返回指定页面的Thymeleaf模板。
阅读全文
相关推荐















