김영한님의 '실전! 스프링 부트와 JPA 활용2 - API 개발과 성능 최적화' 강의를 들으면서 데이터를 조회하는 API를 개발하던 중
기존의 조회하면은 A라는 사용자가 ('JPA도서', 'Spring도서')라는 상품을 두가지 주문했을 때 대표 상품으로
JPA도서만 화면에 출력되었다.
대표 상품만 조회되는 것에서 사용자별 모든 주문목록을 조회리스트로 뽑아내라는 과제를 주셨다.
어떻게 할까 고민하던 중 테이블 코드가 이런식으로 되있는 것을 보고
item.orderItems[0]부분을 2중 each로 뽑아내면 될것이라고 생각하였다.
다음과 같은 코드로 짰을때 이와 같은 에러가 뜨는 것을 확인했다.
org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'orderItems' cannot be found on null
아무리 생각해도 tr태그 내부에 tr을 한번더 감싼다는 것이 말이 안되는 것 같아보였다.
<tbody>
<span th:each="item : ${orders}">
<tr th:each="orderItems : ${item.orderItems}">
<td th:text="${orderItems.item.id}"></td>
<td th:text="${item.member.name}"></td>
<td th:text="${orderItems.item.name}"></td>
<td th:text="${orderItems.orderPrice}"></td>
<td th:text="${orderItems.count}"></td>
<td th:text="${item.status}"></td>
<td th:text="${item.orderDate}"></td>
<td>
<a th:if="${item.status.name() == 'ORDER'}" href="#" th:href="'javascript:cancel('+${item.id}+')'"
class="btn btn-danger">CANCEL</a>
</td>
</tr>
</span>
</tbody>
상위 each 태그를 span으로 감쌌을 때 되는 것을 볼 수 있었다.
그리고 클래스의 내부 클래스로 한 단계씩 접근할 때 온점(.)으로 접근하는 것을 다시한번 배우게 되었다.