Spring 3——如何从转发器发出 GET 请求

3 spring-mvc http-status-code-405

我试图将请求转发到另一个接受 GET 请求的 Spring 控制器,但它告诉我不支持 POST。这是我的第一个控制器方法的相关部分,它确实接受 POST 请求,因为我将它用于登录功能。

@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(@ModelAttribute("administrator") Administrator administrator,
    Model model) {
    // code that's not germane to this problem
    return "forward:waitingBulletins";
}
Run Code Online (Sandbox Code Playgroud)

这是我尝试转发的方法。

@RequestMapping(value = "/waitingBulletins", method = RequestMethod.GET)
public String getWaitingBulletins(Model model) {
        // the actual code follows
    }
Run Code Online (Sandbox Code Playgroud)

这是我的浏览器中的错误消息。

HTTP Status 405 - Request method 'POST' not supported

--------------------------------------------------------------------------------

type Status report

message Request method 'POST' not supported

description The specified HTTP method is not allowed for the requested resource (Request method 'POST' not supported).
Run Code Online (Sandbox Code Playgroud)

sou*_*eck 5

forward保持原始请求完好无损,因此您正在转发请求POST并缺少它的处理程序。

从表面上看,您真正想要实现的是POST-redirect-GET模式,它使用重定向而不是转发。

您只需将POST处理程序更改为:

@RequestMapping(value = "/login", method = RequestMethod.POST) 
public String login(@ModelAttribute("administrator") Administrator administrator,
    Model model) {
    // code that's not germane to this problem
    return "redirect:waitingBulletins";
}
Run Code Online (Sandbox Code Playgroud)

使其发挥作用。