BugDIARY

[SpringBoot]뷰 화면 연결 본문

IT/Java

[SpringBoot]뷰 화면 연결

HEMON 2023. 5. 27. 13:43
이 글은 나중에 프로젝트를 생성할 경우 참고하기 위한 글입니다.
또한 김영한님의 스프링 입문 - 코드로 배우는 스프링 부트, 웹 MVC, DB 접근 기술을 학습한 내용입니다.

 

작업 내역
- View 화면 설정

 

 

Spring Boot의 index.html과 관련된 지원 내용이 기재되어 있음.

https://docs.spring.io/spring-boot/docs/2.7.12/reference/html/web.html#web

 

Web

Graceful shutdown is supported with all four embedded web servers (Jetty, Reactor Netty, Tomcat, and Undertow) and with both reactive and servlet-based web applications. It occurs as part of closing the application context and is performed in the earliest

docs.spring.io

 

Spring boot는 정적, 템플릿 시작페이지를 모두 지원하며 정적 콘텐츠위에서 index.html파일을 찾는다.

index.html을 찾을 수 없는 경우 index템플릿을 찾는다.

둘 중 하나가 발견되면 자동으로 애플리케이션의 시작페이지로 사용된다.

 

아래는 reference문서 내에 있는 내용 중 하나이다. (영어->한국 번역)

1.1.7. 경로 일치 및 콘텐츠 협상
Spring MVC는 요청 경로를 보고 애플리케이션에 정의된 매핑(예: @GetMapping컨트롤러 메서드의 주석)과 일치시켜 수신되는 HTTP 요청을 핸들러에 매핑할 수 있습니다.
Spring Boot는 기본적으로 접미사 패턴 일치를 비활성화하도록 선택합니다. 

이런 이유로 요청이 들어왔을 때 spring은 요청에 맞는 Mapping의 메서드를 실행시키게 되는 것이다. 

 

[예시 코드 참조]

@Controller
public class HelloController {

    @GetMapping("hello")
    public String hello(Model model){
        model.addAttribute("data","YOY");
        return "hello";
    }
}

 

순서를 나열해 보자면 아래와 같다.

1. HTTP에서 요청이 들어옴 
2. Spring MVC가 요청 경로를 확인 후 Application이 있는 컨트롤러의 매핑과 일치시킴
3. @GetMapping과 일치하는 경우 그 매핑이 정의된 메소드를 실행시킴
4. 사용하는 템플릿 엔진에 따라 템플릿에서 자동으로 선택되게 해줌.
Comments