๐ธ๊ฐ์๐ธ
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 ํ์ฉํ ์ฒ๋ฆฌ ๋ฐฉ์์ด ๋ ์ณ๋ค๊ณ ์๊ฐํด ํด๋น ๋ฐฉ์์ ์ ์ฉํ์์ต๋๋ค ๐