ABOUT ME

Today
Yesterday
Total
  • [SpringBoot] SpringBoot @ControllerAdvice
    WEB/SpringBoot 2023. 4. 13. 18:28

     

     

     

    @ControllerAdvice는 스프링 프레임워크에서 제공하는 애노테이션으로, @Controller가 붙은 클래스에서 발생하는 예외를 전역적으로 처리할 수 있는 방법을 제공합니다.
    @ExceptionHandler, @InitBinder, @ModelAttribute와 같은 애노테이션을 이용하여 전역적으로 적용할 수 있는 메서드를 정의할 수 있습니다. 보통 예외 처리나 공통적인 모델 객체를 포함하는 기능을 구현할 때 사용됩니다.

     

     

     

    @ModelAttribute

    @ModelAttribute 어노테이션을 컨트롤러 메소드의 매개변수나 메소드 내부의 변수에 적용하면, 해당 변수에 전달받은 파라미터 값이 자동으로 바인딩됩니다. @ModelAttribute 어노테이션을 @ControllerAdvice 어노테이션이 적용된 클래스의 메소드에 적용하면, 해당 메소드의 반환값이 모든 컨트롤러에서 공통적으로 사용될 수 있는 전역 모델(Global Model) 객체에 자동으로 추가됩니다.

     

     

    @ControllerAdvice
    public class GlobalController {
        @ModelAttribute("user")
        public String User() {
            return "userName";
        }
    }

     

    모든 jsp에서 해당 user라는 변수를 사용할 수 있다.

    <p>Welcome, ${user}!</p>

     

     

     

     

     

    728x90
Designed by Tistory.