工作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标签.