【问题标题】:How to send object that is retrived in a get request to a post requet | Spring Boot Thymeleaf如何将在 get 请求中检索到的对象发送到 post 请求 | Spring Boot Thymeleaf
【发布时间】:2017-10-20 13:22:42
【问题描述】:

假设我已经在 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();
        ......
    }

【问题讨论】:

    标签: forms post spring-boot get thymeleaf


    【解决方案1】:

    好的,我终于在我的表单中使用以下解决了它:

    <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:field 并添加 name 属性即可。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-09
      • 2017-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多