SpringBoot #3.2 Spring MVC

텐저린티·2023년 7월 4일
0

데브코스

목록 보기
23/41

Spring MVC 처리 흐름

  1. DispatcherServlet의 HTTP 요청 수신
  2. DispatcherServlet에서 Contoller로 HTTP 요청 위임
  3. Contoller가 모델 생성 + 정보 등록
  4. Contoller 결과 리턴 → Model + View 반환
  5. DispatcherServlet의 View 호출
  6. View의 Model 참조 + View 생성 반환
  7. HTTP 응답

DispatcherServlet

Front Contoller Pattern


DispatcherServlet 에서 컨트롤러로 HTTP 요청 위임

  • 스프링 컨트롤러 = 핸들러

  • 핸들러 매핑 전략 : 사용자 요청을 어떤 핸들러에게 작업 위임할지 결정

    • RequestMappingHandlerMapping
  • 해당 컨트롤러 오브젝트 메소드 호출로만 요청 전달

  • 핸들러 어댑터 전략

    • 각기 다른 메소드, 포맷 가진 컨트롤러를 DispatcherServlet이 호출할 수 있도록 하는 전략
    • HandlerAdapter interface > AbstractHandlerMethodAdapter
    • 위 그림처럼 컨트롤러를 핸들러 어댑터로 관리
    • 핸들러 어댑터를 핸들러에 등록하면 DispatcherSevlet이 컨트롤러에 맞게 변환해서 호출

DispatcherServlet 뷰 호출과 모델 참조

  1. DispatcherServlet이 컨트롤러에 핸들러를 통해 요청 전달
  2. 컨트롤러가 뷰 이름, 모델 데이터를 DispatcherServlet으로 전달
  3. DispatcherServlet이 InternalResourceViewResolver에 뷰 요청
  4. ViewResolver가 DispatcherServlet에 jsp 반환
  5. DispatcherServlet이 View에 jsp와 model 전달
  • InternaResourceViewResolver
  • ThymleafResolver

구현

JSTL

  • 요즘은 잘 안 씀 ㅠ
  • gradle
    implementation "javax.servlet:jstl";
  • maven
    <dependency>
    		<groupId>javax.servlet</groupId>
    		<artifactId>jstl</artifactId>
    		<version>1.2</version>
    </dependency>

Thymeleaf

  • gradle
implements "org.springframework.boot:spring-boot-starter-thymeleaf"

공식문서

Introduction to Using Thymeleaf in Spring | Baeldung

표현식

  • 변수식 : ${OGNL}
    • OGNL (Object-Graph Navigation Language)
      • 객체 속성에 값 가져오기, 설정에 사용
    • th:text=”${today}”
  • 메시지 식 : #{코드}
    • 스프링 메시지 소스와 연동하면 사용 가능
    • 메시지 코드 넣으면 해당 메시지 출력
    • 다국어 처리 용이
  • 링크 식 : @{링크}
    • /로 시작하는 패스는 자동으로 애플리케이션 컨텍스트 네임이 앞에 붙음
    • th:href=”@{/order/{orderId}}”
  • 선택 변수 식 : *{OGNL}
    • th:object 로 선택한 객체 한해 필드 접근 가능

      <tr th:each="customer: ${customers}" th:object="${customer}" >
          <td th:text="${customer.customerId}"></td>
          <td th:text="*{name}"></td>
          <td th:text="*{email}"></td>
          <td th:text="*{createdAt}"></td>
          <td th:text="*{lastLoginAt}"></td>
      </tr>
profile
개발하고 말테야

0개의 댓글

관련 채용 정보