I am trying to show the results on the same page as the form in Thymleaf and Spring and this is what I have tried till now:
Controller:
@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 page:
<!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>
When i go on test page i see this error:
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
Can someone give me an ideea or an example how I could display a value on the same page as the form / input ? Thanks
CodePudding user response:
Ref : https://hellokoding.com/handling-form-submission-example-with-java-spring-boot-and-freemarker/
Add for get , so this issue will get solved.
@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";
}
}