gpt4 book ai didi

java - 使用 java spring 中的 Post-Redirect-Get 模式转换 Post 请求

转载 作者:行者123 更新时间:2023-12-01 14:11:08 25 4
gpt4 key购买 nike

我想使用 post/redirect/get 模式转换 post 请求,以防止“为 HTTP 路径映射的模糊处理程序方法”错误。见 This question详情。

这是初始代码:

@Controller
@RequestMapping("/bus/topologie")
public class TopologieController {
private static final String VIEW_TOPOLOGIE = "topologie";

@RequestMapping(method = RequestMethod.POST, params = { "genererCle" })
public String genererCle(final Topologie topologie, final Model model)
throws IOException {
cadreService.genererCle(topologie);
return VIEW_TOPOLOGIE;
}

我真的不明白如何使用 PRG 模式重新编码。即使我认为我理解基本概念。

最佳答案

您需要添加另一个可以处理相同 url 映射的 GET 请求的方法。
因此,在您的 POST 方法中,您只需进行重定向,而在您的 GET 方法中,您可以完成所有业务流程。

@Controller
@RequestMapping("/bus/topologie")
public class TopologieController {
private static final String VIEW_TOPOLOGIE = "topologie";

@RequestMapping(method = RequestMethod.POST, params = { "genererCle" })
public String genererClePost(final Topologie topologie, final RedirectAttributes redirectAttributes, @RequestParam("genererCle") final String genererCle, final Model model)
throws IOException {
redirectAttributes.addAttribute("genererCle", genererCle);
return "redirect:/bus/topologie";
}

@RequestMapping(method = RequestMethod.GET, params = { "genererCle" })
public String genererCleGet(final Topologie topologie, final Model model)
throws IOException {
cadreService.genererCle(topologie);
return VIEW_TOPOLOGIE;
}

关于java - 使用 java spring 中的 Post-Redirect-Get 模式转换 Post 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37433564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com