`@RequestMapping(value = "/abc", method = RequestMethod.POST)
public class TestController {
.....
}`
what if user will request /abc
as GET Method how i can Handle Request?
CodePudding user response:
You will get an exception:
Request method 'GET' not supported
You can support multiple method (GET,POST...) by remove method in annotation.
@RequestMapping(value = "/abc")
CodePudding user response:
Yes it is possible. Post /abc
can add data in List
and redirect:/abc
redirect to get mapping value = "/abc", method = RequestMethod.GET
and pass data through model to view.
Request URL for GET
and POST
are same but first redirect to the url and second is send data to view
Controller
private List<String> someData = new ArrayList<>();
@RequestMapping(value = "/abc", method = RequestMethod.POST)
public String TestControllerPost(String something) {
someData.add(something);
return "redirect:/abc"; // going to /abc get url
}
@RequestMapping(value = "/abc", method = RequestMethod.GET)
public String TestControllerGet(Model model) {
model.addAttribute("someData", someData);
return "index";
}
View as Thymeleaf(index.html)
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<body>
<form action="/abc" method="post">
<span>User Name:</span>
<input type="text" name="name"/> <br/>
<input type="submit" value="Submit">
</form>
<br>
<h3>Get Data</h3>
<p th:each="data: ${someData}">
<span th:text="${data}"></span>
</p>
</body>
</html>