카테고리 없음

String Redirect

Handy Smurf 2020. 8. 26. 10:07
	@RequestMapping("/l")
	public String methodl(Model model, RedirectAttributes rttr) {
		log.info("l method");
		model.addAttribute("myAttr1", "myValue1"); // 안 됨
		rttr.addFlashAttribute("myRedirectAttr1", "myRedirectValue1"); // 가능 
		return "redirect:/ret/m";
//      return "redirect:m";
	}

redirect 준비

	@RequestMapping("/l")
	public String methodl() {
		log.info("l method");

		return "/ret/l";
	}

	@RequestMapping("/m")
	public String methodm() {
		log.info("m method");

		return "/ret/m";
	}

redirect 

 

jsp 이전 코드

String contextPath = request.getContextPath();

response.sendRedirect(contextPath + "/ret/m");

	@RequestMapping("/l")
	public String methodl() {
		log.info("l method");

		return "redirect:/ret/m";
	}

http://localhost:1234/ret/l

 

http://localhost:1234/ret/m으로 변경된 걸 확인 할 수 있다.

 

INFO : org.zerock.controller.ReturnController - l method
INFO : org.zerock.controller.ReturnController - m method

l>m

 

 

 

	@RequestMapping("/l")
	public String methodl(Model model, RedirectAttributes rttr) {
		log.info("l method");
		model.addAttribute("myAttr1", "myValue1");
		rttr.addFlashAttribute("myRedirectAttr1", "myRedirectValue1");
		return "redirect:/ret/m";
//      return "redirect:m";
	}

	@RequestMapping("/m")
	public String methodm(Model model) {
		log.info("m method");
	//	model.addAttribute("myAttr1", "myValue1");
		return "/ret/m";
	}

}
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>


<%@ page import="java.sql.*" %>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<script src="https://kit.fontawesome.com/a076d05399.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>

<title>Insert title here</title>
</head>
<body>

<h1>리디렉션 당하는 jsp</h1>


<h1>myAttr1 : ${myAttr1 }</h1>
<h1>myRedirectAttr1 : ${myRedirectAttr1  }</h1>
</body>
</html>

http://localhost:1234/ret/l>http://localhost:1234/ret/m 자동변경

rttr.addFlashAttribute("myRedirectAttr1", "myRedirectValue1"); // session에 잠시 붙고 삭제 된다.
rttr.addAttribute("myRedirectAttr2", "myRedirectValue2"); //parameter로 붙어있다