Thymeleaf笔记
   一些代码   2 评论   5818 浏览

Thymeleaf笔记

   一些代码   2 评论   5818 浏览

使用

常见替换标签,例如th:text,th:href

<script th:src="@{/plugins/jquery/jquery.min.js}"></script>

单独使用变量:

[[${content.views}]]

each

<div class="block-thumb" th:each="content,arrayS:${contents}">
...
</div>

if判断List不是空

<li class="list-group-item" th:if="${!recommendContents.isEmpty()}">
...
</li>

if判断字符不是空

<li class="list-group-item" th:if="!${#strings.isEmpty(optionsMap.location)}">
...
</li>

拼接

<div class="block-thumb2" th:style="'background-image:url('+${content.thumb}+')'">
...

日期格式化

<time>[[${#dates.format(content.createdTime, 'yyyy-MM-dd')}]]</time>

application域对象

// 后端传值
request.getServletContext().setAttribute("username", "admin");

//前端取值
${#servletContext.getAttribute('username')}

字符串常见使用方法

https://blog.csdn.net/qq_33535433/article/details/78876693

前端获取session里对象属性

session.setAttribute("USER_SESSION", user);
th:value="${session.USER_SESSION.mail}

js函数传值通过Thymeleaf

th:onclick="valuesToInput([[${category.cgid}]],[[${category.cgName}]],[[${category.cgSlug}]])"

拼出绝对路径

<a th:href="${#httpServletRequest.getScheme() + '://' + #httpServletRequest.getServerName() + ':' + #request.getServerPort()  + #request.getContextPath() + @{/login}} ">点我啊!</a>

getScheme()获取协议
getServerName()获取服务器名
getServerPort()服务器端口
getContextPath() APP根路径

本文由 RawChen 发表, 最后编辑时间为:2021-02-20 20:59
如果你觉得我的文章不错,不妨鼓励我继续写作。

发表评论 取消评论
选择表情
  1. 新年快乐!

       Windows 8.1   Chrome 78
  2. 涨知识了 赞一个

       Windows 10   Firefox 85
Top