1、引入Thymeleaf模板引擎
org.springframework.boot
spring-boot-starter-thymeleaf
2.2.1.RELEASE
2、配置属性
其实可以直接使用,不用配置。但Spring Boot官方文档建议在开发时将缓存关闭,那就在application.properties文件中加入下面这行就行了:
spring.thymeleaf.cache=false
3、Thymeleaf使用
源文档
package org.springframework.boot.autoconfigure.thymeleaf;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.http.MediaType;
import org.springframework.util.MimeType;
import org.springframework.util.unit.DataSize;
@ConfigurationProperties(prefix = "spring.thymeleaf")
public class ThymeleafProperties {
private static final Charset DEFAULT_ENCODING = StandardCharsets.UTF_8;
public static final String DEFAULT_PREFIX = "classpath:/templates/";
public static final String DEFAULT_SUFFIX = ".html";
/**
* Whether to check that the template exists before rendering it.
*/
private boolean checkTemplate = true;
/**
* Whether to check that the templates location exists.
*/
private boolean checkTemplateLocation = true;
/**
* Prefix that gets prepended to view names when building a URL.
*/
private String prefix = DEFAULT_PREFIX;
/**
* Suffix that gets appended to view names when building a URL.
*/
private String suffix = DEFAULT_SUFFIX;
/**郑州人流医院 http://mobile.zzzzyy120.com/
* Template mode to be applied to templates. See also Thymeleaf's TemplateMode enum.
*/
private String mode = "HTML";
/**
* Template files encoding.
*/
private Charset encoding = DEFAULT_ENCODING;
/**
* Whether to enable template caching.
*/
private boolean cache = true;
}
}
只要把HTML页面放在classpath:/templates/,Thymeleaf就能自动渲染:
使用
1、导入thymeleaf的名称空间( )
Hello静态资源模板引擎配置成功
2、编写Controller类
package com.hern.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @program: springboot-web-study
* @description: 测试环境配置
* @author:
* @create: 2019-12-02 16:51
**/
@Controller
public class HelloController {
@RequestMapping(value = "hello")
public String hello(){
return "hello";
}
}
3、在浏览器地址栏中输入localhost:8080/hello进行测试