首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在java spring中使用Post-Redirect-Get模式转换Post请求

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

Stack Overflow用户
提问于 2016-05-25 17:52:15
回答 1查看 9.7K关注 0票数 4

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

以下是初始代码:

代码语言:javascript
复制
@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模式对其进行重新编码。即使我认为我理解了潜在的概念。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-25 18:09:11

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

代码语言:javascript
复制
@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;
    }
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37433564

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档