themleft模板库_Thymeleaf模板引擎常用总结
一:語(yǔ)法簡(jiǎn)單總結(jié)
Thymeleaf是一個(gè)Java類庫(kù),是xml/html/html5的模板引擎,SpringBoot框架推薦在MVC的Web應(yīng)用做做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的配置注入下面對(duì)應(yīng)的屬性中,有幾個(gè)是默認(rèn)的,剩下的都是根據(jù)使用情況配置的,然后注入到相應(yīng)的值中.
@ConfigurationProperties:批量注入屬性值.
@Value:單個(gè)注入屬性值.@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();
}
命名空間如下,靜態(tài)頁(yè)面轉(zhuǎn)換為動(dòng)態(tài)視圖.
1. Thymeleaf引入css資源.
th:href="@{path}"
2. Thymeleaf引入JavaScript資源.
th:src="@{path}"
通過(guò) "@{}"引用靜態(tài)資源.
3. 訪問(wèn)Model中的數(shù)據(jù).
${}訪問(wèn)model中的屬性.
動(dòng)態(tài)處理的元素使用"th:"為前綴.
4. model中的數(shù)據(jù)迭代.
判斷l(xiāng)ist集合不為空.
迭代list集合數(shù)據(jù).list中保存實(shí)體對(duì)象.
獲得名字
如何獲取遍歷列表的序號(hào),方式一:${stat.count}是從1開始的,方式? 二:${stat.index}是從0開始的,如果從1開始就${stat.index+1}.
迭代list,里面存放map對(duì)象.
th:text=${novel['Map Key']}
5. 在JavaScript中訪問(wèn)model中的數(shù)據(jù).
通過(guò)th:inline=“javascript”添加到script標(biāo)簽,這樣Javascript就可以訪問(wèn)model中的對(duì)象屬性了.
通過(guò)“[[${}]]”訪問(wèn)實(shí)際的值.
var single=[[${singlePerson}]];
console.log(single.name+"/"+single.age);
function getPersonName(name){
console.log(name);
}
6. 解析model屬性值中的html標(biāo)簽.
7. 格式化.(表達(dá)式對(duì)象)
7.1格式化金額.小數(shù)位為2為.
3507.2 格式化日期
28-Jun-20187.3 字符串連接
總結(jié)
以上是生活随笔為你收集整理的themleft模板库_Thymeleaf模板引擎常用总结的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 58端口使用技巧跟推送_Kindle使用
- 下一篇: 二分检索用途及复杂性_二分查找和三分查找