【问题标题】:How to have multiple condition in an th:if tag using thymeleaf如何在 th:if 标记中使用 thymeleaf 有多个条件
【发布时间】:2013-04-07 18:25:51
【问题描述】:

我有一个文本要使用 thymeleaf 以三种可能的颜色呈现。

所以到目前为止我为测试该值所做的代码是:

th:if="${evaluation} > 50"
th:if="${evaluation} < 30"

而且效果很好。

但第三个测试是针对这两者之间的值。 所以我尝试了:

th:if="(${evaluation} < 49) ∧ (${evaluation} > 29)"

但它不起作用,我在解析时遇到了这个错误:

org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: "(${evaluation} < 49) &and; (${evaluation} > 29)" (/property.html:41)

当然,这些行是在标签之间,因为前两行工作正常。

也许 and 操作数不正确,但 thymeleaf 的文档对这些操作数并没有真正明确。

欢迎所有想法!

更新:我从百里香论坛得到了答案。方法是:

th:if="${evaluation &lt; 49 and evaluation &gt; 29}"

问题解决了!

【问题讨论】:

    标签: java html spring jstl thymeleaf


    【解决方案1】:

    我从百里香论坛得到了答案。这样做的方法是:

    th:if="${evaluation &lt; 49 and evaluation &gt; 29}"
    

    问题解决了!

    【讨论】:

      【解决方案2】:

      这对我有用:

      th:if="${evaluation lt 49 and evaluation gt 29}"
      

      【讨论】:

      • 使用 Date 对象我发现了以下作品 th:if="${date.month ge 4 and date.month le 4}"
      • 与公认的答案相同,但语法更简洁。如果这被证实有效,它应该是公认的答案。
      • 接受的答案对我不起作用,这个对我有用!
      【解决方案3】:

      在我看来,更好和更易于维护的解决方案可能是在适当的类中编写评估代码。

      class Evaluator{
      
      private int value;
      ....
      
      public boolean isBounded() {
          return value < 49 && value > 29;
      }
      

      然后在thymeleaf中,调用函数:

      <p th:if="${evaluator.isBounded()} ...
      

      一些好处:

      1. 更清洁的模板。
      2. Java 代码中的控件。
      3. 隔离。无需更改模板即可编写更复杂的评估。

      我希望这会有所帮助。

      【讨论】:

      • 这没有回答问题。
      【解决方案4】:

      我这样做是为了在 thymeleaf 中的 th:if 中有多个条件

      <div 
           th:if="${object.getStatus()} == 'active' and ${object.getActiveDate()}"
           th:text="${#dates.format(object.getActiveDate(), 'yyyy-MM-dd')}"
      </div>
      

      我在条件之间添加了 and 运算符。如果需要,您还可以添加

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-07-01
        • 2014-08-29
        • 1970-01-01
        • 1970-01-01
        • 2015-06-17
        • 1970-01-01
        • 2020-06-18
        相关资源
        最近更新 更多