티스토리 뷰
@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는 스펙이 변경되면 위험하다. 불안전한 스펙이 되어버리면 그것을 가져다 사용하는 개발자들이 불안 할 것이다.
'dev > spring JPA 활용 웹만들기' 카테고리의 다른 글
jpa 1차 캐시와 동일성 보장 (0) | 2024.01.29 |
---|---|
merge와 변경감지의 차이점과 주의점 (0) | 2024.01.18 |
thymleaf @valid , bindingResult 오류처리 (0) | 2024.01.09 |
thyleaf fragments이용 (인크루드) (0) | 2024.01.09 |
jpa 강점 (1) | 2024.01.08 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 스프링공부
- 예외처리
- 백엔드 개발자 공부
- 향해99
- 항해플러스
- 향해플러스백엔드
- SpringBoot
- jpa api
- BindingResult
- Java
- 리터럴
- 백엔드 개발자 역량
- 스프링부트
- 컨트
- Intercepter
- react실행
- 향해플러스
- JPA
- thymleaf
- 로그인
- exception
- 항해99
- rejectValue
- ArgumentResolver
- React
- filter
- reject
- hypertexttransferprotocol
- HTTP
- 인터셉터
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함