Thymeleaf传递给js方法值
Thymeleaf 身为SpringBoot的正宫; 推荐模板竟然不被广大开发者热衷. 这就导致了一些听从Spring建议的程序猿遇到Thymeleaf问题无处可问. 我不幸的也中枪了!!! 可以看官方文档的大佬请无视我这个萌新.
话入正题, 今天在写项目时 发现Thymeleaf给onclick传递值时如此的困难,试了很多次终于成功了! 代码附上
<a th:onclick="'javascript:del('+${user.id}+');'" ></a>
<script>
function del(id) {
alert(id);
}
</script>
解释下: (1) th:onclick 对应的就是 js中的onclick
(2) javascript: del() 前面的javascript: 是不变的 del是你的方法名
(3) ${user.id} 对应的就是 你要获取的对象的值
(4) 上面代码的符号不要少打哦 ,不然网页就崩溃了
这种是只传递id值. 但是很多场景还要根据ajax的返回值进行判断删除. 这个时候把方法中的参数加上this就好了. 这个是时候的代码就变成了这样.
<a th:onclick="'javascript:del('+this+','+${user.id}+');'" ></a>
<script>
function del(obj,id) {
alert(id);
}
</script>
这个时候this也就传进来了. 一起学Thymeleaf的小伙伴们快去试试吧
Thymeleaf常用的判断表达式
gt:great than(大于)>
ge:great equal(大于等于)>=
eq:equal(等于)==
lt:less than(小于)<
le:less equal(小于等于)<=
ne:not equal(不等于)!=
用法: 例 th:if="${xx} lt 'n'" 解释: (当${xx} 小于 'n' 的时候 执行if 的内容)
<div th:if=" ${xx} lt '3'">
<div class="addList">
<p>
<a id="href">
<span class="icon"></span>添加绑定
</a>
</p>
<p> xx绑定只能小于3</p>
</div>
</div>
1.thymealf时间格式化
model传值
th:value="${#dates.format(machine.endDate,'yyyy-MM-dd HH:mm:ss')}">