프로그래밍/thymeleaf

타임리프(thymeleaf) map 데이터 출력

Baesj 2021. 9. 15. 15:40

map 데이터를 출력하여 보자.

 

컨트롤러에서 데이터를 보낸다.

    @GetMapping("/th-map")
    public String thMap(Model model) {
        Map<String, Integer> score = new HashMap<>();
        score.put("userA", 100);
        score.put("userB", 90);
        score.put("userC", 50);
        model.addAttribute("score", score);
        return "view/th-map";
    }
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
</head>
<body>
<h2>th map</h2>
<div>
    <p>
        <span>userA</span>
        <span th:text="${score['userA']}"></span>
    </p>
    <hr>
    <p th:each="entry : ${score}">
        <span th:text="${entry.key}"></span>
        <span th:text="${entry.value}"></span>
    </p>
</div>
</body>
</html>

th:text="${score['userA']}" 이렇게 특정값을 바로 받을 수 있고,

th:each 를 통해 반복할 수도 있다.

 

결과 화면