问题描述
假设我已经在html模板上渲染了获取请求的输出,现在我需要使用这些相同的数据通过同一页面上的按钮传递到发布请求。我该怎么做?我试图做这样的事情:
<!DOCTYPE html>
< html xmlns =http://www.w3.org/1999/xhtml
xmlns:th =http://www.thymeleaf.org>
< head>
< title>客户主页< / title>
< link rel =stylesheethref =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>感谢您与Gamma Airlines的预订,以下是您的预订总结:< / p>
< table th:object =$ {booking} class =table table-bordered table-striped>
< tbody>
< tr>
< tr> ; td>来源< / td>
< td th:text =$ {routeStart}>< / td>
< / tr>
< tr>
< td>目的地< / td>
< td th:text =$ {routeDestination}>< / td>
< / tr>
< ; tr>
< td>预订费用< / td>
< / tr>
< td th:text =$ {bookingCharges}>< / td>
< 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
role =form >
< button type =submitclass =btn btn-primary btn-block>电子邮件< / button>
< / form>
< / div>
< / td>
< td>
< div class =col-sm-9>
< form action =#th:action =@ {/ genpdf}
th:object =$ {booking}method =post
role =form >
< button type =submitclass =btn btn-primary btn-block>生成PDF< / button>
< / form>
< / div>
< / td>
< / tr>
< / tbody>
< / table>
< / div>
< / body>
< / html>
编辑:有人请帮助告诉我一个简单的方法来传递对象,因为我必须使用它许多地方和对象在某些情况下也包含许多子对象。例如。以下情况:
< div style =margin-top:2cmclass =container>
< table class =table table-bordered table-striped>
< thead>
< tr>
< td>来源< / td>
< td>目的地< / td>
< td>金额< / td>
< td>货币< / td>
< td>动作< / td>
< / tr>
< / thead>
< tbody>
< tr th:if =$ {offering.empty}>
< td colspan =5>无优惠< / td>
< / tr>
< 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 = 形式 >
< button type =submitclass =btn btn-primary btn-block> Book< / button>
< / form>
< / td>
< / tr>
< / tbody>
< / table>
< / div>
更新2:
我甚至通过在get请求中发送一个新对象来改变模板,并尝试逐个赋值,但我仍然在控制器中将其作为空值。
< table class =table table-bordered table-striped>
< thead>
< tr>
< td>来源< / td>
< td>目的地< / td>
< td>金额< / td>
< td>货币< / td>
< td>动作< / td>
< / tr>
< / thead>
< tbody>
< tr th:if =$ {offering.empty}>
< td colspan =5>无优惠< / td>
< / tr>
< 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 = 形式 >
< input type =hiddenth:attr =value = $ {offer.route.from}th:field =* {routeStart}/>
< input type =hiddenth:attr =value = $ {offer.route.to}th:field =* {routeDestination}/>
< input type =hiddenth:attr =value = $ {offer.price.amount}th:field =* {bookingCharges}/>
< input type =hiddenth:attr =value = $ {offer.price.currency}th:field =* {chargesCurrency}/>
< button type =submitclass =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 =新的ModelAndView();
AirlineOffer [] offersArray = airlineClient.getOffers();
列表< AirlineOffer> offers = Arrays.asList(offersArray);
modelAndView.addObject(offers,offers);
预订预订=新预订();
modelAndView.addObject(booking,booking);
modelAndView.setViewName(user / booking);
返回modelAndView;
$ b $ **提交存款请求的POST方法* /
@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-边界条纹>
< thead>
< tr>
< td>来源< / td>
< td>目的地< / td>
< td>金额< / td>
< td>货币< / td>
< td>动作< / td>
< / tr>
< / thead>
< tbody>
< tr th:if =$ {offering.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
role = 形式 >
< input type =hiddenth:value =$ {offer.route.from}name =routeStart/>
< input type =hiddenth:value =$ {offer.route.to}name =routeDestination/>
< input type =hiddenth:value =$ {offer.price.amount}name =bookingCharges/>
< input type =hiddenth:value =$ {offer.price.currency}name =chargesCurrency/>
< button type =submitclass =btn btn-primary btn-block> Book< / button>
< / form>
< / 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.
这篇关于如何将获取请求中回复的对象发送到post requet |春季启动Thymeleaf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
07-26 06:39