嗨,我在《行动》第 5 版书中关注了 spring,其中第二章说添加一个 OrderController,它将具有 post 和 get 映射,但每当我尝试点击 URL 时,它都会给我 “出现意外错误(类型=不允许的方法,状态=405)。
不支持请求方法“POST”
org.springframework.web.HttpRequestMethodNotSupportedException:不支持请求方法“POST””
错误
我尝试在 stackoverflow 上搜索,但找不到任何有用的链接。
这是我的OrderForm.html
<h1>Order your taco creations!</h1>
<img th:src="@{/images/TacoCloud.jpg}"/>
<a id="another" th:href="@{/design}">Design another taco</a><br/>
<div th:if="${#fields.hasErrors()}">
<span class="validationError">
Please correct the problems below and resubmit.
</span>
</div>
<h3>Deliver my taco masterpieces to...</h3>
<label for="name">Name: </label>
<input th:field="*{name}" type="text"/>
<br/> <label for="street">Street address: </label>
<input th:field="*{street}" type="text"/>
<br/> <label for="city">City: </label>
这是我的OrderController.java
@RequestMapping("/orders")
public class OrderController {
@GetMapping("/current")
public String orderForm(Model model) {
model.addAttribute("order", new Order());
System.out.println("sdsdsd");
return "orderForm";
}
@PostMapping
public String processOrder(Order order) {
log.info("Order submitted: " + order);
return "redirect:/";
}
最佳答案
如果启用了 spring security,则必须禁用 csrf 保护(如果这是您的项目的可能选项)
@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http);
http.csrf().disable();
}
关于java - Spring MVC 不支持 POST 方法 - tacoCloud spring 的实际应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59414855/