'<th:block th:each> is not retrieving data required? Table shows blank
This is my JSP file
<html>
<head>
<title>Todo's for ${name}</title>
</head>
<body>
<h1>Your ToDo's</h1>
<table>
<caption>Your Todo's are as follows:</caption>
<thead>
<tr>
<th>Description</th>
<th>Target Date</th>
<th>Is it completed?</th>
</tr>
</thead>
<tbody>
<th:block th:each="todo : ${todos}">
<tr>
<td th:text="${todo.desc}"></td>
<td th:text="${todo.targetDate}"></td>
<td th:text="${todo.done}"></td>
</tr>
</th:block>
</tbody>
</table>
<br/>
<a href="/add-todos">Add Todo</a>
</body>
</html>
This is my TodoService.java file
package com.example.springboot.demo.service;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.springframework.stereotype.Service;
import com.in28minutes.springboot.web.model.Todo;
@Service
public class TodoService {
private static List<Todo> todos = new ArrayList<Todo>();
private static int todoCount = 3;
static {
todos.add(new Todo(1, "Prakriti", "Learn Spring MVC", new Date(),
false));
todos.add(new Todo(2, "Prakriti", "Learn Struts", new Date(), false));
todos.add(new Todo(3, "Prakriti", "Learn Hibernate", new Date(),
false));
}
public List<Todo> retrieveTodos(String user) {
List<Todo> filteredTodos = new ArrayList<Todo>();
for (Todo todo : todos) {
if (todo.getUser().equals(user)) {
filteredTodos.add(todo);
}
}
return filteredTodos;
}
public void addTodo(String name, String desc, Date targetDate,
boolean isDone) {
todos.add(new Todo(++todoCount, name, desc, targetDate, isDone));
}
public void deleteTodo(int id) {
Iterator<Todo> iterator = todos.iterator();
while (iterator.hasNext()) {
Todo todo = iterator.next();
if (todo.getId() == id) {
iterator.remove();
}
}
}
}
I have three values in the table but it doesn't retrieve them, can't understand why I have used the thymeleaf block properly only. Below is my TodoController.java file.
package com.example.springboot.demo.controller;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import com.example.springboot.demo.service.LoginService;
import com.example.springboot.demo.service.TodoService;
@Controller
@SessionAttributes("name")
public class TodoController {
@Autowired
TodoService service;
@RequestMapping(value="/list-todos", method=RequestMethod.GET)
public String showTodosList(ModelMap model) {
String name=(String)model.get("name");
model.put("todos",service.retrieveTodos(name));
return "list-todos";
}
@RequestMapping(value="/add-todos", method=RequestMethod.GET)
public String showaddTodoPage(ModelMap model) {
return "add-todos";
}
@RequestMapping(value="/add-todos", method=RequestMethod.POST)
public String addTodos(ModelMap model, @RequestParam String desc) {
service.addTodo((String)model.get("name"), desc, new Date(), false);
return "redirect:/list-todos";
}
}
This is my Todo.java
package com.in28minutes.springboot.web.model;
import java.util.Date;
public class Todo {
private int id;
private String user;
private String desc;
private Date targetDate;
private boolean isDone;
public Todo(int id, String user, String desc, Date targetDate,
boolean isDone) {
super();
this.id = id;
this.user = user;
this.desc = desc;
this.targetDate = targetDate;
this.isDone = isDone;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public Date getTargetDate() {
return targetDate;
}
public void setTargetDate(Date targetDate) {
this.targetDate = targetDate;
}
public boolean isDone() {
return isDone;
}
public void setDone(boolean isDone) {
this.isDone = isDone;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Todo other = (Todo) obj;
if (id != other.id) {
return false;
}
return true;
}
@Override
public String toString() {
return String.format(
"Todo [id=%s, user=%s, desc=%s, targetDate=%s, isDone=%s]", id,
user, desc, targetDate, isDone);
}
}
Please let me know what's needed to be done for the values to be displayed in the table.
Solution 1:[1]
Try using,
<tr th:each="todo : ${todos}">
<td th:text="${todo.desc}"></td>
<td th:text="${todo.targetDate}"></td>
<td th:text="${todo.done}"></td>
</tr>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Abhale Amol |

