一:语法简单总结
Thymeleaf是一个Java类库,是xml/html/html5的模板引擎,SpringBoot框架推荐在MVC的Web应用做做View层使用.
SpringBoot中整合Thymeleaf依赖如下.
org.springframework.boot
spring-boot-starter-thymeleaf
3.0.9.RELEASE
2.1.1
UTF-8
UTF-8
1.8
ThymeleafProperties的源码如下.
将application.yml中的spring.thymeleaf的配置注入下面对应的属性中,有几个是默认的,剩下的都是根据使用情况配置的,然后注入到相应的值中.
@ConfigurationProperties:批量注入属性值.
@Value:单个注入属性值.@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";
private boolean checkTemplate = true;
private boolean checkTemplateLocation = true;
private String prefix = "classpath:/templates/";
private String suffix = ".html";
private String mode = "HTML";
private Charset encoding;
private boolean cache;
private Integer templateResolverOrder;
private String[] viewNames;
private String[] excludedViewNames;
private boolean enableSpringElCompiler;
private boolean enabled;
private final ThymeleafProperties.Servlet servlet;
private final ThymeleafProperties.Reactive reactive;
public ThymeleafProperties() {
this.encoding = DEFAULT_ENCODING;
this.cache = true;
this.enabled = true;
this.servlet = new ThymeleafProperties.Servlet();
this.reactive = new ThymeleafProperties.Reactive();
}
命名空间如下,静态页面转换为动态视图.
1. Thymeleaf引入css资源.
th:href="@{path}"
2. Thymeleaf引入JavaScript资源.
th:src="@{path}"
通过 "@{}"引用静态资源.
3. 访问Model中的数据.
${}访问model中的属性.
动态处理的元素使用"th:"为前缀.
4. model中的数据迭代.
判断list集合不为空.
迭代list集合数据.list中保存实体对象.
获得名字
如何获取遍历列表的序号,方式一:${stat.count}是从1开始的,方式 二:${stat.index}是从0开始的,如果从1开始就${stat.index+1}.
迭代list,里面存放map对象.
th:text=${novel['Map Key']}
5. 在JavaScript中访问model中的数据.
通过th:inline=“javascript”添加到script标签,这样Javascript就可以访问model中的对象属性了.
通过“[[${}]]”访问实际的值.
var single=[[${singlePerson}]];
console.log(single.name+"/"+single.age);
function getPersonName(name){
console.log(name);
}
6. 解析model属性值中的html标签.
7. 格式化.(表达式对象)
7.1格式化金额.小数位为2为.
3507.2 格式化日期
28-Jun-20187.3 字符串连接