RedirectAttributes

김덕근·2023년 4월 25일
0

Spring

목록 보기
5/19

RedirectAttributes ra

ra.addFlashAttribute("message", "아이디 또는 비밀번호가 일치하지 않습니다.");

redirect 시에도 request scope로 세팅된 데이터가 유지될 수 있도록 하는 방법을
Spring에서 제공해줌
-> RedirectAttributes 객체(컨트롤러 매개변수에 작성하면 사용 가능)


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<!-- 
	* http://localhost:8080/comm
	* http://localhost:8080/comm/main 주소로 요청 위임
	-> forward 이기 때문에 출력되는 주소는 http://localhost:8080/comm 유지
 -->
 
 <jsp:forward page="main"/>

package edu.kh.comm.main.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class MainController {

	
	@RequestMapping("/main")
	public String mainForward() {
		
		return "common/main";
	}
	
}

index.jsp

    <!-- footer include -->
    <jsp:include page="/WEB-INF/views/common/footer.jsp" />

footer.jsp

<%-- request에 message 속성이 존재하는 경우 alert창으로 해당 내용을 출력 --%>
<c:if test="${ !empty message }">
    <script>
        alert("${message}");
        // EL 작성 시 scope를 지정하지 않으면
        // page -> request -> session -> application 순서로 검색하여
        // 일치하는 속성이 있으면 출력
    </script>
</c:if> 
profile
안녕하세요!

0개의 댓글