2017-03-03 103 views
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 /(編號)」 ...)

回答

1

您可以指定形式actiondoc ):

還可以包括路徑變量的形式參數 類似於正常的參數,但指定內部 您的URL的路徑的佔位符:

<a th:href="@{/order/{id}/details(id=3,action='show_all')}">

你的情況,你可以這樣做,例如:

<form th:action="@{/finduser/{id}(id=${user.id})}">

,或者如果你喜歡有一個變量的網址:

<form th:action="@{{finduser}/{id}(finduser=${finduser},id=${user.id})}">

相關問題