programing

Thymeleaf로 Spring Boot에서 플래시 메시지를 설정하는 방법

iphone6s 2023. 9. 4. 19:38
반응형

Thymeleaf로 Spring Boot에서 플래시 메시지를 설정하는 방법

저는 Thymeleaf와 함께 Spring-Boot에 구축된 프로젝트에 Flash-Messages를 구현하려고 합니다.하지만 아직까지는 기본 제공 기능이 아니라는 것을 발견했습니다.이 경우 리디렉션 후 사용자에게 메시지를 표시하는 옵션은 무엇입니까?

링크에서 제안한 솔루션을 구현하려고 하지만 소개에서 설명한 것처럼 Spring-Boot에서 작동하도록 의도되지 않았습니다.

여기에서 설명한 대로 RedirectAttributes 개체를 컨트롤러에 주입한 다음 개체에 대해 setFlashAttribute를 호출합니다.

@GetMapping("/test1")
public String test1(RedirectAttributes redirAttrs){
    redirAttrs.addFlashAttribute("message", "This is message from flash");
    return "redirect:/test2";
}

@GetMapping("/test2")
public String test2(Model model){
    return "test2";
}

이제 "/test2" 모델에서 메시지를 사용할 수 있으므로 test.html에 메시지를 표시합니다.

<h2 th:text="${message}"></h2>

이 코드를 추가하면 다음과 같습니다.

<div class="alert alert-success" role="alert"
                             th:if="${message}">
                            <span th:text="${message}"></span>
                            <button aria-label="Close" class="btn-close" data-bs-dismiss="alert"
                                    type="button"></button>
                        </div>

언급URL : https://stackoverflow.com/questions/43751860/how-to-set-a-flash-message-in-spring-boot-with-thymeleaf

반응형