首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Spring Boot 集成 Thymeleaf 模板

1. pom.xml 文件里添加 Thymeleaf 模板依赖

代码语言:javascript
复制
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

2. application.properties 文件中添加 Thymeleaf 模板配置

代码语言:javascript
复制
### thymeleaf 相关配置 ###
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
# 关闭缓存,即时刷新,生产环境应改为true
spring.thymeleaf.cache=false

3. 代码示例

代码语言:javascript
复制
package com.codingos.springbootdemo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.codingos.springbootdemo.entity.Resource;

@Controller
@RequestMapping("/th")
public class ThymeleafController {

	
	@RequestMapping("/index")
	public String index(Model model) {
		model.addAttribute("test", "Thymeleaf 示例");
		return "thymeleaf/index";
	}
	
	@RequestMapping("/center")
	public String center() {
		return "thymeleaf/center/center";
	}
}
下一篇
举报
领券