【问题标题】:Evaluate 2 conditional expressions in a th:if or th:unless计算 th:if 或 th:unless 中的 2 个条件表达式
【发布时间】:2016-11-26 20:26:09
【问题描述】:

我的代码是这样的:

    <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
      <h1 class="page-header" th:utext="#{dashboard}">Dashboard</h1>
    </div>

    <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main" th:if="${lista}">
      <h1 class="page-header" th:text="#{__${name}__}"></h1>
    </div>

    <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main" th:if="${command}">
      <h1 class="page-header"><th:block th:utext="#{__${action}__}"/> <th:block th:utext="#{__${name}__}"/></h1>
    </div>

如果lista 不为空,则显示第二个&lt;div&gt; 块。当command 不为空时显示第三个。当listacommand 都为空时,应显示第一个。类似的东西:

${lista} && ${command}

但是这个表达式会导致错误。有人知道怎么做吗?

更新

我尝试:${lista &amp;&amp; command}${lista and command} 但没有解决我的问题(仍然发生错误)。

【问题讨论】:

标签: spring thymeleaf


【解决方案1】:

我认为@Andrew 引用了正确的链接。 如果您需要检查 lista 是否为空,command 是否为空,只需检查它。 我不确定,但我猜这个语法不正确 - th:if="${lista}"
请改用th:if="${lista != null}"

所以,检查这两个条件:

th:if="${lista == null and command == null}"

【讨论】:

    猜你喜欢
    • 2014-07-22
    • 2018-05-31
    • 1970-01-01
    • 1970-01-01
    • 2012-07-21
    • 2012-07-11
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    相关资源
    最近更新 更多