Spring Boot 12之 thymeleaf

来源:互联网 发布:wwwtaoyitu淘宝号查询 编辑:程序博客网 时间:2024/05/18 13:07

整体步骤:
(1) 在pom.xml中引入thymeleaf;
(2) 如何关闭thymeleaf缓存
(3) 编写模板文件.html
spring Boot默认就是使用thymeleaf模板引擎的,所以只需要在pom.xml加入依赖即可:

1、 在pom.xml中引入thymeleaf;

org.springframework.boot
spring-boot-starter-thymeleaf

2、在 src/main/resources/templates下加入Thymeleaf模板。
Thymeleaf缓存在开发过程中,肯定是不行的,那么就要在开发的时候把缓存关闭,
只需要在application.properties进行配置即可:

spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html

thymeleaf start

spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html

开发时关闭缓存,不然没法看到实时页面

spring.thymeleaf.cache=false

在spring-boot下,默认约定了Controller试图跳转中thymeleaf模板文件的的前缀prefix是”classpath:/templates/”,后缀suffix是”.html”
这个在application.properties配置文件中是可以修改的。
如下配置可以修改试图跳转的前缀和后缀
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html

3、创建一个Controller

@Controller
public class TemplateController {
/**
* 返回html模板.
*/
@RequestMapping(“/helloHtml”)
public String helloHtml(Model model){
model.addAttribute(“hello”,”from TemplateController.helloHtml”);
return “/helloHtml”;
}
@RequestMapping(“/helloHtml2”)
public String helloHtml(Map

0 0