一:常用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:name和th: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:;" 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层:

//根据品牌名更改品牌的名称和编号信息
    @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=")"用于在对应结束位置添加双引号

thymeleaf中th:field和th:value的区别相关推荐

  1. thymeleaf中th:text和th:utext的区别

    th:text 1.可以对表达式或变量进行求值 2.用"+"符号可进行文本连接 3.当获取后端传来的参数时,若后端有标签,如: @RequestMapping("/&qu ...

  2. Mark一个关于Thymeleaf 使用th:field属性与对象属性绑定的小问题

    ##Mark一个关于Thymeleaf 使用th:field属性与对象属性绑定的小问题 最近在尝试用Spring Boot + Thymeleaf搭建一个个人博客,其中Article类中有一个属性如下 ...

  3. python怎么设置字段_Django Model中字段(field)的各种选项说明

    字段与选项 CharField() 字符字段 max_length = xxx or None # 必选项 blank = True 和 default = '' # 如果不是必填项,可以设置 uni ...

  4. thymeleaf 中select下拉回显

    thymeleaf中的select下拉回显的问题 找了网上很大一圈相关的解决方案没一个管用,最后还是它处理了 进行回显作用的.

  5. 利用vantUI组件库中的Field 输入框、Cell 单元格完成金额数字框的数字转金额格式和金额大写

    近期公司的任务需要这个功能,利用vantUI组件库中的Field 输入框.Cell 单元格完成金额数字框的数字转金额格式和金额大写 1.首先写工具类utils.js // 将数字转换成金额千字文格式显 ...

  6. java method field_java_解析Java中的Field类和Method类,Field类 Field类中定义了一些方 - phpStudy...

    解析Java中的Field类和Method类 Field类Field类中定义了一些方法,可以用来查询字段的类型以及设置或读取字段的值.将这些方法与继承而来的member方法结合在一起.就可以使我们能够 ...

  7. MySQL中函数field()的用法

    MySQL中的field()函数,可以用来对SQL中查询结果集进行指定顺序排序. 函数使用格式如下: order by (str,str1,str2,str3,str4--),str与str1,str ...

  8. 在学习thymeleaf中,给function 方法传Controller传递过来的参数,报错:Uncaught SyntaxError: missing ) after argument list

    在学习thymeleaf中,给function 方法传Controller传递过来的参数. 报错:Uncaught SyntaxError: missing ) after argument list ...

  9. 【Thymeleaf】Thymeleaf中的判断语句

    目录 IF判断 Switch Case unless IF判断 Thymeleaf 条件 gt:great than(大于)> ge:great equal(大于等于)>= eq:equa ...

最新文章

  1. 架构师速成6.6-知识的收集整理学习
  2. 向app store提交应用时,必须点“ready to upload binary”!
  3. 2.5 程序示例--非线性决策边界-机器学习笔记-斯坦福吴恩达教授
  4. 天翼云从业认证(1.5)了解Windows和Linux两种操作系统的常见命令和操作
  5. POJ - 3342 Party at Hali-Bula(树形dp)
  6. 图解TCP/IP读书笔记(三)
  7. Intellij IDEA设置系列(四):各种乱码解决方案
  8. shell 判断目录还是文件
  9. 重磅!Intel联合OpenStack基金会发布Kata Container项目
  10. 电机驱动芯片效果对比
  11. Qt QPainter CompositionMode解读及图片透明度设置
  12. vue动态style设置背景图片
  13. 初来乍到,余生请多指教
  14. 网络线综合布线接地注意事项
  15. android华为手机全屏显示
  16. python实现分词算法代码
  17. 虚拟机安装windows10
  18. win10系统打印服务器,Win10手动添加TCP/IP端口及安装打印机驱动(系统向导安装,不需要任何软件)...
  19. 搜狗怎么做收录?我们来看看搜狗官方的回答
  20. 在vue中实现picker样式_vue中van-picker的多列联动数据格式如何设置以及调用

热门文章

  1. 网络协议层次(熟读并背诵全文)
  2. Windows11安装 Python
  3. 财务报表分析实务(第六讲)
  4. lingo求解运输问题
  5. 变革家五步投资法学习体会
  6. Go之开发小功能集合(viper获取配置信息,test编写测试单元,用户目录获取,JWT安全校验,字符串去重,Swag在线接口文档,画字符图像工具,Gin模式的选择,iota枚举,promhttp等)
  7. BZOJ 2999 inint【数DP优化】(Ender的模拟赛)
  8. 可靠性设计:容错设计
  9. python字符串换行的三种方式_python中的字符串内部换行方法
  10. uavcan学习,libcanard c语言