Issue
- I am trying to implement a project in which the values are being fetched from
MySQL
and when I am trying to return it to theHTML
page for displaying. It is not being display but it can be seen as alist
. Controller
@GetMapping(value = "/successful")
public ModelAndView registered(Model model) {
List<Student> student = dao.getStudents();
Map<String, Object> params = new HashMap<>();
System.out.println(params);
params.put("students", student);
return new ModelAndView("index", params);
}
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table>
<tr>
<th>Name</th>
<th>DOB</th>
<th>Address</th>
</tr>
<tr th:each="student : ${students}">
<td th:text="${student.name}"></td>
<td th:text="${student.dob}"></td>
<td th:text="${student.address}"></td>
</tr>
</table>
<script>
var data = ${students};
console.log(data);
</script>
</body>
</html>
- Output that I am able to get as you can see below the
<td th:text="${student.name}"></td>
is empty.
Solution
Try this:
@GetMapping(value = "/successful")
public String registered(Model model) {
List<Student> students = dao.getStudents();
model.addAttribute("students", students);
return "index";
}
Answered By - zulqarnain
Answer Checked By - Willingham (JavaFixing Volunteer)