Jeff
Jeff

Reputation: 8411

Sending parameter inThymleaf using link urls @

I have the following form in my Thymleaf page:

  <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>

I have tried to send the value of po.Email to the method. I thought the th:action="@{'/dashboard/makeAndSendInvoice(email=${po.Email})'}" will make a link like dashboard/makeAndSendInvoice/{Email}

So i tried to get it in the method like this:

 @RequestMapping(method=POST, path="makeAndSendInvoice")
    public String makeAndSendInvoice(@PathVariable("email") String email){

        System.out.println("Invoice is sent to..................."+email);
        return "Invoice";
    }

but it seems to me it does not work, since it does not recognize my method. So how can recieve the po.Email in my method

Upvotes: 1

Views: 98

Answers (1)

Tob&#237;as
Tob&#237;as

Reputation: 6287

Change th:action to:

th:action="@{/dashboard/makeAndSendInvoice/{email}(email=${po.Email})}"

and add the PathVariable in the RequestMapping value like:

@RequestMapping(method=POST, path="/dashboard/makeAndSendInvoice/{email:.+}")
public String makeAndSendInvoice(@PathVariable("email") String email) {

From 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>

From 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";
}

Upvotes: 1

Related Questions