springboot(thymeleaf)中th:field和th:value的區(qū)別及說明
一、常用th:標(biāo)簽簡介
我們再用spring-boot框架的時候,可能不會采用我們以往用的jsp頁面的方式,而是通過采用thymeleaf渲染的方式進行
前后臺數(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的時候,會解析成name="${th:field}"的值。
然后后臺就可以接收到從前臺傳過來的值。而th:value可以接受到后臺的的值,后臺則可以根據(jù)name獲取到前臺的值。
th:field和th:value都有兩種從后臺接受值的方式:1、${obj.name} 2、*{name}。需要注意的是,th:field需要有th:object
指定前臺傳過來的參數(shù),否則瀏覽器在解析的時候會出現(xiàn)錯誤。
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中條件成立時才顯示,th:unless于th:if恰好相反,只有表達式中的條件不成立,才會顯示其內(nèi)容。
樣例:
<td th:if="${order.productStateName}==部分退款" th:text="交易成功" class="validate"></td> <td th:unless="${order.productStateName}==部分退款" th:text="${order.productStateName}"></td>
三、thymeleaf實現(xiàn)動態(tài)訪問controller
我們一般鏈接地址都是寫死了的,這樣其實間接的降低了系統(tǒng)的靈活性,我們可以在th:href的鏈接里添加參數(shù),這樣
就可以動態(tài)的訪問controller,舉個栗子:
//static頁面層: <a title="編輯信息" href="javascript:;" rel="external nofollow" 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ù)據(jù)庫中查詢的時候?qū)懗鑫覀児潭ㄐ枰闹?,比如我們想要查的字段需要在某?/p>
范圍內(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。這時候他可能又需要寫一套select語句。則這樣也失去的代碼靈活性。
我們可以傳一個狀態(tài)數(shù)組進去,這時候就會根據(jù)狀態(tài)數(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的條件:
解釋一下各個的參數(shù)
1、foreach collection="傳過來的狀態(tài)數(shù)組名稱"
2、item="state"用于遍歷每個狀態(tài)
3、separator=",":用于分割各個狀態(tài)
4、open=" 用于在對應(yīng)位置添加雙引號
5、and order_state in (" close=")"用于在對應(yīng)結(jié)束位置添加雙引號
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
springboot整合Dubbo與Feign的實現(xiàn)?(無注冊中心)
本文主要介紹了springboot整合Dubbo與Feign的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04詳解Mybatis-plus中更新date類型數(shù)據(jù)遇到的坑
這篇文章主要介紹了詳解Mybatis-plus中更新date類型數(shù)據(jù)遇到的坑,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10elasticsearch索引index之engine讀寫控制結(jié)構(gòu)實現(xiàn)
這篇文章主要為大家介紹了elasticsearch索引index之engine讀寫控制結(jié)構(gòu)實現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-04-04springboot項目編寫發(fā)送異常日志到企微工具包的操作方法
本文介紹了Springboot項目如何編寫發(fā)送異常日志到企業(yè)微信的工具包,內(nèi)容包括創(chuàng)建基礎(chǔ)Bean、配置類、pom依賴等步驟,并展示了如何通過nacos進行配置,這為開發(fā)者提供了一種有效的日志管理方案,方便快速定位和處理項目中的異常問題,感興趣的朋友跟隨小編一起看看吧2024-09-09SpringBoot配置文件的優(yōu)先級順序、加載順序、bootstrap.yml與application.yml區(qū)別及說明
在SpringBoot中,配置文件的優(yōu)先級順序是:application-{profile}.yml或.properties > application.yml或.properties > bootstrap.yml或.properties,{profile}代表不同環(huán)境,如dev、test、prod,加載順序是先加載bootstrap文件2024-09-09