2017-05-20 1 views
0

출력 템플릿을 HTML 템플릿으로 가져온 결과, 동일한 데이터를 사용하여 동일한 페이지의 버튼을 통해 게시 요청에 전달해야한다고 가정 해 봅시다. 어떻게해야합니까?가져 오기 요청에서 retrived 된 객체를 post 요청에 보내는 방법 | Spring Boot Thymeleaf

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:th="http://www.thymeleaf.org"> 

<head> 
<title>Customer Home</title> 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" /> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
</head> 

<body> 
    <div th:replace="fragments/header :: header"> 
    </div> 

    <div class="container"> 
     <p> Thanks for booking with Gamma Airlines, here are your booking summary: </p> 
     <table th:object="${booking} class="table table-bordered table-striped"> 
     <tbody> 
      <tr> 
        <td>Source</td> 
        <td th:text="${routeStart}"></td>     
      </tr> 
      <tr> 
        <td>Destination</td> 
        <td th:text="${routeDestination}"></td>     
      </tr> 
      <tr> 
        <td>Booking Charges</td> 
        <td th:text="${bookingCharges}"></td>     
      </tr> 
      <tr> 
        <td>Booking Charges Currency</td> 
        <td th:text="${chargesCurrency}"></td>     
      </tr> 
      <tr> 
        <td>Booking Date</td> 
        <td th:text="${#calendars.format(bookingDate)}">July 11, 2012 2:17:16 PM CDT</td>    
      <tr> 
       <td> 
        <div class="col-sm-9"> 
         <form action="#" th:action="@{/email}" 
           th:object="${booking}" method="post" 
           role="form"> 
           <button type="submit" class="btn btn-primary btn-block">Email</button> 
         </form> 
        </div> 
       </td> 
       <td> 
        <div class="col-sm-9"> 
         <form action="#" th:action="@{/genpdf}" 
           th:object="${booking}" method="post" 
           role="form"> 
           <button type="submit" class="btn btn-primary btn-block">Generate PDF</button> 
         </form> 
        </div> 
       </td> 
      </tr> 
     </tbody> 
     </table> 
    </div> 
</body> 
</html> 

편집 : 누군가가 나는 많은 장소와 목적에 사용해야하기 때문에 개체를 전달할 나에게 간단한 방법을 알려 도움을 주시기 바랍니다 포함 많은 자식 객체 나는 이런 식으로 뭔가를 할 노력하고있어

어떤 경우에는 마찬가지입니다. 예. 다음과 같은 경우 :

<div style = "margin-top: 2cm" class="container"> 
     <table class="table table-bordered table-striped"> 
     <thead> 
      <tr> 
      <td>Source</td> 
      <td>Destination</td> 
      <td>Amount</td> 
      <td>Currency</td> 
      <td>Action</td> 
      </tr> 
     </thead> 
     <tbody> 
      <tr th:if="${offers.empty}"> 
      <td colspan="5">No Offers</td> 
      </tr> 
      <tr th:each="offer : ${offers}"> 
      <td th:text="${offer.route.from}"></td> 
      <td th:text="${offer.route.to}"></td> 
      <td th:text="${offer.price.amount}"></td> 
      <td th:text="${offer.price.currency}"></td> 
      <td> 
       <form action="#" th:action="@{/user/booking}" 
         th:object="${offer}" method="post" 
         role="form"> 
         <button type="submit" class="btn btn-primary btn-block">Book</button> 
       </form> 
      </td> 
      </tr> 
     </tbody> 
     </table> 
    </div> 

업데이트 2 : 난 GET 요청에 새로운 객체를 전송하고 값을 하나씩 할당하려고하지만 난 여전히이 null를 얻을하여 템플릿을 변경

컨트롤러.

<table class="table table-bordered table-striped"> 
     <thead> 
      <tr> 
      <td>Source</td> 
      <td>Destination</td> 
      <td>Amount</td> 
      <td>Currency</td> 
      <td>Action</td> 
      </tr> 
     </thead> 
     <tbody> 
      <tr th:if="${offers.empty}"> 
      <td colspan="5">No Offers</td> 
      </tr> 
      <tr th:each="offer : ${offers}"> 
      <td th:text="${offer.route.from}"></td> 
      <td th:text="${offer.route.to}"></td> 
      <td th:text="${offer.price.amount}"></td> 
      <td th:text="${offer.price.currency}"></td> 
      <td> 
       <form action="#" th:action="@{/user/booking}" 
         th:object="${booking}" method="post" 
         role="form"> 
         <input type="hidden" th:attr="value = ${offer.route.from}" th:field="*{routeStart}" /> 
         <input type="hidden" th:attr="value = ${offer.route.to}" th:field="*{routeDestination}" /> 
         <input type="hidden" th:attr="value = ${offer.price.amount}" th:field="*{bookingCharges}" /> 
         <input type="hidden" th:attr="value = ${offer.price.currency}" th:field="*{chargesCurrency}" /> 
         <button type="submit" class="btn btn-primary btn-block">Book</button> 
       </form> 
      </td> 
      </tr> 
     </tbody> 
     </table> 

이러한 내 GET 및 사후 방법은 다음과 같습니다

@RequestMapping(value="/user/booking", method = RequestMethod.GET) 
    public ModelAndView getOffers() 
    { 
     ModelAndView modelAndView = new ModelAndView(); 
     AirlineOffer[] offersArray= airlineClient.getOffers(); 
     List<AirlineOffer> offers = Arrays.asList(offersArray); 
     modelAndView.addObject("offers", offers); 
     Booking booking = new Booking(); 
     modelAndView.addObject("booking", booking); 
     modelAndView.setViewName("user/booking"); 
     return modelAndView; 
    } 

    /** POST method for submitting deposit request */ 
    @RequestMapping(value = "/user/booking", method = RequestMethod.POST) 
    public ModelAndView book(@Valid Booking booking, 
      BindingResult bindingResult, HttpServletRequest request) 
    { 
     ModelAndView modelAndView = new ModelAndView(); 
     ...... 
    } 

답변

0

좋아, 나는 마침내 내 형태로 다음 사용하여 해결 :

<table class="table table-bordered table-striped"> 
     <thead> 
      <tr> 
      <td>Source</td> 
      <td>Destination</td> 
      <td>Amount</td> 
      <td>Currency</td> 
      <td>Action</td> 
      </tr> 
     </thead> 
     <tbody> 
      <tr th:if="${offers.empty}"> 
      <td colspan="5">No Offers</td> 
      </tr> 
      <tr th:each="offer, stat : ${offers}"> 
      <td th:text="${offer.route.from}"></td> 
      <td th:text="${offer.route.to}"></td> 
      <td th:text="${offer.price.amount}"></td> 
      <td th:text="${offer.price.currency}"></td> 
      <td> 
       <form action="#" th:action="@{/user/booking}" 
         th:object="${booking}" method="post" 
         role="form"> 
         <input type="hidden" th:value="${offer.route.from}" name="routeStart" />  
         <input type="hidden" th:value = "${offer.route.to}" name="routeDestination" /> 
         <input type="hidden" th:value = "${offer.price.amount}" name="bookingCharges" /> 
         <input type="hidden" th:value = "${offer.price.currency}" name="chargesCurrency" /> 
         <button type="submit" class="btn btn-primary btn-block">Book</button> 
       </form> 
      </td> 
      </tr> 
     </tbody> 

일을 제거하는 것이었다했다 모든 : 필드에 이름이 인 특성을 추가하십시오.

관련 문제