【工作笔记-20211209】Thymeleaf语法的一些学习

本文介绍了如何在SpringBoot项目中使用Thymeleaf进行下拉框的动态绑定与选项显示,并探讨了th:object、th:each、th:selected等关键语法。同时涵盖了Layui的集成和常用的模板指令如th:block、th:include和th:inline。
摘要由CSDN通过智能技术生成

前言

  • 新的项目采用了SpringBoot+Thymleaf来搭建,这是一个网络上的开源项目:https://gitee.com/bdj/SpringBoot_v2
  • 由于之前对于Thymeleaf的使用不多,这里稍微记录一些Thymeleaf的语法

一、关于<select>下拉框的返显

后台代码

@GetMapping("/editHtml/{id}")
public String editHtml(@PathVariable("id")String id, ModelMap map) {
	map.put("object", object); // 一个实体类(由service查询出来,这里省略了)
	map.put("list", list);// 下拉栏的选项集合
	return prefix + "/edit";
}

HTML代码


<form class="layui-form" action="" th:object="${object}">
	<select name="xx" >
		<option value="">请选择</option>
		<option th:selected="*{type}==${item.type}" th:each="item:${list}" th:value="${item.type}" th:text="${item.name}" />
	</select>
</form>

这里涉及到以下几个th的语法

  • th:object
  • th:each:遍历
  • th:value
  • th:text:显示在页面上的text
  • th:selected:被选中的option
  • ${...}和*{...}:这里的*{type}拿到的是object的属性,如果是一个从后台传过来的数据,则使用${}

二、th:block、th:include、th:inline

<!--引用其他页面的hear部门-->
<th:block th:include="include :: header('xxxx')">

<th:block th:include="include :: footer">

<script th:inline="javascript">
// 用了th:inline属性,就可以在js中引用后端传来的参数
let a = [[${a}]];
</script>

三、Layui的使用

参考资料

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

行走中思考

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值