开发者

how to get the object from a redirected view?

Im currently using spring mvc, java and annotations.

@RequestMapping(value = "/submitTask", method = RequestMethod.POST)
public ModelAndView submitTask(HttpSession session, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<St开发者_开发问答ring, Object>();
        ModelAndView model = new ModelAndView(new RedirectView("home.html"));
        map.put("email", request.getParameter("email"));
        map.put("task",request.getParameter("task"));
        map.put("error", request.getParameter("error"));
        model.addObject("map", map);
        return model;
}

@RequestMapping("/home")
 public ModelAndView home(HttpSession session, HttpServletRequest request) {
    ModelAndView model = new ModelAndView("home");
    model.addObject("map", request.getParameter("map"));
    return model;
 }

I don't seem to get the value of "map" via "request.getParameter("map")" when i redirected my view to home.html. how can i be able to retrieve it. Thanks


I belive that the problem is that the HttpServletRequest request passed to "home" method contains the parameter "map.email", "map.task", "map.error", but not "map".


Using RedirectView makes the browser issue a new request, so the original request is lost.

You need something like a flash scope or conversation scope. I don't know of any implementation of these, but check the google results.

Using spring webflow is a way to handle conversations, but it is too complicated for the simple task.

As a workaround you can use the session, and then clear it immediately (which is essentially what the flash-scope would do)


Change

new RedirectView("home.html")

to

"forward:home.html"

Redirect will drop all data on the side of server unless a session attribute or bean as there will be a new request generated from the side of a client. Forward will transfer everything to a different "method" - request attributes will not be altered, therefore, they will be accessible.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜