2017-05-20 11 views
0

私は出力HTMLをHTMLテンプレート上で取得要求をレンダリングしたとしましょう。今は同じデータを使用して、同じページのボタン経由で投稿要求に渡す必要があります。それ、どうやったら出来るの?投稿要求に取得されたオブジェクトをポスト・リプライに送信する方法| Springブート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> 

UPDATE 2:

私もget要求に新しいオブジェクトを送信することにより、テンプレートを変更し、値を一つずつ割り当てるしようが、私はまだでヌルとしてそれを得ますコントローラ。

<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とPOSTメソッドです:

@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> 

それが取ったすべてが目を削除することでした:フィールドに、という名前の属性を追加します。

関連する問題