👉 Spring MVC 이란?
클라이언트 HTTP Request 서버에 요청으로 응답하기 위해서 세가지 구성요소가 있습니다. Controller, Model, View 등 MVC 패턴으로 return 값으로 반환합니다.
1. MVC 구조
세가지 구성요소인 Controller, Model, View 나눠 어떻게 동작하는 알아보겠습니다.
- DispatcherServlet(Front Controller)
- Handler(Controller)
- ModelAndView
- ViewResolver
2. MVC 패턴 동작순서
클라이언트 HTTP Request 요청으로 DispatcherServlet(Front Controller) 처리하여, Controller에서는 Model를 만들어 데이터를 저장합니다. 그리고나서 Model에 있는 데이터 값을 받아와 return 값으로 View 화면에 반환합니다.
Controller
@GetMapping("/list")
public String list(Model model){
Board boards = boardService.findAll();
model.addAttribute("boards",boards);
return "board/boardList";
}
Model
public Board findAll() {
return boardRepository.findAll();
}
View
<table class="table caption-top">
<tr class="tr">
<th scope="col">No</th>
<th scope="col">제목</th>
<th scope="col">내용</th>
<th scope="col">작성자</th>
<th scope="col">작성일</th>
<th scope="col">조회수</th>
</tr>
<tr th:each="board : ${boards}">
<td th:text="${board.id}"></td>
<td><a th:text="${board.title}" th:href="@{/board/detail(id=${board.id})}">Title</a></td>
<td th:text="${board.content}"></td>
<td th:text="${board.createBy}"></td>
<td th:text="${board.regTime}"></td>
<td th:text="${board.hit}"></td>
</tr>
</table>
'BackEnd > SpringFramework' 카테고리의 다른 글
Spring Framework 란? (0) | 2022.04.12 |
---|