【发布时间】:2017-07-23 16:11:34
【问题描述】:
我想通过 ID 在我的 Spring-Boot 应用程序中查找用户。 控制器方法是:
@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必须通过url传递,因为
@RequestMapping(value = "finduser/{id}"...)
【问题讨论】:
标签: spring-mvc model-view-controller spring-boot thymeleaf