【问题标题】:HTTP Status 405 - Request method 'GET' not supported MVCspringHTTP状态405 - 不支持请求方法'GET' MVCspring
【发布时间】:2018-07-13 22:26:23
【问题描述】:

我正在使用 spring mvc 创建一个 web 应用程序,并且我是一个初学者。所以我的应用程序是一个基于本教程的简单登录应用程序: https://www.onlinetutorialspoint.com/spring/spring-mvc-login-form-example.html

当我运行应用程序时显示错误:

État HTTP 405 – Method Not Allowed


Type Rapport d''état

message Request method 'GET' not supported

description La méthode HTTP spécifiée n''est pas autorisée pour la ressource demandée.

【问题讨论】:

  • 请发布您的完整代码

标签: spring-mvc tomcat


【解决方案1】:

检查登录表单操作方法是否为POST

【讨论】:

    【解决方案2】:

    这是我的登录控制器:

    package com.spring.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.ModelAttribute;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    @Controller
    public class LoginController {
        @RequestMapping(value = "/login", method=RequestMethod.GET)
        public String init(Model model) {
            model.addAttribute("msg", "Please Enter Your Login Details");
            return "login";
        }
    
        @RequestMapping(method=RequestMethod.POST)
        public String submit(Model model, @ModelAttribute("loginBean") LoginBean loginBean) {
            if (loginBean != null && loginBean.getUserName() != null & loginBean.getPassword() != null) {
                if (loginBean.getUserName().equals("chandra") && loginBean.getPassword().equals("chandra123")) {
                    model.addAttribute("msg", loginBean.getUserName());
                    return "success";
                } else {
                    model.addAttribute("error", "Invalid Details");
                    return "login";
                }
            } else {
                model.addAttribute("error", "Please enter Details");
                return "login";
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      提供您的完整代码,回答问题会更有帮助。 也许你在几个地方弄错了。

      1. 你的表单是post方法,所以你的action方法必须是post类型。像这样,

        @RequestMapping(method=RequestMethod.POST)

      2. 检查您的表单是否为method="post"。默认情况下,表单方法是method="get"

      【讨论】:

        猜你喜欢
        • 2015-04-23
        • 2016-05-24
        • 2013-03-03
        • 2016-11-11
        • 2015-06-26
        • 1970-01-01
        • 1970-01-01
        • 2012-12-14
        • 2015-03-10
        相关资源
        最近更新 更多