`@RequestMapping(value = "/abc", method = RequestMethod.POST)
public class TestController {
.....
}`
如果用戶將請求/abc作為 GET 方法,我該如何處理請求?
uj5u.com熱心網友回復:
你會得到一個例外:
Request method 'GET' not supported
您可以通過注釋中的 remove 方法支持多種方法(GET、POST...)。
@RequestMapping(value = "/abc")
uj5u.com熱心網友回復:
對的,這是可能的。帖子/abc可以添加資料List和redirect:/abc重定向得到映射value = "/abc", method = RequestMethod.GET,并通過模型資料傳遞到視圖。
請求的URLGET和POST是相同的,但第一個重定向到URL和第二個是將資料發送到視圖
控制器
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";
}
以 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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/360454.html
