我的 Thymleaf 页面中有以下表格:
<div class="panel-body">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Issue Date</th>
<th>Payment Schedule</th>
<th>Total</th>
<th>Status</th>
<th>Start Date</th>
<th>End Date</th>
<th>Send Invoice</th>
</tr>
</thead>
<tbody>
<tr class="table-row" th:each="p : ${POList}">
<td th:text="${p.issueDate}"></td>
<td th:text="${p.paymentSchedule}"></td>
<td th:text="${p.total}"></td>
<td th:text="${p.status}"></td>
<td th:text="${p.rentalPeriod.startDate}"></td>
<td th:text="${p.rentalPeriod.endDate}"></td>
<td>
<form style='float:left; padding:5px; height:0px' th:object="${po}" th:method="post" th:action="@{'/dashboard/makeAndSendInvoice(email=${po.Email})'}">
<button class="btn btn-default btn-xs" type="submit">Send Invoice</button>
</form>
</td>
</tr>
</tbody>
</table>
</div>
我试图将po.Email
的值发送给该方法。
我想th:action="@{'/dashboard/makeAndSendInvoice(email=${po.Email})'}"
会创建dashboard/makeAndSendInvoice/{Email}
所以我试图在这样的方法中得到它:
@RequestMapping(method=POST, path="makeAndSendInvoice")
public String makeAndSendInvoice(@PathVariable("email") String email){
System.out.println("Invoice is sent to..................."+email);
return "Invoice";
}
但在我看来它不起作用,因为它无法识别我的方法。
那么如何在我的方法中接收po.Email
答案 0 :(得分:1)
将th:action
更改为:
th:action="@{/dashboard/makeAndSendInvoice/{email}(email=${po.Email})}"
并在PathVariable
值中添加RequestMapping
,如:
@RequestMapping(method=POST, path="/dashboard/makeAndSendInvoice/{email:.+}")
public String makeAndSendInvoice(@PathVariable("email") String email) {
URL路径中也允许使用变量模板,例如
@{/order/{orderId}/details(orderId=${orderId})}
<!-- Will produce '/gtvg/order/details?orderId=3' (plus rewriting) --> <a href="details.html" th:href="@{/order/details(orderId=${o.id})}">view</a> <!-- Will produce '/gtvg/order/3/details' (plus rewriting) --> <a href="details.html" th:href="@{/order/{orderId}/details(orderId=${o.id})}">view</a>
来自Spring - URI Template Patterns:
在Spring MVC中,您可以在方法上使用
@PathVariable
注释 将其绑定到URI模板变量的值的参数:@RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET) public String findOwner(@PathVariable String ownerId, Model model) { Owner owner = ownerService.findOwner(ownerId); model.addAttribute("owner", owner); return "displayOwner"; }