SpringBoot中提供了大量模板引擎,包括FreeMarker、Grooy、Thymeleaf、Velocity和Mustache,SpringBoot推荐使用Thymeleaf作为模板引擎,因为Thymeleaf提供了完美的SpringMVC支持。
Thymeleaf基础知识
Thymeleaf是一个Java类库,它是一个xml/xhtml/html5的模板引擎,作为MVC web应用的view层,Thymeleaf提供了额外的模板与SpringMVC集成,完全可以使用Thymeleaf替代JSP。
1、引入Thymeleaf
一个基本的Thymeleaf模板页面
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta content="text/html;charset=UTF-8" />
<link th:src="@{bootstrap/css/bootstrap.min.css}" rel="stylesheet"/>
<link th:src="@{bootstrap/css/bootstrap-theme.min.css}" rel="stylesheet"/>
<script type="text/javascript" th:src="@{jquery-1.10.2.min.js}"></script>
<script type="text/javascript" th:src="@{bootstrap/js/bootstrap.min.js}"></script>
</head>
<body>
</body>
</html>
通过xmlns:th=”http://www.thymeleaf.org”命名空间,将静态页面转换为动态的视图,需要进行动态处理的元素将使用”th:”前缀,使用“@{}”引用web静态资源。
2、访问model中的数据
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">访问model</h3>
</div>
<div class="panel-body">
<span th:text="${singlePerson.name}"></span>
</div>
</div>
通过“${}”访问model中的属性
3、model中的数据迭代
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">列表</h3>
</div>
<div class="panel-body">
<ul class="list-group">
<li class="list-group-item" th:each="person:${people}">
<span th:text="${person.name}"></span>
<span th:text="${person.age}"></span>
</li>
</ul>
</div>
</div>
使用th:each来做循环迭代(th:each=”person:${people}”),person作为迭代元素。
4、数据判断
<div class="panel panel-primary" th:if="${not #lists.isEmpty(people)}">
<div class="panel-heading">
<h3 class="panel-title">列表</h3>
</div>
<div class="panel-body">
<ul class="list-group">
<li class="list-group-item" th:each="person:${people}">
<span th:text="${person.name}"></span>
<span th:text="${person.age}"></span>
</li>
</ul>
</div>
</div>
使用${not #lists.isEmpty(people)}表达式判断people是否为空。Thymeleaf支持 > < >= <= == != 作为比较条件,同时也支持将SpringEL表达式语言用于条件中。
5、在javascript中访问model
<script th:inline="javascript">
var single = [[${singlePerson}]];
console.log(single.name+"/"+single.age)
</script>
使用th:inline=”javascript” 添加到script标签,这样Javascript代码即可访问model中的属性。使用”[[${}]]”格式获得实际的值。
<li class="list-group-item" th:each="person:${people}">
<span th:text="${person.name}"></span>
<span th:text="${person.age}"></span>
<button class="btn" th:onclick="'getName(\''+${person.name}+'\');'">获得名字</button>
</li>
与SpringMVC集成(写的不完整)
Thymeleaf模板引擎提供了
org.thymeleaf.spring4.view.ThymeleafView
org.thymeleaf.spring4.view.ThymeleafViewResolver,默认使用ThymeleafView作为View。
SpringTemplateEngine类,用来驱动在SpringMVC下使用Thymeleaf模板引擎
TemplateResolver用来设置通用的模板引擎(包括前缀、后缀等)
SpringBoot的Thymeleaf支持
SpringBoot通过org.springframework.boot.autoconfigure.thymeleaf包对Thymeleaf进行了自动配置
ThymeleafAutoConfiguration类对集成所需的Bean进行自动配置,包括templateResolver、templateEngine和thymeleafViewResolver的配置。
ThymeleafProperties类配置Thymeleaf,在application.properties中,以spring.thymeleaf开头来配置。
ThymeleafProperties.java
@ConfigurationProperties("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";
/**
* Check that the template exists before rendering it (Thymeleaf 3+).
*/
private boolean checkTemplate = true;
/**
* Check that the templates location exists.
*/
private boolean checkTemplateLocation = true;
/**
* 前缀设置,SpringBoot默认模板放置在classpath:/template/目录下
*/
private String prefix = DEFAULT_PREFIX;
/**
* 后缀设置,默认为.html
*/
private String suffix = DEFAULT_SUFFIX;
/**
* 模板模式设置,默认为HTML5
*/
private String mode = "HTML5";
/**
* 模板的编码设置,默认UTF-8
*/
private Charset encoding = DEFAULT_ENCODING;
/**
* 模板的媒体类型设置,默认为text/html
*/
private MimeType contentType = DEFAULT_CONTENT_TYPE;
/**
* 是否开启模板缓存,默认是开启,开发时请关闭
*/
private boolean cache = true;
/**
* Order of the template resolver in the chain. By default, the template resolver is
* first in the chain. Order start at 1 and should only be set if you have defined
* additional "TemplateResolver" beans.
*/
private Integer templateResolverOrder;
/**
* Comma-separated list of view names that can be resolved.
*/
private String[] viewNames;
/**
* Comma-separated list of view names that should be excluded from resolution.
*/
private String[] excludedViewNames;
/**
* Enable MVC Thymeleaf view resolution.
*/
private boolean enabled = true;