java - 使用链接 url @ 发送参数 inThymleaf

标签 java spring-mvc thymeleaf

我的 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) {

来自Thymeleaf - Link URLs :

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/

相关文章:

spring - @ControllerAdvice 是否覆盖 SimpleMappingExceptionResolver?

java - 如何在为路径提供变量的同时使用 spring select 标签获取列表

java - BeanCreationException - 以前工作过,现在不再工作

java - Tangosol Coherence 可以缓存不可序列化的对象吗?

java意外隐式接口(interface)实现

java - 根据单元格数据为表格单元格着色

html - 复选框对齐未按预期工作

java - 表单中的用户名始终为空

java - thymeleaf 项目中的 Bootstrap 模式

java - jOOQ 禁用 DAO 和 POJO 生成