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.
This is the code that generates the exception on line 234
UPDATE: Note: line 234 is mentioned in html
and had the <!-- Error-Line 234 -->
due to a missing )
at ${#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)
I've already been on these topics
- 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
and gone through this documentation
and still can't figure a proper way of doing it , without triggering an 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. Due to <div class="card" th:each="i : ${#numbers.sequence(0,3)} ">
I get the 4 same tables.
qr.tableid
are the table numbers, 1 to x
For purposes of demonstration take a look at this java
snippet
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:
This is my 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";
}