如何从重定向视图中获取对象?

时间:2010-12-01 10:23:07

标签: java spring jsp spring-mvc annotations

我目前正在使用spring mvc,java和annotations。

@RequestMapping(value = "/submitTask", method = RequestMethod.POST)
public ModelAndView submitTask(HttpSession session, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, 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;
 }

当我将视图重定向到home.html时,我似乎没有通过“request.getParameter(”map“)获取”map“的值。我怎么能够检索它。感谢

3 个答案:

答案 0 :(得分:1)

我相信问题是传递给“home”方法的HttpServletRequest请求包含参数“map.email”,“map.task”,“map.error”,但不包含“map”。

答案 1 :(得分:1)

使用RedirectView会使浏览器发出新请求,因此原始请求将丢失。

您需要类似闪存范围或会话范围的内容。我不知道这些的任何实现,但是check the google results

使用spring webflow是一种处理对话的方法,但对于简单的任务来说太复杂了。

作为一种解决方法,您可以使用会话,然后立即清除它(这实际上就是闪存范围所做的)

答案 2 :(得分:0)

更改

new RedirectView("home.html")

"forward:home.html"

重定向将删除服务器端的所有数据,除非会话属性或bean因为客户端会生成新请求。 转发会将所有内容转移到不同的“方法” - 请求属性不会被更改,因此,它们将是可访问的。