Spring MVC:使用属性从 POST 重定向到 GET
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
RedirectAttributes
来实现这一点。从文档来看,flash 属性执行以下操作。
在
get
方法中,单独或通过模型使用参数。假设 boardSize 为整数且绝不为空;
Use
RedirectAttributes
to achieve this.From the docs, flash attributes does the following.
On the
get
method, consume the parameters individually or via a model.Assuming boardSize is integer and never null;