表单 – 如何将在get请求中重试的对象发送到post requet | Spring Boot Thymeleaf

假设我已经在html模板上呈现了get请求的输出,现在我需要使用相同的数据通过同一页面上的按钮传递给post请求.我怎么做?

我正在尝试做这样的事情:

<!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和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();
        ......
    }

解决方法

好吧,我终于在我的表单中使用以下内容解决了它:
<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>

所需要的只是删除th:字段并添加name属性.

相关文章

vue阻止冒泡事件 阻止点击事件的执行 &lt;div @click=&a...
尝试过使用网友说的API接口获取 找到的都是失效了 暂时就使用...
后台我拿的数据是这样的格式: [ {id:1 , parentId: 0, name:...
JAVA下载文件防重复点击,防止多次下载请求,Cookie方式快速简...
Mip是什么意思以及作用有哪些