jayjaypg22
jayjaypg22

Reputation: 1751

transform Post request using the Post-Redirect-Get pattern in java spring

I would like to transform a post request using the post/redirect/get pattern in order to prevent an "ambiguous handler methods mapped for HTTP path" error. See This question for details.

Here is the initial code :

@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;
    }

I don't really understand how to re code it using the PRG pattern. Even if I think that I understand the underlying concept.

Upvotes: 6

Views: 14074

Answers (1)

perbellinio
perbellinio

Reputation: 682

You need to add another method that can handle a GET request for the same url mapping. So in your POST method you just make a redirection and in your GET method you can do all the business process.

@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;
    }

Upvotes: 7

Related Questions