首页 > 解决方案 > Spring Boot MVC - 不支持请求方法“POST”

问题描述

我正在使用 Maven 使用 Spring Boot 开发一个基本的 Web 应用程序。当我运行应用程序时,我收到以下错误,并且我的应用程序停止运行:

There was an unexpected error (type=Method Not Allowed, status=405).Request method 'POST' not supported
org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported

我想要做的是,我想从 html 页面 ( index.html) 内的表单内的文本框中获取用户输入,然后我将处理该数据。在 JSF 中,我们可以简单地使用保存在简单 POJO 类中的 getter 和 setter 来做到这一点。但是在 Spring MVC 中,由于手动处理 HTTP 请求,它似乎有点复杂。

这是我的文件夹结构:

在此处输入图像描述

这是我的IndexController.java

package com.ilter.application;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;

import com.ilter.model.Index;

@Controller
public class IndexController {

    @GetMapping("/static/index")
    public String greetingForm(Model model) {
        model.addAttribute("index", new Index());
        return "index";
    }

    @PostMapping("/static/index")
    public String greetingSubmit(@ModelAttribute Index index, Model model) {
        model.addAttribute("index", index);
        return "/tags.html"; //after data is gathered, user is redirected to tags.html page
    }

}

这是Application.java上课

package com.ilter.application;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan(basePackages = {"com.ilter.model"})
@SpringBootApplication
public class Application {
    
    
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

这是我保存在com.ilter.model包内的简单 pojo

package com.ilter.model;

public class Index {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

这是我的 index.html 网页

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<link rel="stylesheet"
    href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
    integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO"
    crossorigin="anonymous">
<title>My Tool</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="index.css">
</head>
<body>

    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
        integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
        crossorigin="anonymous"></script>
    <script
        src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"
        integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy"
        crossorigin="anonymous"></script>
    <div role="navigation">

        <nav class="navbar navbar-dark bg-dark">
            <a class="navbar-brand" href="#">App</a>
            <button class="navbar-toggler" type="button" data-toggle="collapse"
                data-target="#navbarText" aria-controls="navbarText"
                aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarText">
                <ul class="navbar-nav mr-auto">
                    <li class="nav-item active"><a class="nav-link" href="#">Home
                            <span class="sr-only">(current)</span>
                    </a></li>
                    <li class="nav-item"><a class="nav-link" href="#">Report</a></li>
                    <li class="nav-item"><a class="nav-link" href="#">Past
                            Reports History</a></li>
                </ul>
                <span class="navbar-text"> About us </span>
            </div>
        </nav>
    </div>
    <form class="form" action="#" th:action="@{/index}"
        th:object="${index}" method="post">
        <h3 class="text-center" style="margin-top: 50px;">Enter Person's
            Name</h3>
        <div class="md-form active-cyan-2 mb-3" id="search"
            style="padding-left: 150px">
            <input class="form-control" type="text" placeholder="Enter Name"
                aria-label="Search" th:field="*{name}">
        </div>
        <div id="div-btn">
            <button id="prm-btn" type="submit" class="btn btn-primary">Go
                (To the next page)</button>
        </div>
    </form>
</body>
</html>

最后,这是我的 POM 文件

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.ilter</groupId>
    <artifactId>MyApp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.2.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.webjars/bootstrap -->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>4.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>7.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.eclipse.m2e</groupId>
                <artifactId>lifecycle-mapping</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <lifecycleMappingMetadata>
                        <pluginExecutions>
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>org.apache.maven.plugins</groupId>
                                    <artifactId>maven-compiler-plugin</artifactId>
                                    <versionRange>[3.3]</versionRange>
                                    <goals>
                                        <goal>testCompile</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <ignore></ignore>
                                </action>
                            </pluginExecution>
                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
    

我已经在互联网上尝试了几乎所有可能的解决方案,检查了几乎所有与我相似的问题,但没有一个有帮助,仍然无法解决。我曾经使用 JSF 框架进行 Web 应用程序开发,由于它现在被认为是侏罗纪,我认为是时候转向一个更新的框架,例如 Spring。但是 CRUD 操作和 HTTP 请求让我很难说实话。

我不知道根本问题,可能是 CRUD 操作使用不当,缺少 Maven 包甚至文件夹结构。无论如何,我在这个问题上花了一些时间,所有的帮助和想法将不胜感激。

标签: javaspringspring-bootmaven

解决方案


推荐阅读