我使用 Spring Boot v2.7.0,從“start.spring.io”安裝并從那里安裝 Thymeleaf,當我在 parent-pom 中搜索時,我發現:thymeleaf-spring5 (v3.0.15.RELEASE),thymeleaf -extras-java8time (v3.0.4.RELEASE)
最近,我需要應用模式<form th:method="put/delete".../>。在谷歌搜索了很多地方之后,我找到了解決方案,該解決方案也在書中被參考:“ Taming Thymeleaf Practical Guide to build a web application with Spring Boot and Thymeleaf - Wim Deblauwe ”這是 Thymeleaf 的頂級/優秀書籍,和我從中學習Thymeleaf。根據這些,我做了:
步驟1:
在application.properties中添加了這個屬性:
spring.mvc.hiddenmethod.filter.enabled=true
我在application.yaml中嘗試了它(作為第二種解決方案,因為以前的方法不起作用),就像這樣:
spring:
mvc:
hiddenmethod:
filter:
enabled: true
第2步:
我用了:
<form th:method="put".../>
<form th:method="delete".../>
第 3 步:
最后,我在控制器處理程式方法中使用了:“ @PutMapping , @DeleteMapping ”。
結果是錯誤訊息:
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
at org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.handleNoMatch(RequestMappingInfoHandlerMapping.java:253)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lookupHandlerMethod(AbstractHandlerMethodMapping.java:442)
谷歌搜索后,我找到了這個解決方案,通過以下方式自己添加所需的bean,它確實有效:
@Bean
public FilterRegistrationBean<HiddenHttpMethodFilter> hiddenHttpMethodFilter() {
FilterRegistrationBean<HiddenHttpMethodFilter> filterRegistrationBean = new FilterRegistrationBean<>(new HiddenHttpMethodFilter());
filterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));
return filterRegistrationBean;
}
我想知道為什么這個配置“spring.mvc.hiddenmethod.filter.enabled=true”,在我的情況下沒有添加需要的bean,我必須自己添加它。
任何人都可以幫助我嗎?非常感謝提前
uj5u.com熱心網友回復:
我自己做了一個測驗,效果很好。
- 在 start.spring.io 上創建一個新專案,選擇具有依賴項 Spring Web 和 Thymeleaf 的 Spring Boot 2.7.1
- 創建控制器:
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.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@RequestMapping
@Controller
public class TestController {
@GetMapping
public String index(Model model) {
model.addAttribute("formData", new TestFormData());
return "index";
}
@PutMapping
public String doPut(@ModelAttribute("formData") TestFormData formData) {
System.out.println("formData.getSomeString() = " formData.getSomeString());
return "redirect:/";
}
}
使用此表單資料類:
public class TestFormData {
private String someString;
public String getSomeString() {
return someString;
}
public void setSomeString(String someString) {
this.someString = someString;
}
}
- 在中創建一個
index.html檔案src/main/resources/templates:
<!DOCTYPE html>
<html lang="en"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form th:action="@{/}" th:method="put" th:object="${formData}">
<input th:field="*{someString}">
<button>Save</button>
</form>
</body>
</html>
- 更新
application.properties包含:
spring.mvc.hiddenmethod.filter.enabled=true
- 啟動應用程式并轉到 http://localhost:8080 并在輸入欄位中輸入一些內容。按下保存時,它會列印到控制臺,顯示
@PutMapping有效。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/495709.html
上一篇:春季啟動調度程式暫停
下一篇:org.springframework.dao.IncorrectResultSizeDataAccessExceptionMongoLimit不起作用?
