SpringBoot1.5.12.RELEASE整合Thymeleaf

SpringBoot1.5.12.RELEASE整合Thymeleaf

1.首先导入Thymeleaf的场景启动器,也就Maven坐标

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

1.1 SpringBoot1.5.12.RELEASE版本默认Thymeleaf的版本是2.x.x,如果想修改Thymeleaf的默认版本,可以参考如下配置,如果不想修改,可不配置

<properties>
		<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
		<!-- 布局功能的支持程序  thymeleaf3主程序  layout2以上版本 -->
		<!-- thymeleaf2   layout1-->
		<thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>
 </properties>

3.在application.properties中可以配置thymeleaf模板解析器属性

####  thymeleaf配置   #######
#thymeleaf start
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
#开发时关闭缓存,不然没法看到实时页面
spring.thymeleaf.cache=false
#thymeleaf end
# 默认路径
spring.thymeleaf.prefix=classpath:/templates/
# 后缀
spring.thymeleaf.suffix=.html
spring.thymeleaf.enabled=true

4.其实不必application.properties中配置thymeleaf模板解析器属性,因为默认已经配置了,只要我们把HTML页面放在classpath:/templates/,thymeleaf就能自动渲染;

@ConfigurationProperties(prefix = "spring.thymeleaf")
public class ThymeleafProperties {

	private static final Charset DEFAULT_ENCODING = Charset.forName("UTF-8");

	private static final MimeType DEFAULT_CONTENT_TYPE = MimeType.valueOf("text/html");

	public static final String DEFAULT_PREFIX = "classpath:/templates/";

	public static final String DEFAULT_SUFFIX = ".html";

5.按照上面步骤就整合成功了,下面就是如何使用Thymeleaf,首先在html文件中引入thymeleaf的命名空间,方便提示

<html lang="en" xmlns:th="http://www.thymeleaf.org">

6.简单使用,具体的Thymeleaf的语法,后续再说

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>成功!</h1>
    <!--th:text 将div里面的文本内容设置为 -->
    <div th:text="${hello}">这是显示欢迎信息</div>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/xinzai245/article/details/84729193