thymeleaf中th:field和th:value的区别
一:常用th:標(biāo)簽簡介:
我們再用spring-boot框架的時(shí)候,可能不會采用我們以往用的jsp頁面的方式,而是通過采用thymeleaf渲染的方式進(jìn)行
前后臺數(shù)據(jù)的交互。常用的標(biāo)簽有
th:href,用法:th:href="/brand/selectbrand",(用來指明要要跳轉(zhuǎn)的鏈接)
th:object,用法:th:object="${brand}",(用來接受后臺傳過來的對象)
樣例:
????<form class="form form-horizontal" id="form-admin-add" action="#" th:action="@{/brand/updatebyid}" th:object="${brand}">
th:field,用法:th:field="*{name}",(用來綁定后臺對象和表單數(shù)據(jù))
th:value,用法:th:value="${brand.name}",(用對象對name值替換value屬性)
總結(jié):th:field
樣例:
<form class="form form-horizontal" id="form-admin-add" action="#" th:action="@{/brand/updatebyid}" th:object="${brand}">
????????<input type="text" class="input-text" value="" th:value="*{id}" name="id" />
????????<input type="text" class="input-text" ?value="" th:field="*{code}" placeholder="" id="code"/>
th:field和th:value的小結(jié):
thymeleaf里的th:field等同于th:name和th:value,瀏覽器在解析th:field的時(shí)候,會解析成name="${th:field}"的值。
然后后臺就可以接收到從前臺傳過來的值。而th:value可以接受到后臺的的值,后臺則可以根據(jù)name獲取到前臺的值。
th:field和th:value都有兩種從后臺接受值的方式:1、${obj.name} 2、*{name}。需要注意的是,th:field需要有th:object
指定前臺傳過來的參數(shù),否則瀏覽器在解析的時(shí)候會出現(xiàn)錯(cuò)誤。
th:each,用法:th:each="brand:${pageInfo.list}",(后臺把對象放在了pageinfo的分頁插件的List里,然后用th:each遍
歷后臺傳過來的對象)。
樣例:
<tr class="text-c" th:each="order:${pageInfo.list}">
?????<td th:text="${order.id}"></td>
?????<td th:text="${order.dicMethodDescription}"></td>
?????<td th:text="${order.productBrandName}"></td>
?????<td th:text="${order.productModelName}"></td>
th:if,用法:th:if="${pageInfo.list.size() == 0}",(判斷傳過來的對象是否為空,如果為空,則執(zhí)行...)
th:unless,用法:th:unless="${pageInfo.list.size() == 0}"(判斷傳過來的對象是否為空,如果不為空,則執(zhí)行...)
標(biāo)簽只有在th:if中條件成立時(shí)才顯示,th:unless于th:if恰好相反,只有表達(dá)式中的條件不成立,才會顯示其內(nèi)容。
樣例:
<td th:if="${order.productStateName}==部分退款" th:text="交易成功" class="validate"></td>
<td th:unless="${order.productStateName}==部分退款" th:text="${order.productStateName}"></td>
三:thymeleaf實(shí)現(xiàn)動態(tài)訪問controller:
我們一般鏈接地址都是寫死了的,這樣其實(shí)間接的降低了系統(tǒng)的靈活性,我們可以在th:href的鏈接里添加參數(shù),這樣
就可以動態(tài)的訪問controller,舉個(gè)栗子:
//static頁面層:
<a title="編輯信息" href="javascript:;" th:οnclick="'javascript:brand_edit(\'編輯品牌信息\',\'/brand'+@{/updatebyid}+'/'+${brand.id}+'\',800,400)'" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont"></i></a>
//controller層:
????//根據(jù)品牌名更改品牌的名稱和編號信息
????@RequestMapping(value = "/updatebyid/{id}")
????public String updatebyidBefore(Model model, @ModelAttribute(value = "brand") ProductBrand brand,
????????????@PathVariable("id") Long id) {
????????// 根據(jù)ID查詢用戶 和對應(yīng)的角色
????????brand = operatebrand.selectByPrimaryKey(id);
????????// 查詢對應(yīng)用戶的角色
????????model.addAttribute("brand", brand);
????????return "maintenance-operate/editor-brand";
????}
四:對數(shù)據(jù)庫的數(shù)據(jù)靈活篩選:
一般我們在訪問數(shù)據(jù)庫的時(shí)候,可能在數(shù)據(jù)庫中查詢的時(shí)候?qū)懗鑫覀児潭ㄐ枰闹?#xff0c;比如我們想要查的字段需要在某一
范圍內(nèi),例如需要查出狀態(tài)表的狀態(tài)在(1,3,5,7)狀態(tài)里。這些狀態(tài)可以在狀態(tài)的字典表里查出。
你可能想到會這樣寫:select id ,name,code from table where state in(1,3,5,7)。但是別人也想查對應(yīng)的字段,但是篩選
的狀態(tài)不同,比如他篩選的是2,4,6,8。這時(shí)候他可能又需要寫一套select語句。則這樣也失去的代碼靈活性。
我們可以傳一個(gè)狀態(tài)數(shù)組進(jìn)去,這時(shí)候就會根據(jù)狀態(tài)數(shù)組的值來進(jìn)行篩選。我們來看看實(shí)現(xiàn)。
ServiceImpl層:
List<OrderInfoForm> result = null;
result=orderMapper.selectOrdersByStatesSelective(order, new Integer[] {3,4,6,7,8});
dao層:
List<OrderInfoForm> selectOrdersByStatesSelective(@Param(value="order")Order order,
? ? @Param(value="states")Integer[] states);
mapper層:
<select id="selectOrdersByStatesSelective" resultMap="AllResultMap" >
? ? select?
? ? <include refid="All_Column_List" />
? ? from order_list
?? ?LEFT JOIN product_method ON product_method.`code` = order_list.purchase_method
?? ?LEFT JOIN product_color ON product_color.`code` = order_list.color
?? ?LEFT JOIN product_guarantee ON product_guarantee.`code` = order_list.guarantee
?? ?LEFT JOIN product_info ON order_list.product_id = product_info.id
?? ?LEFT JOIN product_model ON product_info.model = product_model.`code`
?? ?LEFT JOIN product_standard ON product_info.standard = product_standard.`code`
?? ?LEFT JOIN product_state ON product_state.`code` = order_list.order_state
?? ?LEFT JOIN product_apperance ON product_apperance.`code` = order_list.apperance
?? ?LEFT JOIN product_brand ON product_brand.`code` = product_info.brand
? ? <where>
? ? ?? ?<if test="order.orderNum != null " >
?? ? ? ? ? ?order_num like "%"#{order.orderNum,jdbcType=VARCHAR}"%"
?? ? ? ?</if>
?? ? ? ?<if test="order.operator != null " >
?? ? ? ? ? ?and operator like "%"#{order.operator,jdbcType=VARCHAR}"%"
?? ? ? ?</if>
?? ? ? ?<if test="order.purchaseTime != null" >
?? ? ? ? ? ?and purchase_time = #{order.purchaseTime,jdbcType=DATE}
?? ? ? ?</if>
?? ? ? ?<if test="order.orderState != null" >
?? ? ? ? ? ?and order_state = #{order.orderState,jdbcType=VARCHAR}
?? ? ? ?</if>
?? ? ? ?<if test="order.serialNum != null" >
?? ? ? ? ? ?and serial_num like "%"#{order.serialNum,jdbcType=VARCHAR}"%"
?? ? ? ?</if>
?? ? ? ?
?? ??? ?<if test="states != null and states.length >0">
?? ??? ??? ?<foreach collection="states" item="state" separator="," open=" and order_state in (" close=")">
?? ??? ??? ??? ?#{state,jdbcType=BIGINT}
?? ??? ??? ?</foreach>
?? ??? ?</if>
?? ?</where>
? </select>
注意最后的test的條件:
解釋一下各個(gè)的參數(shù):
1、foreach collection="傳過來的狀態(tài)數(shù)組名稱"
2、item="state"用于遍歷每個(gè)狀態(tài)
3、separator=",":用于分割各個(gè)狀態(tài)
4、open=" 用于在對應(yīng)位置添加雙引號
5、and order_state in (" close=")"用于在對應(yīng)結(jié)束位置添加雙引號
?
總結(jié)
以上是生活随笔為你收集整理的thymeleaf中th:field和th:value的区别的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php中使用redis队列操作实例代码
- 下一篇: 关于蓝牙DIY设计+18个详细案例