Java Spring boot redirectAttributes.addFlashAttribute不保留立即重定向中的值

Java Spring boot redirectAttributes.addFlashAttribute不保留立即重定向中的值,java,spring,spring-boot,redirect,Java,Spring,Spring Boot,Redirect,我有一个实现GET-POST-GET重定向模式的控制器类。我尝试在POST请求中使用addFlashAttribute并重定向以获取,但GET中的模型对象不包含设置值。这是我的密码: @Controller @RequestMapping("/eg") public class Example extends AbstractBaseController { @RequestMapping(value = "/account", method = RequestMethod.GET) publ

我有一个实现GET-POST-GET重定向模式的控制器类。我尝试在POST请求中使用addFlashAttribute并重定向以获取,但GET中的模型对象不包含设置值。这是我的密码:

@Controller
@RequestMapping("/eg")
public class Example extends AbstractBaseController {

@RequestMapping(value = "/account", method = RequestMethod.GET)
public String renderFavouriteView(
        HttpServletRequest request,
        HttpServletResponse response,
        ExtendedModelMap modelMap,
        @ModelAttribute("result") String postResult) {


    modelMap.addAttribute("result", postResult); //postResult is empty

    return "account.ftl";
}

@RequestMapping(value = "/account", method = RequestMethod.POST)
public String handleFavouriteView(
        AccountForm accountForm,
        HttpServletRequest request,
        HttpServletResponse response,
        ExtendedModelMap modelMap,
        RedirectAttributes redirectAttributes) {

    ServiceResult serviceResult = myAccountService.createAccount(accountForm);

    if (!serviceResult.isSuccess()) {
        redirectAttributes.addFlashAttribute("result", "Done");
    } else {

        redirectAttributes.addFlashAttribute("result", "Failed");
    }

    Map<String,?> m = redirectAttributes.getFlashAttributes(); // present here.

    return "redirect:/eg/account";
}
@控制器
@请求映射(“/eg”)
公共类示例扩展了AbstractBaseController{
@RequestMapping(value=“/account”,method=RequestMethod.GET)
公共字符串renderFavouriteView(
HttpServletRequest请求,
HttpServletResponse,
扩展模型映射模型映射,
@ModelAttribute(“结果”)字符串(postResult){
modelMap.addAttribute(“result”,postResult);//postResult为空
返回“account.ftl”;
}
@RequestMapping(value=“/account”,method=RequestMethod.POST)
公共字符串handleFavouriteView(
会计表格会计表格,
HttpServletRequest请求,
HttpServletResponse,
扩展模型映射模型映射,
重定向属性(重定向属性){
ServiceResult ServiceResult=myAccountService.CreateCount(accountForm);
如果(!serviceResult.isSuccess()){
redirectAttributes.addFlashAttribute(“结果”、“完成”);
}否则{
redirectAttributes.addFlashAttribute(“结果”,“失败”);
}
Map m=redirectAttributes.getFlashAttributes();//此处显示。
返回“重定向:/eg/account”;
}
}


如果我在这里遗漏了什么,请告诉我。

在PRG情况下使用
重定向属性时,您需要做的只是:

  • 重定向属性
    作为参数包含到POST方法签名中
  • 在POST方法中通过
    addFlashAttribute
    设置所需的属性
  • 在引用flash属性的GET方法签名中包含@modeldattribute注释参数
  • 因此,您确实不需要POST方法中的这一行:

    Map<String,?> m = redirectAttributes.getFlashAttributes(); // present here.
    

    在重定向过程中,Flash属性已经自动添加到模型中,所以上面的行实际上正在覆盖它如果您删除该行,它将按预期工作。

    请帮助他人
    modelMap.addAttribute("result", postResult); //postResult is empty