πŸ’» 곡뢀 기둝/πŸƒ Spring

Spring | RedirectAttributes

  • -

redirect μ‹œ 데이터 전달 방법

  • RedirectAttributes 클래슀λ₯Ό μ‚¬μš©ν•˜μ—¬ 전달
  • RedirectAttributes ν΄λž˜μŠ€λŠ” Spring 3.1 버전에 좔가됨
  • RedirectAttributes 클래슀λ₯Ό 톡해 String ν˜•νƒœλ₯Ό ν¬ν•¨ν•œ map, list, vo λ“±μ˜ Object ν˜•νƒœλ‘œ 전달 κ°€λŠ₯

 

λ™μž‘ μ‹€μŠ΅ | addAttribute

@Controller
public class TestAttributes {
	@RequestMapping(value="/aaa")
	public String insertA(ModelMap model, RedirectAttributes rttr) throws Exception{

	    String message ="λ“±λ‘λ˜μ—ˆμŠ΅λ‹ˆλ‹€.";
	    rttr.addAttribute("message", message);
	    
	    return "redirect:/abc";
	}

}

 

message 에 담은 객체λ₯Ό redirect λ₯Ό ν†΅ν•΄μ„œ λ³΄λ‚΄κ³ μž ν•˜λŠ” 경둜둜 데이터λ₯Ό λ³΄λƒˆλ‹€.

/abc?message=λ“±λ‘λ˜μ—ˆμŠ΅λ‹ˆλ‹€.

 

μ‹œν–‰μ°©μ˜€ | method = RequestMethod.POST
redirect μ‹œ  RequestMethod.POST 둜 λ³΄λ‚΄κ²Œ 될 경우 (postman μœΌλ‘œλŠ” 확인 κ°€λŠ₯)
// WARN : org.springframework.web.servlet.PageNotFound - Request method 'GET' not supported
RequestMethod.GET 을 μΆ”κ°€ν•˜κ±°λ‚˜ μ½”λ“œμ™€ 같이 κΈ°λ³Έκ°’μœΌλ‘œ μ‚¬μš©ν•œλ‹€.

 

λ™μž‘ μ‹€μŠ΅ | addFlashAttribute

κ²Œμ‹œκΈ€μ˜ 등둝/μˆ˜μ •/μ‚­μ œ λ“± λ³΄μ•ˆμ— λ―Όκ°ν•œ κΈ°λŠ₯에 많이 μ‚¬μš©λ¨
νœ˜λ°œμ„± 데이터(μƒˆλ‘œκ³ μΉ¨ μ‹œ 데이터 사라짐)
@Controller
public class TestAttributes {
	@RequestMapping(value="/aaa")
	public String insertA(ModelMap model, RedirectAttributes rttr) throws Exception{

	    String message ="λ“±λ‘λ˜μ—ˆμŠ΅λ‹ˆλ‹€.";
	    rttr.addFlashAttribute("message", message);
	    
	    return "redirect:/bbb";
	}
}

 

addAttribute λ₯Ό μ‚¬μš©ν–ˆμ„ λ•Œ GET 으둜 받은 객체 λ°μ΄ν„°λŠ” URL 에 κ³ μŠ€λž€νžˆ λ‚¨μ•„μžˆλ˜ λ°˜λ©΄μ—

addFlashAttribute λ₯Ό μ‚¬μš©ν–ˆμ„ λ•Œ POST 둜 객체λ₯Ό μ „λ‹¬ν•˜μ—¬ URL μ—λŠ” λ…ΈμΆœλ˜μ§€ μ•ŠλŠ”λ‹€.

post url ν˜•νƒœ

 

κ·Έλ ‡λ‹€λ©΄ 객체λ₯Ό 담은 λ°μ΄ν„°λŠ” μ–΄λ–»κ²Œ 확인할 수 μžˆλŠ”κ°€?

 

(1) | ModelAttribute

" @ModelAttribute("message") String text "
addAttribute/addFlashAttribute 으둜 객체λ₯Ό 보내고, νŒŒλΌλ―Έν„° Key λ₯Ό λ°›μ•„ νƒ€μž…μ— 맞게 객체에 λ‹΄μ•„μ„œ μ‚¬μš©ν•œλ‹€.
@Controller
public class TestAttributes {
	@RequestMapping(value="/bbb")
	protected void insertB(@ModelAttribute("message") String text, ModelMap model,
			RedirectAttributes rttr, HttpServletRequest request, HttpServletResponse response)
         	 	  throws Exception{
		
		// ν•œκΈ€ 인코딩
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		// ν™”λ©΄ 좜λ ₯ μ½”λ“œ
		PrintWriter out = response.getWriter();
		String textResult = text; // rttr 객체 데이터
		
		out.println("<html><head><title>μ•ˆλ…•ν•˜μ„Έμš”</title></head><body>");
		out.println("<h1>Hello</h1>");
		out.println("<h1>"+ textResult + "</h1>");
		out.println("</body><html>");
		
	}
}

 

(2) | RequestContextUtils.getInputFlashMap(request)

RequestContextUtils.getInputFlashMap(request)
좜λ ₯(print)
// FlashMap [attributes={message=λ“±λ‘λ˜μ—ˆμŠ΅λ‹ˆλ‹€.}, targetRequestPath=/ch2/bbb, targetRequestParams={}]
Map<String, ?> flashMap = RequestContextUtils.getInputFlashMap(request);

 

(3) | JSP

 ${ key }
객체 데이터λ₯Ό λ°›κ³ μž ν•˜λŠ” ' .jsp ' 에 ${ key } λ₯Ό μž…λ ₯ν•˜λ©΄, addAttribute/addFlashAttribute 으둜 보낸 객체 데이터λ₯Ό 받을 수 μžˆλ‹€.
${ message }

 

 

 

좜처 | https://web-obj.tistory.com/455

좜처 | https://galid1.tistory.com/563

좜처 | https://m.blog.naver.com/allkanet72/220964699929

'πŸ’» 곡뢀 기둝 > πŸƒ Spring' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

Spring | File Upload  (0) 2023.02.06
Spring | Exception  (0) 2023.01.19
Spring | REST API μˆ™μ œ  (0) 2023.01.16
Spring MVC | Web Application  (0) 2023.01.13
Spring Boot/VSC | New Project  (0) 2023.01.10
Contents

ν¬μŠ€νŒ… μ£Όμ†Œλ₯Ό λ³΅μ‚¬ν–ˆμŠ΅λ‹ˆλ‹€

이 글이 도움이 λ˜μ—ˆλ‹€λ©΄ 곡감 λΆ€νƒλ“œλ¦½λ‹ˆλ‹€.