Spring MVC:使用属性从 POST 重定向到 GET

发布于 2025-01-15 08:09:00 字数 891 浏览 0 评论 0原文

我对 Spring MVC 还很陌生。我想将模型属性从 POST 方法传递到 GET 方法。我怎么能这样做呢?

这是我的 POST 服务:

@PostMapping("reset-game")
public ModelAndView resetGame(@RequestBody String body, Model model) {
    
    String[] args = body.split("&");
    
    String mode = "";
    
    for (String arg : args) {
        if ("mode".equals(arg.split("=")[0])) {
            mode = arg.split("=")[1];
            break;
        }
    }
    
    Game resettedGame = gameService.resetGame(mode);
    
    model.addAttribute("mode", mode);
    model.addAttribute("boardSize", resettedGame.getBoardSize());
    model.addAttribute("moves", getJSONMoves(resettedGame.getMoves()).toString());
    
    return new ModelAndView("redirect:/board");
}

这是我的 GET 服务,POST 方法中定义的属性不会传递给 GET 方法。有人可以帮助我吗? =)

@GetMapping("/board")
public String board() {
    return "board";
}

I am quite new with Spring MVC. I would like to pass the model attributes from my POST method to the GET method. How could I do this ?

This is my POST service:

@PostMapping("reset-game")
public ModelAndView resetGame(@RequestBody String body, Model model) {
    
    String[] args = body.split("&");
    
    String mode = "";
    
    for (String arg : args) {
        if ("mode".equals(arg.split("=")[0])) {
            mode = arg.split("=")[1];
            break;
        }
    }
    
    Game resettedGame = gameService.resetGame(mode);
    
    model.addAttribute("mode", mode);
    model.addAttribute("boardSize", resettedGame.getBoardSize());
    model.addAttribute("moves", getJSONMoves(resettedGame.getMoves()).toString());
    
    return new ModelAndView("redirect:/board");
}

And this is my GET service, the attributes defined in POST method are not passed to the GET method. Can anybody help me ? =)

@GetMapping("/board")
public String board() {
    return "board";
}

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

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

发布评论

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

评论(1

她如夕阳 2025-01-22 08:09:00

使用 RedirectAttributes 来实现这一点。

@PostMapping("reset-game")
public ModelAndView resetGame(@RequestBody String body, RedirectAttributes redirectedAttributes) {
    
    String[] args = body.split("&");
    
    String mode = "";
    
    for (String arg : args) {
        if ("mode".equals(arg.split("=")[0])) {
            mode = arg.split("=")[1];
            break;
        }
    }
    
    Game resettedGame = gameService.resetGame(mode);
    
    redirectedAttributes.addFlashAttribute("mode", mode);
    redirectedAttributes.addFlashAttribute("boardSize", resettedGame.getBoardSize());
    redirectedAttributes.addFlashAttribute("moves", getJSONMoves(resettedGame.getMoves()).toString());
    
    return new ModelAndView("redirect:/board");
}

从文档来看,flash 属性执行以下操作。

重定向后,flash 属性会自动添加到
为目标 URL 提供服务的控制器的模型。

get 方法中,单独或通过模型使用参数。

假设 boardSize 为整数且绝不为空;

@GetMapping("/board")
public String board(@RequestParam String mode, @RequestParam int boardSize, @RequestParam String moves) {

    return "board";
}

Use RedirectAttributes to achieve this.

@PostMapping("reset-game")
public ModelAndView resetGame(@RequestBody String body, RedirectAttributes redirectedAttributes) {
    
    String[] args = body.split("&");
    
    String mode = "";
    
    for (String arg : args) {
        if ("mode".equals(arg.split("=")[0])) {
            mode = arg.split("=")[1];
            break;
        }
    }
    
    Game resettedGame = gameService.resetGame(mode);
    
    redirectedAttributes.addFlashAttribute("mode", mode);
    redirectedAttributes.addFlashAttribute("boardSize", resettedGame.getBoardSize());
    redirectedAttributes.addFlashAttribute("moves", getJSONMoves(resettedGame.getMoves()).toString());
    
    return new ModelAndView("redirect:/board");
}

From the docs, flash attributes does the following.

After the redirect, flash attributes are automatically added to the
model of the controller that serves the target URL.

On the get method, consume the parameters individually or via a model.

Assuming boardSize is integer and never null;

@GetMapping("/board")
public String board(@RequestParam String mode, @RequestParam int boardSize, @RequestParam String moves) {

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