【问题标题】:How to send data to a redirected URL spring mvc如何将数据发送到重定向的 URL spring mvc
【发布时间】:2013-06-21 10:50:59
【问题描述】:

我有一个 URL,它在调用时会重定向到另一个 URL。我想在重定向的同时传递一些数据。

例如我有这个方法:

@RequestMapping("efetuaEnvioEmail")
    public String efetuaEnvioEmail(HttpServletResponse response) throws IOException {
        System.out.println("efetuaEnvioEmail");
        return "redirect:inicio";
    }

重定向到这个:

@RequestMapping("inicio")
    public String Inicio(HttpServletRequest request) throws IOException {

        return "Inicio";
    }

我想传递一些数据,告知第一种方法一切正常。

我尝试了一些 HttpServletRequest 和 HttpServletResponse 的方法,但我什么都没有。

【问题讨论】:

    标签: servlets redirect spring-mvc request response


    【解决方案1】:

    使用RedirectAttributes 在处理程序方法之间传递任何数据:

    @RequestMapping("efetuaEnvioEmail")
    public String efetuaEnvioEmail(RedirectAttributes rattrs) {
        rattrs.addAttribute("string", "this will be converted into string, if not already");
        rattrs.addFlashAttribute("pojo", "this can be POJO as it will be stored on session during the redirect");
        return "redirect:inicio";
    }
    
    @RequestMapping("inicio")
    public String Inicio(@ModelAttribute("pojo") String pojo) {
        System.out.println(pojo);
        return "Inicio";
    }
    

    【讨论】:

    • 嘿,谢谢你的回答!不过我遇到了一些问题。我正在使用 spring mvc 3.0.5,它似乎在这个版本中没有 RedirectAttributes。我已经下载了 spring-webmvc-3.1.0.RELEASE.jar,它有 RedirectAttributes 类。但我有一个 java .lang.IllegalArgumentException:参数类型不匹配
    • 在我的情况下,我只想知道 inicio 是由 efetuaEnvioEmail 调用的.. 而不是任何其他 URL 重定向.. 所以也欢迎任何其他解决方案 :)
    • @FelipeMosso 这可能对你有帮助 - viralpatel.net/blogs/spring-mvc-flash-attribute-example
    • @FelipeMosso 我强烈建议您升级到 Spring 3.2+ 并解决您会发现的任何问题(例如,通过在 SO 上提问)。 3.0 和 3.1 版本不如 3.2 成熟和“功能完整”。
    • 顺便说一句。应该可以像这样添加查询参数:return "redirect:inicio?email=foo";
    猜你喜欢
    • 2014-06-20
    • 2016-09-21
    • 2016-02-03
    • 2017-12-30
    • 1970-01-01
    • 2012-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多