Spring MVC GET / redirect / POST

时间:2012-12-05 11:51:16

标签: redirect spring-mvc

假设我有2个Spring MVC服务:

@RequestMapping(value = "/firstMethod/{param}", method = RequestMethod.GET)
public String firstMethod(@PathVariable String param) {
    // ...
    // somehow add a POST param
    return "redirect:/secondMethod";
}

@RequestMapping(value = "/secondMethod", method = RequestMethod.POST)
public String secondMethod(@RequestParam String param) {
    // ...
    return "mypage";
}

可以将第一个方法调用重定向到第二个(POST)方法吗? 使用第二种方法作为GET或使用会话是不可取的。

感谢您的回复!

1 个答案:

答案 0 :(得分:1)

您不应将HTTP GET重定向到HTTP POST。 HTTP GET和HTTP POST是两回事。预计它们的行为会有很大不同(GET是安全的,幂等的和可缓存的.POST是幂等的)。有关详情,请参阅HTTP GET and POST semantics and limitationshttp://www.w3schools.com/tags/ref_httpmethods.asp

您可以做的是:使用RequestMethod.GET注释secondMethod。然后你应该能够进行所需的重定向。

@RequestMapping(value = "/secondMethod", method = {RequestMethod.GET, RequestMethod.POST})
public String secondMethod(@RequestParam String param) {
...
}

但请注意,然后可以通过HTTP GET请求调用secondMethod。