반응형
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
반응형
'programing' 카테고리의 다른 글
| 사용자 정의 HTML5 필수 필드 검증 메시지 설정 (0) | 2023.09.09 |
|---|---|
| Oracle에서 여러 문자열을 함께 바꾸는 방법 (0) | 2023.09.09 |
| MariaDB datetime 자동 업데이트 버그 (0) | 2023.09.04 |
| 무엇을 합니까?PowerShell에서 평균? (0) | 2023.09.04 |
| YY-MM에 의해 주문된 영업 담당자, 영업 담당자의 상태 및 해당 영업 담당자의 상태에 대한 총 매출이 포함된 SQL 보고서 (0) | 2023.09.04 |