【问题标题】:How to handle two Lists in thymeleaf with th:switch and th:each如何使用 th:switch 和 th:each 处理 thymeleaf 中的两个列表
【发布时间】:2021-06-10 11:19:19
【问题描述】:

我正在从数据库中获取一个对象列表,并使用 th:switch 和 th:each 使用 thymeleaf 引擎在 html 页面内迭代该列表。现在我想将这个列表中每个对象的一个​​属性更改为另一种格式,问题是我如何在百里香的 switch 语句中的另一个列表中使用这些格式化值,以及它与第一个列表中的对应对象。

下面显示了我如何在每个对象中循环以获取 getKinachoitajika() 的值,这是一个在输出中显示为 5.0E7 的双数,现在我对其进行格式化以便显示像 50,000,000 这样的普通数字,并将这些值放入 arrItajikaArrayList 中,并在循环第一个列表的对象时在 html 文件中使用它们。

@GetMapping("/joinMichangoQuery")
   public String kandaAndParokia(Model model){
     List<Kmichango> listKandaAndParokia = kMichangoRepository.findAll();
     ArrayList arrItajika = new ArrayList<>();
        for(int i=0; i< listKandaAndParokia.size(); i++){
           NumberFormat nf = NumberFormat.getInstance();
           nf.setMinimumFractionDigits(0);
           arrItajika.add(nf.format(listKandaAndParokia.get(i).getKinachoitajika()));
         }
     model.addAttribute("listKandaAndParokia", listKandaAndParokia);
     model.addAttribute("itajikaFromated", arrItajika);
     return "michango_joined";
 }

现在在 michango_joined.html 中,我遍历每个对象的所有值并将它们显示在下表中。

        <div th:switch="${listKandaAndParokia}" class="container my-5">
        <h2 th:case="null">No Michango</h2>
        <div th:case="*">
            <table class="table table-bordered table-striped table-responsive-md">
                <thead class="thead-dark">
                    <tr>
                <th>ID</th>
                <th>KandaID</th>
                <th>Kinachohitajika</th>
                <th>Kilichopatikana</th>
                <th>Hali</th>
                <th>Jina La Mchango toka Parokia Michango</th>
                <th></th>
                    </tr>
                </thead>
                <tbody>
                <tr th:each="kMchango : ${listKandaAndParokia}">
                <td th:text= "${kMchango.id}"></td>
                <td th:text= "${kMchango.kandaId.kandaName}"></td>
                <td th:text= "${kMchango.kinachoitajika}"></td>
                <td th:text= "${kMchango.kilichopatikana}"></td>
                <td>[[${kMchango.mchangoParokia.status}]]</td>
                <td th:text= "${kMchango.mchangoParokia.mchangoJina}"></td>
                </tr>
            </tbody>
            </table>
        </div>
    </div>

现在我想使用 ArrayList arrItajika 的值与开关内的每个对应对象一起显示,这意味着我想替换这个 "${kMchango.kinachoitajika}" 与我的 arrItajika 在 thymeleaf 中,我该如何执行此交互,或者有其他关于如何处理此问题的建议。

更新: 我已经添加了存储库文件

public interface KmichangoRepository extends JpaRepository <Kmichango, String> {
@Query(value = "SELECT * FROM michango_kanda INNER JOIN michango ON michango_kanda.mchango_code = michango.mchango_code",nativeQuery = true)
public List<Kmichango>  getKandaAndParokiaByMichangoCode();


}

【问题讨论】:

    标签: java spring-boot spring-mvc java-8 thymeleaf


    【解决方案1】:

    如果两个列表的索引相等,您可以将th:each 与索引一起使用。见Thymeleaf - How to loop a list by index

    <tr th:each="kMchango,iter: ${listKandaAndParokia}">
    ...
      <td th:text= "${itajikaFromated[iter.index]}"></td>
    ...
    </tr>
    

    或者,创建一个新类,将 Kmichango 与一个额外字段(格式化值)结合起来,并且在您的模型中只有 1 个包含该新类元素的列表。

    最后,您还可以在模板本身中进行格式化:

    <span th:text="${#numbers.formatDecimal(kMchango.kinachoitajika, 0, 'COMMA', 2, 'POINT')}">
    

    【讨论】:

    • 非常感谢。第二个有效,但第一个无效,告诉我这个错误 Caused by: org.springframework.core.convert.ConverterNotFoundException: No converter found able to convert from type [org.thymeleaf.engine.IterationStatusVar]输入 [java.lang.Integer] 。我试图找出原因,因为第一个选项将来也很有用。
    • 我已添加存储库文件以突出显示第一个选项不起作用的原因。 @Wim Deblauwe
    • 我更新了第一个使用iter.index的答案,我搞错了。
    • 现在这两个选项都可以正常工作。非常感谢@Wim Deblauwe,这是一个五星级的答案。
    • @GabrielRogath - 仅供参考,您可以查看迭代状态变量列表here。它们是indexcountsizeevenoddfirstlast
    猜你喜欢
    • 2018-05-09
    • 2018-05-11
    • 2020-10-09
    • 1970-01-01
    • 2021-04-24
    • 1970-01-01
    • 2023-03-31
    • 2018-07-10
    • 1970-01-01
    相关资源
    最近更新 更多