7

假设我有 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 或使用会话是不可取的。

感谢您的回复!

4

1 回答 1

2

您不应将 HTTP GET 重定向到 HTTP POST。HTTP GET 和 HTTP POST 是两个不同的东西。预计它们的行为会非常不同(GET 是安全的、幂等的和可缓存的。POST 是幂等的)。有关更多信息,请参见HTTP GET 和 POST 语义和限制http://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。

于 2013-08-31T09:22:47.157 回答