1

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

Jeff
  • 7,767
  • 28
  • 85
  • 138

1 Answers1

1

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";
}
Tobías
  • 6,142
  • 4
  • 36
  • 62
  • when i print the email value in the method it has a value like this **{email}(email=${p** – Jeff May 01 '16 at 09:19
  • 1
    I've done a typo. The path in @{...} should be unquoted. Edited. – Tobías May 01 '16 at 16:57
  • Thanks it is working, but when i sent sth like **salman@ut.ee** it does not consider **.ee** part, it does not consider characters after the dot – Jeff May 03 '16 at 11:09
  • That's a Spring MVC matter. Here you can find a detailed explanation: [Spring MVC @PathVariable with dot (.) is getting truncated](http://stackoverflow.com/questions/16332092/spring-mvc-pathvariable-with-dot-is-getting-truncated) – Tobías May 03 '16 at 11:56