工作bug汇总---1

工作bug汇总—1

java

前端

关于thymeleaf模板引擎:

Thymeleaf中的表达式有:

${},*{},#{},@{},~{},[[...]] 或 [(...)]

例如:

<!-- book 中从上下文中取的一个变量 -->
<div th:object="${book}">
	<!-- title 是 book 中的属性 -->
	<span th:text="*{title}"></span>
</div>

其中 th:object 的意思是:用来接受后台传过来的对象。

Thymeleaf常用th标签中有一个:th:field
其用于表单页面填充数据,如:

<form th:object="${obj}">
	输入框:<input type="text" name="user" th:field="*{user}"/><br>
	单选框:<input type="radio" name="sex" value="1" th:field="*{sex}"/><input type="radio" name="sex" value="0" th:field="*{sex}"/><br>
	复选框:<input type="checkbox" name="hobby" value="1" th:field="*{hobby}"/>运动
	       <input type="checkbox" name="hobby" value="2" th:field="*{hobby}"/>学习
	       <input type="checkbox" name="hobby" value="3" th:field="*{hobby}"/>听音乐
	       <input type="checkbox" name="hobby" value="4" th:field="*{hobby}"/>旅游<br>
	下拉框:<select name="major" th:field="*{major}">
	            <option value="1">选项1</option>
	            <option value="2">选项2</option>
	            <option value="3">选项3</option>
	        </select>
</form>

由于th:value只能给普通的input文本框填值,所以引入了th:field,其默认可以基于值,选中默认值,可用于选择框。
但是!!!它必须搭配th:object使用,其中的值也必须是object对象中的属性值,也因此会用到${}与*{}的配合!
bug案例:

<form id="form-payment-add" class="form-horizontal" th:object="${arriveMain}">
 <input id="customerId" name="customerId" type="hidden" th:field="*{customerId}">
 <input id="arriveId" name="arriveId" type="hidden" th:value="*{id}">

th:field绑定了th:object和${}与*{},但th:value更自由,

bug原因:th:field在赋值时同时起到了th:name和th:value的作用,浏览器在解析th:field的时候,会解析成name="${th:field}"的值。”,当th:field用在input标签时,th:field属性会将参数名作为id和name的值,并将th:field属性的值作为value的值。因此上述案例中当要求参数名与name和id名不一致时,要把th:field改成th:value,如arriveId标签一样

引用: Thymeleaf之表达式.
引用: Thymeleaf常用th标签.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值