简体   繁体   English

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

[英]Thyemleaf nested iteration triggers org.thymeleaf.exceptions.TemplateInputException

I'm trying to iterate through a list of objects and generate a div class="card-deck" every 4 objects and a nested div class="card" for every object.我正在尝试遍历对象列表并每4对象生成一个div class="card-deck" ,并为每个 object 生成一个嵌套的div class="card"

This is the code that generates the exception on line 234这是在第 234 行生成异常的代码

UPDATE: Note: line 234 is mentioned in html and had the <!-- Error-Line 234 --> due to a missing ) at ${#numbers.sequence(0,3}更新:注意: html )提到了第 234 行<!-- 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) org.thymeleaf.exceptions.TemplateInputException:模板解析期间发生错误(模板:“类路径资源 [templates/qrcodes.html]” - 第 234 行,第 10 列)

I've already been on these topics我已经讨论过这些话题

and gone through this documentation并浏览了这个文档

and still can't figure a proper way of doing it, without triggering an exception并且仍然无法在不触发exception的情况下找到正确的方法

UPDATE: Exception is fixed, the logic i'm trying implementing doesn't have the desired outcome:更新:异常已修复,我尝试实现的逻辑没有预期的结果:

Outcome of the above snippet:上述片段的结果:

在此处输入图像描述

Imagine there are 8 tables, table1, table2... table8, i'm trying to generate a <div class="card-deck"... for every 4 or 5 tables.想象一下有 8 个表,table1,table2...table8,我正在尝试为每 4 或 5 个表生成一个<div class="card-deck"... Due to <div class="card" th:each="i: ${#numbers.sequence(0,3)} "> I get the 4 same tables.由于<div class="card" th:each="i: ${#numbers.sequence(0,3)} ">我得到了 4 个相同的表。

  • qr.tableid are the table numbers, 1 to x qr.tableid是表格编号,从 1 到 x

For purposes of demonstration take a look at this java snippet出于演示目的,请查看此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)+" ");    
     }

Output: Output:

在此处输入图像描述

This is my Controller这是我的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";
      }

Here is an approach which I think represents what you are trying to do.这是一种我认为代表您正在尝试做的方法的方法。

End Result最终结果

Jumping to the end result, this will display the following text in a browser:跳转到最终结果,这将在浏览器中显示以下文本:

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

More usefully, the HTML is as follows:更有用的是,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>

The Java Objects Java 对象

The Deck:甲板:

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;
    }

}

The Card:卡片:

public class Card {

    private final String cardName;

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

    public String getCardName() {
        return cardName;
    }

}

Assembling the Decks:组装甲板:

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);

The above code is fairly crude - it could probably be refined.上面的代码相当粗糙——它可能会被改进。 But the point is: it assembles a collection of decks, with each deck containing no more than the allowed maximum of cards (4 in this example).但重点是:它组合了一组牌组,每个牌组包含的牌数不超过允许的最大值(本例中为 4 张)。

The Thymeleaf 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>

I used <span> s here, just so things are aligned.我在这里使用了<span> ,只是为了对齐。 You can use whatever you need, and provide the CSS styling you need also.您可以使用任何您需要的东西,并提供您需要的 CSS 样式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Thymeleaf:org.thymeleaf.exceptions.TemplateInputException - Thymeleaf : org.thymeleaf.exceptions.TemplateInputException 如何调试org.thymeleaf.exceptions.TemplateInputException? - How to debug org.thymeleaf.exceptions.TemplateInputException? org.thymeleaf.exceptions.TemplateInputException:Spring Boot - org.thymeleaf.exceptions.TemplateInputException: Spring Boot springboot项目的问题:org.thymeleaf.exceptions.TemplateInputException - issue with springboot project : org.thymeleaf.exceptions.TemplateInputException org.thymeleaf.exceptions.TemplateInputException:解决片段错误:无法解析模板或片段 - org.thymeleaf.exceptions.TemplateInputException: Error resolving fragment: template or fragment could not be resolved 我的spring-boot应用程序给出以下错误“ org.thymeleaf.exceptions.TemplateInputException:” - My spring-boot app gives the following error “org.thymeleaf.exceptions.TemplateInputException:” org.thymeleaf.exceptions.TemplateInputException:异常分析文档:template =“ login”,第36行-第3列 - org.thymeleaf.exceptions.TemplateInputException: Exception parsing document: template=“login”, line 36 - column 3 如何摆脱 org.thymeleaf.exceptions.TemplateInputException: 同时使用 thymeleaf 表达式以引导卡的形式打印数据? - how to get rid of org.thymeleaf.exceptions.TemplateInputException: while using thymeleaf expression to print data in form of bootstrap cards? 嵌套迭代百里香 - Nested iteration thymeleaf org.thymeleaf.exceptions.TemplateProcessingException:串联 href - org.thymeleaf.exceptions.TemplateProcessingException: Concatenation href
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM