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 λ₯Ό ν΅ν΄μ 보λ΄κ³ μ νλ κ²½λ‘λ‘ λ°μ΄ν°λ₯Ό 보λλ€.
μνμ°©μ€ | 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 μλ λ
ΈμΆλμ§ μλλ€.
κ·Έλ λ€λ©΄ κ°μ²΄λ₯Ό λ΄μ λ°μ΄ν°λ μ΄λ»κ² νμΈν μ μλκ°?
(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