spring-mvc - Spring 3——如何从转发器执行 GET 请求

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

我正在尝试将请求转发到另一个接受 GET 请求的 Spring Controller ,但它告诉我不支持 POST。这是我的第一个 Controller 方法的相关部分,它接受 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).

最佳答案

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

让它发挥作用。

关于spring-mvc - Spring 3——如何从转发器执行 GET 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16380558/

相关文章:

file-upload - Next.js & Ant Design Dragr : File upload fails on deployed instance

javascript - Spring MVC JavaScript

java - 在 spring mvc 中,如何使用@RequestMapping 链接到另一个 jsp?

java - Spring 环境变量

gitlab API PUT(项目/:id) 405 Method Not Allowed

Spring security login-processing-url 抛出 405 请求方法 POST 不支持

android - 为什么我们的服务器从我们的 android 应用程序接收 CONNECT 方法请求,而我们没有在代码中使用它?

java - JSON 发布到 Spring Controller

java - 如何使用 Spring application.properties 自定义 Jackson ObjectMapper?

java - Spring MVC HTTP 状态 405 - 不支持请求方法 'POST'