티스토리 뷰
FlashMap["MESSAGE"] = "내용"
FlashMap (Spring Framework 6.1.13 API)
A FlashMap provides a way for one request to store attributes intended for use in another. This is most commonly needed when redirecting from one URL to another -- e.g. the Post/Redirect/Get pattern. A FlashMap is saved before the redirect (typically in th
docs.spring.io
redirectArrtibures.addFalshAttribute("MESSAGE", "메시지 내용")
Spring Framework Reference Documentation
Authors Rod Johnson, Juergen Hoeller, Keith Donald, Colin Sampaleanu, Rob Harrop, Thomas Risberg, Alef Arendsen, Darren Davison, Dmitriy Kopylenko, Mark Pollack, Thierry Templier, Erwin Vervaet, Portia Tung, Ben Hale, Adrian Colyer, John Lewis, Costin Leau
docs.spring.io
이렇게 두가지를 사용해서
프론트 페이지에서 메시지를 날려보내주는 두개의 방식이있다.
이번 프로젝트에는 FlashMap를 사용하려한다.
spring 3.1 버전부터 지원하는 FlashMap은
import org.springframework.web.servlet.FlashMap
먼저 주소를 표시해주고,
val flashMap = FlashMap()
객체를 생성해준다.
when(exception) {
is BadCredentialsException -> {
flashMap["MESSAGE"] = "아이디와 비밀번호를 확인해주세요"
logger.info("FlashMap MESSAGE: " + flashMap.get("MESSAGE"));
redirectUrl= "/login"
}else -> {
flashMap["MESSAGE"] = "오류가 발생했습니다. 문의해주세요"
redirectUrl= "/login"
}
**아직 포스팅하지 않은 Spring Security의 CustomFailuerHandler에 작성한 코드이다**
해당하는 Exception에 맞다면
FlashMap["MESSAGE"] =" " 를 사용해서 메시지를 뿌려주도록 설정했다.
* FlashMap["MESSAGE"] 이 형식은 소스를 더 간단하게 작성하기 위해 결정했다.*
val flashMapManager = SessionFlashMapManager()
flashMapManager.saveOutputFlashMap(flashMap, request, response)
response.sendRedirect(redirectUrl)
**이부분은 FlashMap이 리다이렉트 이후에도 데이터를 전달하기위해 사용하는 매커니즘이기때문에 작성했다.
sendRedirect()는 이 프로세스를 완성하는데 필수적으로 들어가야하는 소스이다.**
SessionFlashMapManager : FlashMap을 세션에 저장하고 리다이렉트 후에도 데이터를 사용할 수 있도록 관리
SaveOutputFlashMap : 객체에 저장한 데이터를 현재 세션에 저장하는 메서드
SendRedirect : 지정한 url로 리다이렉트
이후 login.html에서 해당 message의 유무를 판단하고 띄워주기 위해 작성한 코드이다.
<html lang="ko" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
...
<script defer th:inline="javascript" type="text/javascript" charset="UTF-8">
/* <![CDATA[ */
'use strict';
const ERROR = /*[[ ${ERROR} ]]*/;
const MESSAGE = /*[[ ${MESSAGE} ]]*/;
if(ERROR !== null){
alert(ERROR);
}else if(MESSAGE !== null){
alert(MESSAGE);
}
/* ]]> */
</script>
<script></script>
에 작성한 소스이고,
받아오는 MESSAGE가 있다면 alert로 화면에 뿌려주도록 했다.
위 화면과 같이 사용할 수 있다
**2024-10-14 10:43:08 EkeprlFailuerHandler - FlashMap MESSAGE: 아이디와 비밀번호를 확인해주세요**
해당 FlashMap 로그
이상.
'Project > WWW' 카테고리의 다른 글
게시판 조회 기능 (1) (0) | 2025.01.16 |
---|---|
Nginx 디폴트(default)페이지 설정 (0) | 2024.12.05 |
로그인 (Login)기능 (1) | 2024.08.30 |
회원가입(Join) 기능 추가 -2 (0) | 2024.08.30 |
회원가입(Join) 기능 추가 -1 (0) | 2024.08.30 |