0
我想通過ID在我的Spring-Boot應用程序中找到用戶。 的控制器 - 方法是:通過thymeleaf中的URL從視圖到控制器發佈ID
@RequestMapping(value = "finduser/{id}", method = RequestMethod.GET)
public User get(@PathVariable Long id) throws NotFoundException {
log.info("Invoked method: get with ID: " + id);
log.warn("Searching for user with ID " + id);
User findOne = userRepository.findOne(id);
if (findOne == null){
log.error("Unexpected error, User with ID " + id + " not found");
throw new NotFoundException("User with ID " + id + " not found");
}
log.info("User found. Sending request back. ID of user is " + id);
return findOne;
}
我finduser.html的主體是:
<body>
<h1>Find User:</h1>
<form th:object="${user}" th:action="@{finduser}" method="post">
<p>Find by ID: <input type="text" th:field="*{id}" /></p>
<p><input type="submit" value="Submit" /></p>
</form>
</body>
我應該改變什麼從視圖ID傳遞給我的控制器,所以我的控制器可以使用id和搜索用戶?
我覺得ID必須通過,因爲
@RequestMapping的URL傳遞(值= 「finduser /(編號)」 ...)