본문 바로가기
Java, Kotlin, Spring/Spring, Spring Boot

[Spring Boot] 16 - DataBinder, 템플릿 엔진, 에러 핸들링

by Wordbe 2020. 4. 7.
728x90

Spring Boot 016 - DataBinder, 템플릿 엔진, 에러 핸들링

7.1.9 ConfigurableWebBindingInitializer

Spring MVC 는 특정 요청이 들어올 때 WebDataBinder를 초기화 시키기 위해 WebBindingInitializer를 사용합니다. ConfigurableWebBindingInitializer @Bean 을 생성하기 위해 스프링부트는 자동적으로 스프링 MVC를 설정합니다.


7.1.10 템플릿 엔진

REST 웹 서비스 뿐 아니라, 스프링부트를 사용해서 동적 HTML 컨텐츠를 서비스할 수 있습니다. Spring MVC는 다양한 템플릿 기술을 지원합니다.

가능하다면 JSP는 사용을 피하도록 합니다. 내장된 서블릿 컨테이너와 함께 사용할 때 발생하는 알려진 한계들이 존재하기 때문입니다.

https://start.spring.io/ 에서 템플릿 엔진이 포함된 메이븐 프로젝트 파일을 생성할 수 있습니다.

WebController.java

package me.whiteship.springbootday016thymeleaf;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class WebController {

    @GetMapping("/hello")
    public String hello(Model model, @RequestParam String name){
        model.addAttribute("name", name);
        return "hello";
    }
}

FreeMarket

resources/static/hello.ftl

<html>
<head>
  <title>Welcome!</title>
</head>
<body>
  <h1>Welcome ${name}!</h1>
</body>
</html>

주소창에 http://localhost:8080/hello?name=seongho

이런 식으로 파라미터 값을 넣어봅시다. 파라미터 값을 뷰로 받아 viewResolver가 이를 뷰 객체로 전환하게 됩니다. 모델에 name이라는 받아온 값이 추가되고, html은 이를 출력합니다.

Thymeleaf

resources/static/hello.html

<!DOCTYPE html>

<html xmlns:th="http://www.thymeleaf.org">

<head>
    <title>Good Thymes Virtual Grocery</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" type="text/css" media="all"
          href="../../css/gtvg.css" th:href="@{/css/gtvg.css}" />
</head>

<body>

Hello <p th:text="${name}">Hello Whiteship</p>

</body>

</html>

IntelliJ IDEA에서 메인 메소드를 실행하는 것과, 패키지된 jar파일을 Maven 또는 Gradle로 실행시키는 것에서 조금 차이가 날 수 있습니다. 이는 Spring Boot로 하여금 클래스패스에 있는 템플릿을 찾는 것을 실패하게 할 수도 있습니다. 이런 문제를 가진다면, 클래스 파일들과 리소스들이 다른 jar 파일에 있는 것 보다 우선순위가 되도록 조정합니다. 또는 클래스 패스에 있는 모든 template 디렉토리를 살펴보도록 템플릿 접두사를 설정할 수 있습니다. (classpath*:/templates/)


7.1.11 Error Handling

스프링 부트는 기본적으로 /error 맵핑을 제공합니다. 또한 서블릿 컨네이너에서 전역적 에러페이지를 등록합니다.

  • machine 클라이언트에게는 에러의 상세사항, HTTP 상태, 예외 메시지가 JSON 응답으로 제공됩니다.
  • browser 클라이언트에게는 HTML 형식의 같은 데이터를 렌더링하는 "whitelabel" 에러 뷰가 있습니다. 기본 행동을 대체하려면, ErrorController를 사용하고 타입 정의에 대한 bean 정의를 등록할 수 있습니다. 또는 ErrorAttributes 의 bean을 추가함으로써 가능합니다.

BasicErrorController 는 커스텀 ErrorController 의 베이스 클래스로 사용될 수 있습니다. BasicErrorController 를 확장하고, produces 속성을 가진 @RequestMapping 과 함께 public 메소드를 추가하고, 새로운 타입의 bean을 생성하면 됩니다.


Reference

https://www.youtube.com/watch?v=V-gBDmR6gZk&list=PLfI752FpVCS8tDT1QEYwcXmkKDz-_6nm3&index=17&t=5s

https://docs.spring.io/spring-boot/docs/2.2.4.RELEASE/reference/html/spring-boot-features.html#boot-features-spring-mvc-web-binding-initializer

728x90

댓글