티스토리 뷰

    @GetMapping("/members")
    public String list(Model model){
        model.addAttribute("members", memberService.findMembers()); //control + t 에 인라인으로 코드를 줄일 수 있다.

        return "members/memberList";
    }

 

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="fragments/header :: header" />
<body>
<div class="container">
    <div th:replace="fragments/bodyHeader :: bodyHeader" />
    <div>
        <table class="table table-striped">
            <thead>
            <tr>
                <th>#</th>
                <th>이름</th> <th>도시</th> <th>주소</th>
                <th>우편번호</th> </tr>
            </thead>
            <tbody>
            <tr th:each="member : ${members}">
                <td th:text="${member.id}"></td>
                <td th:text="${member.name}"></td>
                <td th:text="${member.address?.city}"></td><!--?는 null이면 진행 안한다.-->
                <td th:text="${member.address?.street}"></td>
                <td th:text="${member.address?.zipcode}"></td>
            </tr>
            </tbody>
        </table>
    </div>
    <div th:replace="fragments/footer :: footer" />
</div> <!-- /container -->
</body>
</html>

 

소스를 봤을 때에 컨트롤러 model에 담아 html에 뿌려줬다. 여기에서 주의점이 있다.

 

지금 model에는 members 엔티티 그 자체를 담아 뿌려줬다.

 

실무에서는 화면에 뿌릴 데이터가 일정하지 않은 경우가 있어 반환을 할 때에는 form 객체나 dto를 만들어 set해준 후에 model에 뿌려주는 것이 좋다. 엔티티는 그 자체로 최대한 변경이 없게 끔 사용해야한다.

 

특히 api를 사용할 때에는 더더욱 엔티티를 보내면 안된다. api는 스펙을 정한 후에 보내주게 되는데, 엔티티가 수정되거나 개인정보 등이 추가 될 경우 그대로 보내지기 때문에 더더욱 위험하다. api는 스펙이 변경되면 위험하다. 불안전한 스펙이 되어버리면 그것을 가져다 사용하는 개발자들이 불안 할 것이다.

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/10   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
글 보관함