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為.
3507.2 格式化日期
28-Jun-20187.3 字符串連接
總結
以上是生活随笔為你收集整理的themleft模板库_Thymeleaf模板引擎常用总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 58端口使用技巧跟推送_Kindle使用
- 下一篇: 二分检索用途及复杂性_二分查找和三分查找