【问题标题】:Spring 3 -- how to do a GET request from a forwardSpring 3 - 如何从转发中执行 GET 请求
【发布时间】:2013-04-29 03:14:14
【问题描述】:

我正在尝试将请求转发到另一个接受 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";
}

这是我要转发的方法。

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

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

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).

【问题讨论】:

    标签: spring-mvc http-status-code-405


    【解决方案1】:

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

    让它工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-30
      • 1970-01-01
      • 1970-01-01
      • 2021-08-04
      • 2017-04-14
      • 2010-10-31
      相关资源
      最近更新 更多