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>
     
     
     
     
  • 相关阅读:
    video标签
    正则表达式
    BOM和DOM
    css样式属性
    js简介
    格式与布局
    CSS样式表
    表单
    redis学习心得之三-【java操作redis】
    redis学习心得之二【redis主从配置】
  • 原文地址:https://www.cnblogs.com/xiaohu1218/p/9634101.html
Copyright © 2011-2022 走看看