Adventure Time - Lady Rainicorn [Spring Boot] View 환경설정
본문 바로가기
💻공부/Spring 🍃

[Spring Boot] View 환경설정

by 강켄트 2023. 1. 7.

☺️ 김영한님의 '스프링 입문 - 코드로 배우는 스프링 부트, 웹 MVC, DB 접근 기술'을 공부하고 정리한 글입니다.

 

  • 정적 파일 동작 원리
  • 템플릿 엔진 동작 원리 

 

🍃 Spring Boot가 제공하는 Welcom Page 기능 - 정적 파일

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#web.servlet.spring-mvc.static-content

 

Spring Boot Reference Documentation

This section goes into more detail about how you should use Spring Boot. It covers topics such as build systems, auto-configuration, and how to run your applications. We also cover some Spring Boot best practices. Although there is nothing particularly spe

docs.spring.io

⚙️ 동작 원리

resources/static 폴더에 index.html을 생성하면index를 웰컴 페이지(도메인 누르고 들어왔을 때 첫 화면)로 인식한다. 

 

 

🍃 Spring Boot의 템플릿 엔진 - 동적 파일 

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#web.servlet.spring-mvc.template-engines

 

Spring Boot Reference Documentation

This section goes into more detail about how you should use Spring Boot. It covers topics such as build systems, auto-configuration, and how to run your applications. We also cover some Spring Boot best practices. Although there is nothing particularly spe

docs.spring.io

 

⚙️ 동작 원리

웹 브라우저에 http://localhost:8080/hello  라고 입력하면 일어나는 일

(스프링 부트는 톰캣이라는 웹서버를 내장하고 있다.)

김영한 : 스프링 입문

 

HelloController의 hello 메서드가 실행되고

파라미터로 model을 받으니까 스프링이 model이라는 것을 만들어 넣어준다.

여기서 나는 model에 key=data, value=hello!!로 설정했다. 

@Controller
public class HelloController {
    @GetMapping("hello") //웹어플리케이션에서 /hello가 들어오면 이 메서드를 호출해줌
    public String hello(Model moldel){
    molde.addAttribute("data", "hello!!");
    
    return "hello"; //String은 뷰이름을 리턴(이 화면을 실행시켜라)
    }
}

 

위의 코드에서 hello를 리턴하면 스프링 부트는 resources/templates 밑에 "hello.html"를 찾아서 실행한다.

(여기서 템플릿 엔진은 thymeleaf를 썼으니까 hello.html을 thymeleaf가 처리해준다.)

 

💡컨트롤러에서 리턴 값으로 문자를 반환하면 뷰 리졸버(viewResolver)가 화면을 찾아서 처리한다. 

💡스프링 부트 템플릿 엔진은 기본적으로 viewName을 매핑한다.

resources/templates/ + viewName + .html 

 

댓글