gpt4 book ai didi

java - Thyemleaf 嵌套迭代触发 org.thymeleaf.exceptions.TemplateInputException

转载 作者:行者123 更新时间:2023-12-04 10:14:54 28 4
gpt4 key购买 nike

我正在尝试遍历对象列表并生成 div class="card-deck"4对象和嵌套 div class="card"对于每个对象。

这是在 上生成异常的代码第 234 行

更新:注:第 234 行 html 中提到并拥有 <!-- Error-Line 234 -->由于缺少 )${#numbers.sequence(0,3}

    <div class="card-deck" th:each="qr: ${objects}" th:if="${qr.tableid}%4==0"> <!-- Iterate every 4 objects -->

<!--syntax error missed clossing ) at ${#numbers.sequence(0,3) triggered the exception here -->
<div class="card" th:each="i : ${#numbers.sequence(0,3)} "> <!-- Error-Line 234 -->


<!-- Some More Code -->
<img th:src="${qr.qrcodestaticpath}" class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title" align="center" th:text="'Table '+${qr.tableid}"></h5>
<p class="card-text" align="center" th:text="'Random Generated QR Code'"></p>
<h6 align="center" th:text=" ${qr.qrcodestring}"></h6>

</div>
</div>
</div>

org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/qrcodes.html]" - line 234, col 10)



我已经讨论过这些话题
  • How to do if-else in Thymeleaf?
  • Thymeleaf Loop Until a Number
  • nested (double) loop with thymeleaf
  • Thymeleaf - How to loop a list by index

  • 并浏览了本文档
  • Iteration in Thymeleaf | Baeldung

  • 并且仍然无法找到正确的方法,而不触发 exception
    更新:异常已修复,我正在尝试实现的逻辑没有预期的结果:

    以上片段的结果:

    enter image description here

    想象一下有 8 个表, table1, table2 ... table8 ,我试图生成一个 <div class="card-deck" ...每 4 或 5 table 。由于 <div class="card" th:each="i : ${#numbers.sequence(0,3)} ">我得到了 4 张相同的 table 。
  • qr.tableid是表号,1 到 x

  • 出于演示目的,请查看此 java片段

    int numOfObjects=11;
    for(int i=0 ;i<numOfObjects;i++)
    {
    if(i%4==0)
    {
    System.out.println();
    System.out.print("Deck:");
    }
    System.out.print("Card"+(i+1)+" ");
    }


    输出:

    enter image description here

    这是我的 Controller
    @GetMapping("/qrcodes")
    public String greetingForm(Model model) {

    List<QrObject> qr =qrRepo.findAll();
    int numOfobj= qr.size();
    int decks;

    if(numOfobj % 4==0)
    decks = numOfobj / 4 ;
    else
    decks = (numOfobj / 4) +1 ;

    int posa_periseuoun = numOfobj % 4 ;
    model.addAttribute("objects", qr);
    model.addAttribute("decks",decks);
    model.addAttribute("cards",posa_periseuoun);
    model.addAttribute("size", numOfobj);
    return "qrcodes";
    }

    最佳答案

    这是一种我认为代表您正在尝试做的方法。

    最终结果

    跳转到最终结果,这将在浏览器中显示以下文本:

    Deck: Card1 Card2 Card3 Card4
    Deck: Card5 Card6 Card7 Card8
    Deck: Card9 Card10 Card11

    更有用的是,HTML 如下所示:
    <div class="card-deck">
    <span>Deck: </span>
    <span class="card">Card1 </span>
    <span class="card">Card2 </span>
    <span class="card">Card3 </span>
    <span class="card">Card4 </span>
    </div>
    <div class="card-deck">
    <span>Deck: </span>
    <span class="card">Card5 </span>
    <span class="card">Card6 </span>
    <span class="card">Card7 </span>
    <span class="card">Card8 </span>
    </div>
    <div class="card-deck">
    <span>Deck: </span>
    <span class="card">Card9 </span>
    <span class="card">Card10 </span>
    <span class="card">Card11 </span>
    </div>

    Java 对象

    甲板:
    public class Deck {

    private final String deckName;
    private final List<Card> cards = new ArrayList();

    public Deck(String deckName) {
    this.deckName = deckName;
    }

    public List<Card> getCards() {
    return cards;
    }

    public String getDeckName() {
    return deckName;
    }

    }

    卡片:
    public class Card {

    private final String cardName;

    public Card(String cardName) {
    this.cardName = cardName;
    }

    public String getCardName() {
    return cardName;
    }

    }

    组装甲板:
    Map<String, Object> model = new HashMap();

    // this is equivalent to your findAll()...
    List<Card> allCards = new ArrayList();
    for (int i = 1; i<= 11; i++) {
    allCards.add(new Card("Card" + i));
    }

    int maxCardsPerDeck = 4;
    List<Deck> decks = new ArrayList();

    Deck deck;
    List<Card> deckCards = new ArrayList();
    int cardCount = 0;
    for (Card card : allCards) {
    cardCount++;
    deckCards.add(card);
    if (cardCount % maxCardsPerDeck == 0 ||
    cardCount == allCards.size()) {
    deck = new Deck("Deck");
    deck.getCards().addAll(deckCards);
    decks.add(deck);
    deckCards.clear();
    }
    }

    model.put("decks", decks);

    上面的代码相当粗糙 - 它可能会被改进。但重点是:它组合了一组牌组,每副牌组包含的牌数不超过允许的最大数量(在本例中为 4)。

    thymeleaf
    <div class="card-deck"
    th:each="deck: ${decks}">
    <span th:text="${deck.deckName + ': '}">
    </span>
    <span class="card"
    th:each="card: ${deck.cards}"
    th:text="${card.cardName + ' '}">
    </span>
    </div>

    我用过 <span> s 在这里,只是为了让事情保持一致。你可以使用任何你需要的东西,并提供你需要的 CSS 样式。

    关于java - Thyemleaf 嵌套迭代触发 org.thymeleaf.exceptions.TemplateInputException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61111220/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com