【问题标题】:Redirect to external url with post attributes using spring MVC使用spring MVC重定向到带有post属性的外部url
【发布时间】:2020-07-23 22:58:46
【问题描述】:

重定向到外部 url 后,我在获取属性时遇到了问题。 重定向到外部 url 时,我将属性值设为 null,就像我将这些方法放在同一个 webapp 中一样。

请提出我做错了什么,或者是否有任何方法,以便我可以将属性传递给重定向的 url 并使用它。

我不希望属性显示为 Queryparams。

示例代码如下。

//app1中的控制器

@PostMapping("/redirect")
public RedirectView next(RedirectAttributes redirectAttributes, Model model, HttpServletResponse response, HttpServletRequest request) {
    request.setAttribute("payment",new Payment("1","2","3"));
    redirectAttributes.addAttribute("test", "test");

    redirectAttributes.addFlashAttribute("message", "Successfully changed..");
    redirectAttributes.addFlashAttribute("payment1",new Payment("1","2","3"));

    request.setAttribute(
              View.RESPONSE_STATUS_ATTRIBUTE, HttpStatus.TEMPORARY_REDIRECT);
    RedirectView redirectView = new RedirectView("redirect:http://localhost:8085/redirectedPostToPost", true);
    redirectView.addStaticAttribute("payment",new Payment("1","2","3"));
    return redirectView;
}

//app2的控制器。

@PostMapping("/redirectedPostToPost")
public ModelAndView redirectedPostToPost(@ModelAttribute("message") String message,@ModelAttribute("payment") Payment payment,@ModelAttribute("payment1") Payment payment1, RedirectAttributes redirectAttributes,Model model, HttpServletResponse httpServletResponse, HttpServletRequest request, ModelAndView modelview) {
    logger.info("in redirectedPostToPost:{} : modelview:{}", model, modelview.getModel());
    Map<String, ?> inputFlashMap = RequestContextUtils.getInputFlashMap(request);
    logger.info("flashmap:{}", inputFlashMap);
    return new ModelAndView("about");
}

提前谢谢你们。

【问题讨论】:

    标签: spring-boot redirect response.redirect post-redirect-get


    【解决方案1】:

    HTTP 仅支持 GET 方法重定向。这是详细信息why doesn't HTTP have POST redirect

    有一种解决方法,您将数据发送到视图并在视图中创建表单,您应该使用所有隐藏字段来提交数据。这将是一个白页。您可以使用 javascript 自动提交表单重定向 URL。

    【讨论】:

    • 如果我这样做了,如果在自动提交表单时它重定向到另一个页面并且点击浏览器的后退按钮后退一步(上一个 url)它会再次重定向会发生什么?如果我这样做会不会出现这个问题?
    • 是的,如果用户点击后退按钮会再次重定向
    • 这里是另一种可能的解决方案,使用 HttpClient stackoverflow.com/questions/3324717/…在内部提交
    猜你喜欢
    • 1970-01-01
    • 2013-08-06
    • 2019-05-02
    • 1970-01-01
    • 2017-03-03
    • 2012-04-06
    • 2010-11-23
    • 1970-01-01
    • 2013-07-31
    相关资源
    最近更新 更多