Spring Boot 12之 thymeleaf

总体步骤:html

(1)            在pom.xml中引入thymeleaf;java

(2)            如何关闭thymeleaf缓存spring

(3)            编写模板文件.html缓存

spring Boot默认就是使用thymeleaf模板引擎的,因此只须要在pom.xml加入依赖便可:app

一、 在pom.xml中引入thymeleaf;spring-boot

<dependency>spa

<groupId>org.springframework.boot</groupId>.net

<artifactId>spring-boot-starter-thymeleaf</artifactId>xml

</dependency>htm

二、在 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

三、建立一个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<String,Object> map){

map.put("hello","from TemplateController.helloHtml");

return"/helloHtml";

}

}

相关文章
相关标签/搜索