我試圖在與 Thymleaf 和 Spring 中的表單相同的頁面上顯示結果,這是我到目前為止所嘗試的:
控制器:
@Controller
public class ThymeleafController {
@GetMapping("/hello")
public String getHello() {
return "hello";
}
@PostMapping("/test")
public String test(@RequestParam("text1")String str, Model model) {
model.addAttribute("sample", str);
return "test";
}
}
test.html 頁面:
<!DOCTYPE html>
<html xmlns:th ="http://www.thymeleaf.org" >
<head>
<meta charset ="UTF-8" ></meta>
<title> Hello World</title>
</head>
<body>
<h1> Hello World</h1>
<form method ="post" action ="/test" >
Enter: <input type ="text" name ="text1" />
<input type ="submit" value ="click" />
</form>
<span th:text ="${sample == null} ? 'null' : ${sample}" ></span>
</body>
</html>
當我進入測驗頁面時,我看到了這個錯誤:
There was an unexpected error (type=Method Not Allowed, status=405).
Request method 'GET' not supported
org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported
有人可以給我一個想法或一個例子,我如何在與表單/輸入相同的頁面上顯示一個值?謝謝
uj5u.com熱心網友回復:
參考:https ://hellokoding.com/handling-form-submission-example-with-java-spring-boot-and-freemarker/
添加 for get ,這樣這個問題就會得到解決。
@Controller
public class ThymeleafController {
@GetMapping("/hello")
public String getHello() {
return "hello";
}
@GetMapping("/test")
public String testfrm() {
return "test";
}
@PostMapping("/test")
public String test(@RequestParam("text1")String str, Model model) {
return "test";
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/451679.html
