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

发布于 2024-10-05 04:49:20 字数 913 浏览 6 评论 0原文

我目前使用 spring mvc、java 和注释。

@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”的值。我怎样才能找回它。谢谢

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<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;
 }

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我最亲爱的 2024-10-12 04:49:20

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

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".

紙鸢 2024-10-12 04:49:20

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

您需要诸如闪光范围或对话范围之类的东西。我不知道这些的任何实现,但是 检查 google 结果

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

作为解决方法,您可以使用会话,然后立即清除它(这本质上就是 flash-scope 所做的事情)

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)

夕嗳→ 2024-10-12 04:49:20

更改

new RedirectView("home.html")

"forward:home.html"

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文