themleft模板库_Thymeleaf模板引擎常用总结

一:语法简单总结

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为.

350

7.2 格式化日期

28-Jun-2018

7.3 字符串连接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值