Java Spring Boot MVC-请求方法';邮政';不支持
我正在使用Maven使用springboot开发一个基本的web应用程序。当我运行应用程序时,出现以下错误,我的应用程序停止运行:Java Spring Boot MVC-请求方法';邮政';不支持,java,spring,spring-boot,maven,Java,Spring,Spring Boot,Maven,我正在使用Maven使用springboot开发一个基本的web应用程序。当我运行应用程序时,出现以下错误,我的应用程序停止运行: There was an unexpected error (type=Method Not Allowed, status=405).Request method 'POST' not supported org.springframework.web.HttpRequestMethodNotSupportedException: Request method '
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就可以做到这一点。但在SpringMVC中,由于手动处理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
}
}
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);
}
}
下面是Application.java
class
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
}
}
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);
}
}
这是我的简单pojo,它保存在com.ilter.model
包中
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>
4.0.0
com.ilter
MyApp
0.0.1-快照
1.8
org.springframework.boot
spring启动程序父级
2.3.2.2发布
org.springframework.boot
弹簧启动装置
org.springframework.boot
SpringBootStarterWeb
org.springframework.boot
弹簧靴起动器执行器
org.springframework.boot
弹簧靴开发工具
运行时
真的
org.webjars
独自创立
4.3.1
org.primefaces
素面
7
org.springframework.boot
弹簧起动试验
测试
org.junit.vintage
朱尼特老式发动机
org.eclipse.m2e
生命周期映射
1.0.0
org.apache.maven.plugins
maven编译器插件
[3.3]
测试编译
org.springframework.boot
springbootmaven插件
我在互联网上尝试了几乎所有可能的解决方案,检查了几乎所有与我类似的问题,但没有一个是有用的,仍然无法解决。我曾经使用JSF框架进行web应用程序开发,由于它被认为是一种法律上的框架,现在我认为是时候转向一个更新的框架,比如Spring了。但是CRUD操作和HTTP请求给了我一些诚实的时间
我不知道问题的根源,可能是CRUD操作使用不当,Maven包丢失,甚至是文件夹结构丢失。无论如何,我已经在这个问题上花了一些时间,所有的帮助和想法都将得到感谢。一些toolstip。在SpringMVC中,您将html文件放在模板文件夹中,而不是静态文件夹中。@PostMapping(“name”)上的名称是该方法的别名,因此在提交表单中,尝试将th:action=“@{/index}”更改为th:action=“@{/static/index}”。它应该会起作用/tags.html也不是必需的。只要“标记”就可以了。index.html中的操作与映射不同。还有,为什么要使用静态文件夹?静态文件夹用于静态资源,如css、javascripts或IMG。您的模板应位于templates文件夹中。感谢@AlexandreGuerreiro和yogsma提供的提示。我使用静态文件夹,我想是因为我在下面的示例中看到了它[.但作为下一步,我会将它们放在template文件夹中,并通知您所有结果。我已将index.html移到templates文件夹中,但现在我得到以下错误:
由以下原因引起:
经过一些研究,我发现我需要添加modeldattribute=“loginModel”
组件添加到我的html页面,这似乎是不可能的,因为此组件与jsp一起工作。我想我需要了解如何将此组件添加到html页面,否则我将不得不将我的页面设置为.jsp而不是.html。我已通过添加ViewController
并实现webmvcconfiguer
解决了此问题。此外,我还需要e在我的Indexcontroller类中做了一些更改,例如删除模型
和添加BindingResult
参数。现在我可以成功地获得用户输入。一些工具提示。在Spring MVC中,您将html文件放在模板文件夹中,而不是静态文件夹中。名称在@PostMapping(“名称”)上是该方法的别名,因此在提交表单中,尝试将th:action=“@{/index}”更改为th:action=“@{/static/index}”。它应该可以工作。/tags.html不是必需的。只需“tags”就可以了。您的