thymeleaf使用和语法
1. 引入thymeleaf
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2. thymeleaf使用和语法
@ConfigurationProperties(
prefix = "spring.thymeleaf"
)
public class ThymeleafProperties {
private static final Charset DEFAULT_ENCODING;
//默认的前缀
public static final String DEFAULT_PREFIX = "classpath:/templates/";
//默认的后缀
public static final String DEFAULT_SUFFIX = ".html";
//只要把html页面放在classpath:/templates/,thymeleaf就会自动渲染
只要把html页面放在classpath:/templates/,thymeleaf就会自动渲染
使用:
- 导入thymeleaf的名称空间
<html lang="en" xmlns:th="http://www.thymeleaf.org">
- 使用thymeleaf语法
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h2>成功页面</h2>
<!--th:text="" 将div里面的内容设置为-->
<div th:text="${hello}"></div>
</body>
</html>
扩展SpringMVC
编写一个配置类(@Configuration),是继承WebMvcConfigurer借口的(在SpringBoot 2之后的版本改用WebMvcConfigurer这个接口来注册controllers);不能标注@EnableWebMvc。
//使用WebMvcConfigurer用来拓展SpringMVC的功能
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//浏览器发送 /wuhu 来到 success页面
registry.addViewController("/wuhu").setViewName("success");
}
}
使用@EnableWebMvc将会全面接管SpringMVC配置,自动配置将失效。