Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
362 views
in Technique[技术] by (71.8m points)

forms - How to send object that is retrived in a get request to a post requet | Spring Boot Thymeleaf

Let's say I have rendered the output rom the get request on an html template, now I need to use this same data to pass to a post request via a button on a same page. How do I do that?

I'm trying to do something like this:

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

Edit: Somebody please help tell me a simple method to pass the object since I have to use it at many places and object contains many child object as well in some cases. eg. the following case:

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

I even changed the template by sending a new object in the get request and try to assign values one by one but I still get it as null in a controller.

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

These are my get and post methods:

@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();
        ......
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Alright, I finally resolved it using the following in my form:

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

All it took was to remove th:field and add the name attribute.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...