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

前端之家收集整理的这篇文章主要介绍了表单 – 如何将在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属性.

原文链接:https://www.f2er.com/html/231972.html

猜你在找的HTML相关文章