Spring Boot 集成 Thymeleaf 模板 原

1. pom.xml 文件裏添加 Thymeleaf 模板依賴

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

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

### 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. 代碼示例

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";
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章