【问题标题】:Thymeleaf: Iterate over enum values and use them in a list filterThymeleaf:遍历枚举值并在列表过滤器中使用它们
【发布时间】:2021-06-22 19:48:28
【问题描述】:

我正在努力在 Thymeleaf 的列表过滤器中使用枚举值。

我知道如何迭代枚举值以及如何将它们与常量值进行比较。但是,我想将它与“变量”值进行比较。我怎样才能实现它?

在下面的示例中,我想遍历所有颜色(枚举),然后按当前颜色枚举过滤汽车列表并显示它们的名称。

如何正确指定第二个<div>中的列表过滤器?

<div th:each="currentColorEnum : ${T(de.my.enum.color).values()}">
    <div th:each="currentCar, carStatus : ${model.carList.?[#this.colorEnum eq __${currentColorEnum}__]}">                  
        <textarea th:field="*{carList[__${carStatus.index}__].carName}"></textarea>
    </div>
</div>

当前错误信息:

org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'red' cannot be found on object of type 'de.my.class.car' - maybe not public or not valid?

【问题讨论】:

    标签: html thymeleaf spring-el


    【解决方案1】:

    在这种情况下不需要预处理。它失败了,因为${model.carList.?[#this.colorEnum eq __${currentColorEnum}__]} 解析为${model.carList.?[#this.colorEnum eq red]}。这意味着它在寻找 car.colorEnum == car.red 的汽车——因此出现错误 field 'red' cannot be found on object of type 'de.my.class.car'

    您的 Thymeleaf 应该类似于:

    <div th:each="currentColorEnum : ${T(de.my.enum.color).values()}">
        <div th:each="currentCar, carStatus : ${model.carList.?[colorEnum eq #root.currentColorEnum]}">                  
            <textarea th:field="*{carList[__${carStatus.index}__].carName}"></textarea>
        </div>
    </div>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-12
      • 1970-01-01
      • 1970-01-01
      • 2021-12-09
      • 1970-01-01
      • 1970-01-01
      • 2014-07-26
      相关资源
      最近更新 更多