I am trying to iterate over an List and show the results in a table, but the table stays empty.
@RequestMapping(value="/home")
public String home(Model model) throws IOException {
List<OrderNotify> notifications = new ArrayList<OrderNotify>();
for (int i = 0; i < 10; i++) {
OrderNotify notification = new OrderNotify("1", "2", "3", "4");
notifications.add(notification);
}
model.addAttribute("notifications", notifications);
return "home";
}
<table id="handle" style=" margin:auto">
<tr>
<th>Bestellnummer</th>
<th>Datum</th>
<th>Status</th>
<th>Handlung erforderlich</th>
</tr>
<tr th:each="notify : ${notifications}">
<td th:text="${notify.orderid}"></td>
<td th:text="${notify.date}"></td>
<td th:text="${notify.status}"></td>
<td th:text="${notify.handle}"> </td>
</tr>
</table>
public class OrderNotify {
public String orderid;
public String date;
public String status;
public String handle;
public OrderNotify(String orderid, String date, String status, String handle) {
this.orderid = orderid;
this.date = date;
this.status = status;
this.handle = handle;
}
public List<String> getAll(){
return null;
}
public String getOrderid() {
return orderid;
}
public void setOrderid(String orderid) {
this.orderid = orderid;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getHandle() {
return handle;
}
public void setHandle(String handle) {
this.handle = handle;
}
}
I expected do see 10 rows with the same information of the OrderNotify objects of the list, but it was empty. I looked at the source code in the browser and got following result:
<tr th:each="notify : [com.example.spring.utils.OrderNotify@42b3c931, com.example.spring.utils.OrderNotify@76af8a32, com.example.spring.utils.OrderNotify@28025a03, com.example.spring.utils.OrderNotify@5e6eadf0, com.example.spring.utils.OrderNotify@2481d4d, com.example.spring.utils.OrderNotify@83ce92c, com.example.spring.utils.OrderNotify@3254786a, com.example.spring.utils.OrderNotify@197e42fd, com.example.spring.utils.OrderNotify@5b1e86ea, com.example.spring.utils.OrderNotify@3484712c]">
<td th:text=""></td>
<td th:text=""></td>
<td th:text=""></td>
<td th:text=""> </td>
</tr>
Thymeleaf and JSP are two different server side rendering templates. You can’t use them simultaneously without configurations.
Basing on your codes, you should update settings for using Thymeleaf:
a. change home.jsp to home.html
b. move home.html to the path: src/main/resources/templates
c. config Thymeleaf in application.properties:
# Thymeleaf
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML5
spring.thymeleaf.cache=false
d. config Thymeleaf dependency in pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
For Viewing templates, SpringBoot recommends use Thymeleaf instead of JSP, see more for details.
For using Thymeleaf and JSP simultaneously in SpringBoot app, you need to configure more. Below articles for your reference:
Using both Thymeleaf and JSP
https://www.oodlestechnologies.com/blogs/Use-Thymeleaf-And-JSP-Simultaneously-In-Spring-Boot-App/