개발/Spring
[Spring] redirect 시 파라미터 또는 객체 넘기는 방법
wwwnghks
2021. 10. 7. 16:22
1.redirect시에 String(파라미터) 넘기는 방법
String으로 넘길경우에는 별도로 처리하지 않아도, jsp에서 해당 파라미터 값 (param1) 을 바로 사용할 수 있다.
@RequestMapping(value="/test1.do", method = RequestMethod.GET)
public String test1(RedirectAttributes redirect,
@RequestParam(value="param1") String param1) throws Exception {
redirect.addFlashAttribute("param1", param1);
return "redirect:/test2.do";
}
@RequestMapping(value="/test2.do", method = RequestMethod.GET)
public String test2() throws Exception {
return "test2";
}
2.redirect시에 Object(객체) 넘기는 방법
Object로 넘길경우에는 아래와 같이 넘기고 받는다. TestVO는 임의로 생성한 객체이다.
@RequestMapping(value="/test1.do", method = RequestMethod.GET)
public String test1(RedirectAttributes redirect,TestVO vo) throws Exception {
redirect.addFlashAttribute("vo", vo);
return "redirect:/test2.do";
}
@RequestMapping(value="/test2.do", method = RequestMethod.GET)
public String test2(HttpServletRequest request,Model model) throws Exception {
Map<String, ?> flashMap =RequestContextUtils.getInputFlashMap(request);
if(flashMap!=null) {
TestVO vo =(TestVO)flashMap.get("vo");
model.addAttribute("vo", vo);
}
return "test2";
}