zoukankan      html  css  js  c++  java
  • Thymeleaf 条件语句

    Thymeleaf 条件语句

    if 与 unless

    假如现在有一个商品列表,当商品有评论时,显示view按钮,否则不显示。这时候就可以使用Thymeleaf的th:if标签来实现:

    1
    2
    3
    <a href="comments.html"
    th:href="@{/product/comments(prodId=${prod.id})}"
    th:if="${not #lists.isEmpty(prod.comments)}">view</a>

    当prod.comments不为空时,页面将渲染出该<a>标签。

    另外,th:if有一个反向属性th:unless,用于代替上面的not:

    1
    2
    3
    <a href="comments.html"
    th:href="@{/comments(prodId=${prod.id})}"
    th:unless="${#lists.isEmpty(prod.comments)}">view</a>

    switch

    Thymeleaf中的th:switch和其他语言的switch case语句差不多:

    1
    2
    3
    4
    <div th:switch="${user.role}">
    <p th:case="'admin'">User is an administrator</p>
    <p th:case="'manager'">User is a manager</p>
    </div>

    th:case="*"表示默认选项,相当于default:

    1
    2
    3
    4
    5
    <div th:switch="${user.role}">
    <p th:case="'admin'">User is an administrator</p>
    <p th:case="'manager'">User is a manager</p>
    <p th:case="*">User is some other thing</p>
    </div>
     
     
     
     
  • 相关阅读:
    前端工程师必备的7个chrome插件
    树莓派 基于Web的温度计
    vue2.0 非父子组件如何通信
    newman
    mysql主从同步设置
    redis集群搭建
    服务器之间共享挂载
    Jenkins自动构建-部署-测试
    postman使用整理
    Charles使用
  • 原文地址:https://www.cnblogs.com/xiaohu1218/p/9634101.html
Copyright © 2011-2022 走看看