2017-04-12 5 views
0

우선 내 영어를 부르짖습니다. 사용자와 게임을 만들 수있는 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

App VIEW

답변

0

몇 가지 작업이 필요합니다.

Thymeleaf에게 모델 속성 객체에 대해 그리고 그것이 어떻게 바인딩되어야 하는지를 알아야합니다. 양식 태그에 th:object을 입력하고 입력 태그에 th:field을 추가하십시오. 나는 또한 당신이 마지막 방법의 논리에 대해 @PostMapping을 원할 것이라고 의심하고 있습니다.

은 제외 : 목록에서 하나의 값이 이후 @RequestMapping(value = "/order-list")@RequestMapping("/order-list")로 단축 할 수있다. 또한 해당 코드에서 NPE 관련 검사를 수행한다고 가정합니다.

+0

답변 해 주셔서 감사합니다. 페이지 코드의 문구에 오류가 있음을 확인했습니다. 나는 푸시 바이크의 원리에 따라 작동하도록 올바르게 쓰는 법을 모른다. 위, 어떻게 작동해야하는지에 대한 그림을 추가했습니다. 코드를 올바르게 작성하는 법을 알려주시겠습니까? –

+0

http://www.thymeleaf.org/doc/tutorials/3.0/thymeleafspring.html#creating-a-form을 살펴보십시오. – bphilipnyc

관련 문제