本文介绍了如何将在 get 请求中检索到的对象发送到 post requet |Spring Boot Thymeleaf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

<html xmlns="http://www.w3.org/1999/xhtml"xmlns:th="http://www.thymeleaf.org"><头><title>客户主页</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><身体><div th:replace="fragments/header::header">

<div class="容器"><p>感谢您通过 Gamma Airlines 预订,以下是您的预订摘要:</p><table th:object="${booking} class="table table-bordered table-striped"><tr><td>来源</td><td th:text="${routeStart}"></td></tr><tr><td>目的地</td><td th:text="${routeDestination}"></td></tr><tr><td>预订费用</td><td th:text="${bookingCharges}"></td></tr><tr><td>预订费用货币</td><td th:text="${chargesCurrency}"></td></tr><tr><td>预订日期</td><td th:text="${#calendars.format(bookingDate)}">2012 年 7 月 11 日下午 2:17:16 CDT</td><tr><td><div class="col-sm-9"><form action="#" th:action="@{/email}"th:object="${booking}" method="post"角色=形式"><button type="submit" class="btn btn-primary btn-block">电子邮件</button></表单>

</td><td><div class="col-sm-9"><form action="#" th:action="@{/genpdf}"th:object="${booking}" method="post"角色=形式"><button type="submit" class="btn btn-primary btn-block">生成PDF</button></表单>

</td></tr></tbody>

有人请告诉我一个简单的方法来传递对象,因为我必须在很多地方使用它,并且在某些情况下对象还包含许多子对象.例如.以下情况:

<table class="table table-bordered table-striped"><头><tr><td>来源</td><td>目的地</td><td>金额</td><td>货币</td><td>动作</td></tr></thead><tr th:if="${offers.empty}"><td colspan="5">无优惠</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"角色=形式"><button type="submit" class="btn btn-primary btn-block">Book</button></表单></td></tr></tbody>

更新 2:

我什至通过在 get 请求中发送一个新对象来更改模板并尝试一个一个地分配值,但我仍然在控制器中将其设为 null.

<头><tr><td>来源</td><td>目的地</td><td>金额</td><td>货币</td><td>动作</td></tr></thead><tr th:if="${offers.empty}"><td colspan="5">无优惠</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"角色=形式"><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></表单></td></tr></tbody>

这些是我的 get 和 post 方法:

@RequestMapping(value="/user/booking", method = RequestMethod.GET)公共 ModelAndView getOffers(){ModelAndView modelAndView = new ModelAndView();AirlineOffer[]offersArray=airlineClient.getOffers();列表<AirlineOffer>优惠 = Arrays.asList(offersArray);modelAndView.addObject("offers", offer);预订预订 = new Booking();modelAndView.addObject("预订", 预订);modelAndView.setViewName("用户/预订");返回模型和视图;}/** 提交存款请求的 POST 方法 */@RequestMapping(value = "/user/booking", method = RequestMethod.POST)public ModelAndView book(@Valid Booking 预订,BindingResult bindingResult, HttpServletRequest 请求){ModelAndView modelAndView = new ModelAndView();......}
解决方案

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

<头><tr><td>来源</td><td>目的地</td><td>金额</td><td>货币</td><td>动作</td></tr></thead><tr th:if="${offers.empty}"><td colspan="5">无优惠</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"角色=形式"><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></表单></td></tr></tbody>

只需删除 th:field 并添加 name 属性.

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();
        ......
    }
解决方案

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.

这篇关于如何将在 get 请求中检索到的对象发送到 post requet |Spring Boot Thymeleaf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-19 07:57