iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >springboot(thymeleaf)中th:field和th:value的区别及说明
  • 939
分享到

springboot(thymeleaf)中th:field和th:value的区别及说明

springboot thymeleafthymeleaf th:fieldthymeleaf th:value 2022-11-13 18:11:47 939人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

目录一、常用th:标签简介二、th:field和th:value的小结三、thymeleaf实现动态访问controller四、对数据库的数据灵活筛选解释一下各个的参数一、常用th:

一、常用th:标签简介

我们再用spring-boot框架的时候,可能不会采用我们以往用的jsp页面的方式,而是通过采用thymeleaf渲染的方式进行

前后台数据的交互。常用的标签有

  • th:href,用法:th:href="/brand/selectbrand",(用来指明要要跳转的链接)
  • 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}",(用来绑定后台对象和表单数据)
  • th:value,用法:th:value="${brand.name}",(用对象对name值替换value属性)

总结: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的小结

thymeleaf里的th:field等同于th:nameth:value,浏览器在解析th:field的时候,会解析成name="${th:field}"的值。

然后后台就可以接收到从前台传过来的值。而th:value可以接受到后台的的值,后台则可以根据name获取到前台的值。

th:field和th:value都有两种从后台接受值的方式:1、${obj.name} 2、*{name}。需要注意的是,th:field需要有th:object

指定前台传过来的参数,否则浏览器在解析的时候会出现错误。

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}",(判断传过来的对象是否为空,如果为空,则执行...)
  • th:unless用法:th:unless="${pageInfo.list.size() == 0}"(判断传过来的对象是否为空,如果不为空,则执行...)

标签只有在th:if中条件成立时才显示,th:unless于th:if恰好相反,只有表达式中的条件不成立,才会显示其内容。

样例:

<td th:if="${order.productStateName}==部分退款" th:text="交易成功" class="validate"></td>
<td th:unless="${order.productStateName}==部分退款" th:text="${order.productStateName}"></td>

三、thymeleaf实现动态访问controller

我们一般链接地址都是写死了的,这样其实间接的降低了系统的灵活性,我们可以在th:href的链接里添加参数,这样

就可以动态的访问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">&#xe6df;</i></a>
//controller层:
    //根据品牌名更改品牌的名称和编号信息
    @RequestMapping(value = "/updatebyid/{id}")
    public String updatebyidBefore(Model model, @ModelAttribute(value = "brand") ProductBrand brand,
            @PathVariable("id") Long id) {
        // 根据ID查询用户 和对应的角色
        brand = operatebrand.selectByPrimaryKey(id);
        // 查询对应用户的角色
        model.addAttribute("brand", brand);
        return "maintenance-operate/editor-brand";
    }

四、对数据库的数据灵活筛选

一般我们在访问数据库的时候,可能在数据库中查询的时候写出我们固定需要的值,比如我们想要查的字段需要在某一

范围内,例如需要查出状态表的状态在(1,3,5,7)状态里。这些状态可以在状态的字典表里查出。

你可能想到会这样写:select id ,name,code from table where state in(1,3,5,7)。但是别人也想查对应的字段,但是筛选

的状态不同,比如他筛选的是2,4,6,8。这时候他可能又需要写一套select语句。则这样也失去的代码灵活性。

我们可以传一个状态数组进去,这时候就会根据状态数组的值来进行筛选。我们来看看实现。

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的条件:

解释一下各个的参数

1、foreach collection="传过来的状态数组名称"

2、item="state"用于遍历每个状态

3、separator=",":用于分割各个状态

4、open=" 用于在对应位置添加双引号

5、and order_state in (" close=")"用于在对应结束位置添加双引号 

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: springboot(thymeleaf)中th:field和th:value的区别及说明

本文链接: https://www.lsjlt.com/news/169257.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作