spring 如果列表为空,如何在Thymeleaf页面上显示适当的消息

jk9hmnmh  于 2023-02-21  发布在  Spring
关注(0)|答案(1)|浏览(172)

我有一些控制器与一些列表连接,现在列表可以是空的,如果列表是空的,我尝试在页面上提供用户消息。更清楚地说,用户可以有钱包,在钱包内用户可以创建交易,因为所有这些我现在有一个页面Transactions,如果用户仍然没有创建任何交易,但访问该页面,我想给他看You didnt create any transaction这样的消息。
我找到了这个例子,并试图应用于我的问题,但到目前为止,它没有工作:How to check null and empty condition using Thymeleaf in one single operation?
这是我的控制器:

@GetMapping("/userTransactions/{user_id}")
public String getUserTransactions(@PathVariable("user_id") long user_id, TransactionGroup transactionGroup, Model model) {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    UserDetailsImpl user = (UserDetailsImpl) authentication.getPrincipal();
    long userId = user.getId();
    model.addAttribute("userId", userId);

    List<Transaction> transactions = transactionRepository.getTransactionsByUserId(user_id);
    List<TransactionGroup> transactionByDate = new ArrayList<>();
    List<Transaction> transOnSingleDate = new ArrayList<>();
    boolean currDates = transactions.stream().findFirst().isPresent();

    if (currDates) {
        LocalDate currDate = transactions.get(0).getDate();

        TransactionGroup transGroup = new TransactionGroup();

        for (Transaction t : transactions) {
            if (!currDate.isEqual(t.getDate())) {
                transGroup.setDate(currDate);
                transGroup.setTransactions(transOnSingleDate);
                transactionByDate.add(transGroup);
                transGroup = new TransactionGroup();
                transOnSingleDate = new ArrayList<>();
            }

            transOnSingleDate.add(t);
            currDate = t.getDate();
        }
        transGroup.setDate(currDate);
        transGroup.setTransactions(transOnSingleDate);

        transactionByDate.add(transGroup);
    } else {
        System.out.println("Empty");
        model.addAttribute("transactionGroup", "NoData");
    }
    model.addAttribute("transactionGroup", transactionByDate);
    return "transactions";
}

这似乎工作正常,我的意思是,如果我没有创建事务,消息System.out.println("Empty");将被打印,但消息在页面上也不会显示。
这是百里香叶

<div class="date" th:each="singleGroup  : ${transactionGroup}">
    <h1 th:text="${singleGroup .date}"></h1>
    <div class="transaction" th:each="singleTrans  : ${singleGroup.transactions}">
        <h2>Amount: <span th:text="${singleTrans.amount}"></span></h2><br>
        <h2>Note: <span th:text="${singleTrans.note}"></span></h2><br>
        <h2>Wallet name: <span th:text="${singleTrans .walletName}"></span></h2><br>
        <h2>Expense Category: <span th:text="${singleTrans .expenseCategories}"></span></h2><br>
        <h2>IncomeCategory: <span th:text="${singleTrans .incomeCategories}"></span></h2>
        <a class="card__link" th:href="@{/api/transaction/delete/{id}(id=${singleTrans.id})}"
           th:data-confirm-delete="|Are you sure you want to delete ${singleTrans.walletName} wallet?|"
           onclick="if (!confirm(this.getAttribute('data-confirm-delete'))) return false">Delete</a>
        <hr>
    </div>
    <th:block th:if="${transactionGroup=='NoData'}"> No Data Found </th:block>
</div>

这是一部分:<th:block th:if="${transactionGroup=='NoData'}"> No Data Found </th:block>但如果列表为空则不显示
我错过了什么?

lyfkaqu1

lyfkaqu11#

您可以像这样检查您的列表是否为空

<div th:if="${#lists.isEmpty(transactionGroup)}"> No Data Found </div>

<div th:if="${#lists.size(transactionGroup) ==0}"> No Data Found </div>

除此之外,您的th:块被放置在list外部循环内部,它应该在外部,这就是为什么当list为空时您看不到任何东西。

相关问题