[英]How to bind the URI param with view in the Constructor in Spring?
我有一部分觀點
<form method="post" th:action="@{{id}/view(id=${id})}">
<p>Get a guy by his id</p>
<input name="id" placeholder="Type his ID here"/>
<button>Find him!</button>
</form>
而一個
@PostMapping("/{id}/view")
但我無法理解使用什么來將該視圖與構造函數綁定以獲得像“/3/view”這樣的輸出請求
在控制器中使用@PathVariable
@PostMapping("/{id}/view")
public String postView(@PathVariable String id) {
// id can be used here
}
編輯:此外,您的th:action="@{{id}/view(id=${id})}"
被發送到瀏覽器: action="/view"
因為id
還不知道。 所以這就是你得到的路徑。
要從 thymeleaf 獲取數據,請使用一個對象,例如 User,並將其添加到表單頁面的模型中,以便 thymeleaf 可以使用th:object
<form method="post" th:action="@{/users/}" th:object="${userInfo}">
<p>Get a guy by his id</p>
<input name="id" placeholder="Type his ID here" th:field="*{id}"/>
<button>Find him!</button>
</form>
並在控制器中
@PostMapping("/view")
public String getUser(@ModelAttribute User user) {
// user.id can be used here to redirect
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.