우선 내 영어를 부르짖습니다. 사용자와 게임을 만들 수있는 CRUD 프로젝트를 만듭니다. 사용자는 게임을 구입하고 라이브러리에 추가 할 수있는 돈이 있습니다. 그 과정에서 문제가 발생했습니다. 컨트롤러의 모델에서 객체를 가져올 수 없습니다. 무엇이 문제 일 수 있습니까?모델에서 객체를 가져 오는 방법은 무엇입니까?
컨트롤러
@RequestMapping("/user-profile/{id}")
public String getUserProfile(@PathVariable Integer id, Model model) {
logger.debug("Received user data");
model.addAttribute("user", userDAO.findOne(id));
model.addAttribute("gameLib", userDAO.findOne(id).getGames());
return "user/userProfile";
}
@RequestMapping(value = "/order-list")
public String getOrderGameList(@ModelAttribute("user") User user, Model model) {
logger.debug("Received order list for user");
model.addAttribute("user", user);
model.addAttribute("games", gameDAO.findAll());
return "order/order-form";
}
@RequestMapping(value = "/order-list/{gameId}")
public String postOrderGameList(@ModelAttribute("user") User user, @PathVariable Integer gameId) {
logger.debug("Add game in user library");
Game game = gameDAO.findOne(gameId);
logger.debug("Ordering game");
user.setWallet(user.getWallet() - game.getPrice());
user.getGames().add(game);
if (user.getWallet() < game.getPrice()) {
logger.debug("Ordering failed");
} else {
logger.debug("Ordering game");
user.setWallet(user.getWallet() - game.getPrice());
user.getGames().add(game);
}
return "redirect:/user-profile/{" + user.getId() + "}";
}
HTML
<h2>Games</h2>
<table class="list">
<thead>
<tr>
<th>ID</th>
<th>NAME</th>
<th>Description</th>
<th>Type</th>
<th>Year</th>
<th>Price</th>
<th></th>
</tr>
</thead>
<tbody>
<tr th:each="game : ${games}">
<td th:text="${game.id}"></td>
<td th:text="${game.name}">Game</td>
<td th:text="${game.description}">Description</td>
<td th:text="${game.gameType}">type</td>
<td th:text="${game.year}">2017</td>
<td th:text="${game.price}">0</td>
<td>
<form th:action="@{/users/order-list/} + ${game.id}" method="post">
<input type="submit" th:value="Buy"/>
</form>
</td>
</tr>
</tbody>
</table>
이 전체 프로젝트에 link입니다
UPD
답변 해 주셔서 감사합니다. 페이지 코드의 문구에 오류가 있음을 확인했습니다. 나는 푸시 바이크의 원리에 따라 작동하도록 올바르게 쓰는 법을 모른다. 위, 어떻게 작동해야하는지에 대한 그림을 추가했습니다. 코드를 올바르게 작성하는 법을 알려주시겠습니까? –
http://www.thymeleaf.org/doc/tutorials/3.0/thymeleafspring.html#creating-a-form을 살펴보십시오. – bphilipnyc