티스토리 뷰

참조:

http://ysssb.tistory.com/entry/forward%EC%99%80-redirect%EC%9D%98-%EC%B0%A8%EC%9D%B4




redirect와 forward의 차이


* Forward : 

Web Container 차원에서 페이지 이동만 있다. 실제로 웹 브라우저는 다른 페이지로 이동했음을 알 수 없다. 그렇기 때문에, 웹 브라우저에는 최초에 호출한 URL이 표시되고 이동한 페이지의 URL 정보는 볼 수 없다. 동일한  웹 컨테이너에 있는 페이지로만 이동할 수 있다.

현재 실행중인 페이지와 forwad에 의해 호출될 페이지는 request와 response객체를 공유한다.


* Redirect : 

Web Container는 Redirect 명령이 들어오면 웹 브라우저에게 다른 페이지로 이동하라고 명령을 내린다. 그러면 웹 브라우저는 URL을 지시된 주소로 바꾸고 그 주소로 이동한다. 다른 웹 컨테이너에있는 주소로 이동이 가능하다.

새로운 페이지에서는 request와 response객체가 새롭게 생성된다.


forward 방식은 다음 이동할 URL로 요청정보를 그대로 전달한다. 말 그대로 forward(건네주기) 하는 것이다. 그렇기 때문에 사용자가 최초로 요청한 요청정보는 다음 URL에서도 유효하다.


반면 redirect의 경우 최초 요청을 받은 URL1 에서 클라이언트에 redirect할 URL2을 리턴하고, 클라이언트에서는 전혀 새로운 요청을 생성하여 URL2에 다시 요청을 보낸다. 따라서 처음 보냈던 최초의 요청정보는 더 이상 유효하지 않다.




최초 요청받은 컨트롤러 메소드

@RequestMapping(value="/asset/saveManagement.do", method=RequestMethod.POST)
public String saveManagement_View(HttpServletRequest req) {

// Parameter
String user_id = FuncLib.removeNULL(req.getParameter("user_id"));
String user_name = FuncLib.removeNULL(req.getParameter("user_name"));

Params params = new Params();
if (!user_id.isEmpty()) params.put("user_id", user_id);
if (!user_name.isEmpty()) params.put("user_name", user_name);

Boolean isSuccess = Constant.FALSE;
int rtn = managementService.saveManagement(params);
if (rtn > 0) {
isSuccess = Constant.TRUE;
} else {
isSuccess = Constant.FALSE;
}

// foward parameter
req.setAttribute("forward_yn","y");
return "forward:/asset/recManagement.do";
}



요청 처리 후 forward 된 메소드

@RequestMapping(value="/asset/recManagement.do")
public String recManagement_View(HttpServletRequest req, Model model) {

// Parameter
int rowPerPage = FuncLib.strToInt(req.getParameter("rowPerPage"), 15); // 페이지당 row 수
int pageNum = FuncLib.strToInt(req.getParameter("pageNum"), 1); // 페이지 번호
String change_section = FuncLib.removeNULL(req.getParameter("change_section"));
String sdate = FuncLib.removeNULL(req.getParameter("sdate"));
String edate = FuncLib.removeNULL(req.getParameter("edate"));

Params params = new Params();
if (!change_section.isEmpty()) params.put("change_section", change_section);
if (!sdate.isEmpty()) params.put("sdate", sdate);
if (!edate.isEmpty()) params.put("edate", edate);

// forward parameter
if (req.getAttribute("forward_yn") != null) {
String user_id = FuncLib.removeNULL(req.getParameter("user_id"));
String user_name = FuncLib.removeNULL(req.getParameter("user_name"));
}

model.addAttribute("changeManageList", recManagementService.select_recManagementList(params));
model.addAttribute("pagingInfo", pagingInfo);
model.addAttribute("params", params);

return "/asset/recManagement";
}


위와 같이 최초 요청에서 전달된 파라미터인 user_id, user_name은 물론이고 추가로 setAttribute를 통해 파라미터를 함께 forward 할 수 있다.





redirect의 경우

참조:

http://noveloper.github.io/blog/spring/2015/02/16/how-transport-parateter-when-redirect.html

@RequestMapping(value="/asset/saveChangeManage.do", method=RequestMethod.POST)
public String saveChangeManage_View(MultipartHttpServletRequest mReq, RedirectAttributes redirectAttributes) {

// Parameter
int idx = FuncLib.strToInt(mReq.getParameter("idx"));
String change_section = FuncLib.removeNULL(mReq.getParameter("sel_change_section"));
float change_quantity = FuncLib.strToFloat(mReq.getParameter("change_quantity"));
String standard_date = FuncLib.removeNULL(mReq.getParameter("standard_date"));
String dealings_section = FuncLib.removeNULL(mReq.getParameter("dealings_section"));
float sale_price = FuncLib.strToFloat(mReq.getParameter("sale_price"));
// attach
String gubun = FuncLib.removeNULL(mReq.getParameter("gubun")); // 구분

Params params = new Params();
if (idx > 0) params.put("idx", idx);
if (!change_section.isEmpty()) params.put("change_section", change_section);
if (change_quantity >= 0) params.put("change_quantity", change_quantity);
if (!standard_date.isEmpty()) params.put("standard_date", standard_date);
if (!dealings_section.isEmpty()) params.put("dealings_section", dealings_section);
if (sale_price >= 0) params.put("sale_price", sale_price);

Boolean isSuccess = Constant.FALSE;

int rtnIdx = changeManageService.saveChangeManage(params);
if (rtnIdx > 0) {
isSuccess = Constant.TRUE;
} else {
isSuccess = Constant.FALSE;
}

if (isSuccess){
try {
boardAttchService.insert(mReq, gubun, rtnIdx);
} catch (RollbackException e) {
e.printStackTrace();
}
}

// redirect parameter
redirectAttributes.addAttribute("change_section", FuncLib.removeNULL(mReq.getParameter("search_change_section")));
redirectAttributes.addAttribute("sdate", FuncLib.removeNULL(mReq.getParameter("search_sdate")));
redirectAttributes.addAttribute("edate", FuncLib.removeNULL(mReq.getParameter("search_edate")));
return "redirect:/asset/recFluctuateManagementList.do";
}


@RequestMapping(value="/asset/recFluctuateManagementList.do")
public String recFluctuateManagementList_View(HttpServletRequest req, Model model) {
// Parameter
int rowPerPage = FuncLib.strToInt(req.getParameter("rowPerPage"), 15); // 페이지당 row 수
int pageNum = FuncLib.strToInt(req.getParameter("pageNum"), 1); // 페이지 번호
String change_section = FuncLib.removeNULL(req.getParameter("change_section"));
String sdate = FuncLib.removeNULL(req.getParameter("sdate"));
String edate = FuncLib.removeNULL(req.getParameter("edate"));

Params params = new Params();
if (!change_section.isEmpty()) params.put("change_section", change_section);
if (!sdate.isEmpty()) params.put("sdate", sdate);
if (!edate.isEmpty()) params.put("edate", edate);

// 페이징 정보
PagingInfo pagingInfo = new PagingInfo(pageNum, rowPerPage);
model.addAttribute("changeManageList", changeManageService.select_ChangeManageList(params, pagingInfo));
model.addAttribute("pagingInfo", pagingInfo);
model.addAttribute("params", params);
return "/asset/recFluctuateManagementList";
}






댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함