Yeji's Tech Notes
๋ฐ˜์‘ํ˜•

๐ŸŒธ๊ฐœ์š”๐ŸŒธ

SpringBoot version up (1.3.X -> 3.2.1) ๊ณผ์ •์—์„œ migrationํ•˜๋Š” ์ค‘ ํƒ€์ž„๋ฆฌํ”„ ์—ญ์‹œ 2 -> 3 ๋ฒ„์ „์œผ๋กœ ์˜ฌ๋ผ๊ฐ€๋Š” ์ƒํ™ฉ์ด์˜€์Šต๋‹ˆ๋‹ค.์ด๋•Œ thymeleaf์—์„œ #httpServletRequest ๊ธฐ๋ณธ ๊ธฐ๋Šฅ์„ ๋”์ด์ƒ ์ง€์›ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์„ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค.

https://www.thymeleaf.org/doc/articles/thymeleaf31whatsnew.html

 

Thymeleaf 3.1: What’s new and how to migrate - Thymeleaf

Thymeleaf 3.1: What’s new and how to migrate Latest version is Thymeleaf 3.1.3.RELEASE.What’s newSupport for Servlet API 5.0 and the jakarta.* class namespaceThymeleaf 3.1 adds support for the new jakarta.* class namespace in the Servlet API since vers

www.thymeleaf.org

 

As explained in the “What’s new” section, expression utility objects #request, #response, #session and  #servletContext are no longer available from expressions in templates.

 

โ€ผ๏ธ๋ฌธ์ œ์ 

๊ธฐ์กด์—๋Š” Spring Boot์—์„œ Thymeleaf๋ฅผ ์‚ฌ์šฉํ•ด ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ฐœ๋ฐœํ•˜๋Š” ๊ฒฝ์šฐ, ์ด์ „ ๋ฒ„์ „์—์„œ๋Š” #httpServletRequest๋ฅผ ํ†ตํ•ด ํ˜„์žฌ ์š”์ฒญ ์ •๋ณด๋ฅผ ์‰ฝ๊ฒŒ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

<!-- Thymeleaf 2.x์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ -->
<p th:text="${#httpServletRequest.requestURI}"></p>

๊ทธ๋Ÿฌ๋‚˜ Thymeleaf 3.x๋ถ€ํ„ฐ #httpServletRequest๊ฐ€ ์ œ๊ฑฐ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์—, ์ด ์ฝ”๋“œ๋Š” ๋” ์ด์ƒ ๋™์ž‘ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

#httpServletRequest.requestURI์˜ ๊ฒฝ์šฐ ๊ณตํ†ต html์—์„œ ์‚ฌ์šฉ ์ค‘์ด๋ฏ€๋กœ #httpServletRequest๊ฐ€ ๋”์ด์ƒ ์ง€์›๋˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๋ฉด Controller์— modelAttribute์— ํ•ด๋‹น ๊ฐ’์„ ๋„ฃ์–ด์ค˜์•ผ๋˜๋Š” ์ƒํ™ฉ์ด ๋ฐœ์ƒํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค.

 

(๋ฌผ๋ก  ์ด์™ธ์—๋„ ๋ณ€๊ฒฝ์‚ฌํ•ญ ๋ฐ ๋ฏธ์ง€์› ๊ธฐ๋Šฅ์ด ์กด์žฌํ–ˆ์ง€๋งŒ httpServletRequest ๊ธฐ๋Šฅ ๋ฏธ์ง€์›์— ๋Œ€ํ•œ ๋ฌธ์ œ์ ์— ๋Œ€ํ•œ ๋ถ€๋ถ„๋งŒ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค.)

๐Ÿค” ๊ณ ๋ฏผ

๋ชจ๋“  Controller์— modelAttribute ๊ฐ’์„ ์ถ”๊ฐ€ํ•ด์ฃผ๋Š” ๋…ธ๊ฐ€๋‹ค๋ฅผ ํ•˜๋Š” ๊ฒƒ์ด ๋งž์„๊นŒ?

์ด ๊ฒƒ์€ Controller๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์•„ ๋…ธ๊ฐ€๋‹ค๋ฅผ ํ•ด์ฃผ๋Š” ๊ฒƒ์ด ๋ฐ”๋žŒ์งํ•˜์ง€๋Š” ์•Š๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ „์—ญ ์„ค์ •ํ•ด์ค„ ์ˆ˜ ์žˆ๋Š” 2๊ฐ€์ง€ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์ƒ๊ฐํ•ด๋ดค์Šต๋‹ˆ๋‹ค.

 

์ƒ๊ฐ 1 : @ControllerAdvice ์‚ฌ์šฉํ•œ ์ „์—ญ ์„ค์ •

@ControllerAdvice
public class ControllerHandler{

	@ModelAttribute
  public void handleRequest(HttpServletRequest request, Model model) {
      String requestURI = request.getRequestURI();
      // requestURI ๊ฐ’ ๊ณตํ†ต์ ์œผ๋กœ model์— add
      model.addAttribute("requestURI", requestURI);
  }
}

 

์ƒ๊ฐ 2 : Interceptor๋ฅผ ํ™œ์šฉํ•œ ์ „์—ญ ์„ค์ •

- global Interceptor ์ƒ์„ฑ

@Component
public class GlobalRequestAttributeInterceptor implements HandlerInterceptor {
    
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
        if (modelAndView != null) {
            String requestUri = request.getRequestURI();
            modelAndView.addObject("requestUri", requestUri);
        }
    }
}

 

- interceptor ๋“ฑ๋ก

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private GlobalRequestAttributeInterceptor globalRequestAttributeInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(globalRequestAttributeInterceptor);
    }
}

 

๐Ÿ”ฅ ๊ฒฐ๋ก : ์–ด๋Š ๋ฐฉ๋ฒ•์ด ๋” ์ข‹์„๊นŒ?

๋น„๊ต ํ•ญ๋ชฉ @ControllerAdvice (@ModelAttribute) Interceptor (HandlerInterceptor)
์ ์šฉ ๋Œ€์ƒ Spring MVC (Thymeleaf ๋ทฐ) ๋ชจ๋“  ์š”์ฒญ (Thymeleaf, REST API ๋“ฑ)
์ ์šฉ ๋ฒ”์œ„ Model์ด ์žˆ๋Š” ๋ชจ๋“  ์š”์ฒญ postHandle ์ดํ›„ ModelAndView ์‘๋‹ต์ด ์žˆ๋Š” ์š”์ฒญ
์˜ˆ์™ธ ์ฒ˜๋ฆฌ @ExceptionHandler์—์„œ ๋ณ„๋„ ์ถ”๊ฐ€ ํ•„์š” ์˜ˆ์™ธ ๋ฐœ์ƒ ์‹œ ํ˜ธ์ถœ๋˜์ง€ ์•Š์Œ
๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ์‹œ์  ์ปจํŠธ๋กค๋Ÿฌ ์‹คํ–‰ ์ „์— Model์— ์ถ”๊ฐ€ ์ปจํŠธ๋กค๋Ÿฌ ์‹คํ–‰ ํ›„ ModelAndView์— ์ถ”๊ฐ€
REST API ์ ์šฉ ๊ฐ€๋Šฅ ์—ฌ๋ถ€ โŒ (๋ถˆ๊ฐ€๋Šฅ) โœ… (๊ฐ€๋Šฅ)

 

์šฐ์„  Interceptor์˜ ๊ฒฝ์šฐ ๋ชจ๋“  ์š”์ฒญ(Thymeleaf + REST API)์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ ๋ชจ๋“  ์š”์ฒญ์— ๋Œ€ํ•ด์„œ ์ฒ˜๋ฆฌ ํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด ๊ฒฝ์šฐ์—๋Š” Thymeleaf์—์„œ๋งŒ ์ฒ˜๋ฆฌํ•ด์ฃผ๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์— Interceptor๊นŒ์ง€๋Š” ํ•„์š”ํ•˜์ง€ ์•Š๋‹ค๊ณ  ์ƒ๊ฐํ•˜์˜€์Šต๋‹ˆ๋‹ค.

Thymeleaf ์œ„ํ•œ ์ฒ˜๋ฆฌ ๋ฉด @ControllerAdvice + @ModelAttribute ํ™œ์šฉํ•œ ์ฒ˜๋ฆฌ ๋ฐฉ์‹์ด ๋” ์˜ณ๋‹ค๊ณ  ์ƒ๊ฐํ•ด ํ•ด๋‹น ๋ฐฉ์‹์„ ์ ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค ๐Ÿš€ 

๋ฐ˜์‘ํ˜•
profile

Yeji's Tech Notes

@Jop

ํฌ์ŠคํŒ…์ด ์ข‹์•˜๋‹ค๋ฉด "์ข‹์•„์š”โค๏ธ" ๋˜๋Š” "๊ตฌ๋…๐Ÿ‘๐Ÿป" ํ•ด์ฃผ์„ธ์š”!