티스토리 뷰

Project/WWW

FlashMap["MESSAGE"] 적용

evolutioning 2024. 10. 14. 10:44

FlashMap["MESSAGE"] = "내용"

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/FlashMap.html

 

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", "메시지 내용")

https://docs.spring.io/spring-framework/docs/3.2.15.RELEASE/spring-framework-reference/htmlsingle/#new-in-3.1-flash-redirect-attributes

 

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로 화면에 뿌려주도록 했다.

 

회원정보가 일치하지않을때의 MESSAGE

 

위 화면과 같이 사용할 수 있다

 

**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
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
글 보관함