Lav Sharma
Lav Sharma

Reputation: 339

data not being displayed in spring boot using thymeleaf

    @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);
    }
<%@ 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>

enter image description here

Upvotes: 0

Views: 1200

Answers (1)

zulqarnain
zulqarnain

Reputation: 1735

Try this:

@GetMapping(value = "/successful")
    public String registered(Model model) {
        List<Student> students = dao.getStudents();
        model.addAttribute("students", students);
        return "index";
    }

Upvotes: 1

Related Questions