我的 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
最佳答案
将 th:action
更改为:
th:action="@{/dashboard/makeAndSendInvoice/{email}(email=${po.Email})}"
并在 RequestMapping
值中添加 PathVariable
,如下所示:
@RequestMapping(method=POST, path="/dashboard/makeAndSendInvoice/{email:.+}")
public String makeAndSendInvoice(@PathVariable("email") String email) {
Variable templates are also allowed in URL paths, like
@{/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 :
In Spring MVC you can use the
@PathVariable
annotation on a method argument to bind it to the value of a URI template variable:@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"; }
关于java - 使用链接 url @ 发送参数 inThymleaf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36956186/