Jeff
Jeff

Reputation: 8411

Receive parameters from Thymleaf page

I have tried to send a parameter from this thymleaf page

<table class="table table-bordered table-striped">
    <thead>
        <tr>
            <th>Email</th>
            <th>Send Invoice</th>
        </tr>
    </thead>
    <tbody>
        <tr class="table-row" th:each="p : ${POList}">
            <td th:text="${p.email}"></td> 
            <td>
            <form style='float:left; padding:5px; height:0px' th:object="${p}" th:method="post" th:action="@{/dashboard/makeAndSendInvoice/{email}(email=${p.email})}">

                <button class="btn btn-default btn-xs" type="submit">Send Invoice</button>
            </form>
            </td>
        </tr>
    </tbody>

and then I tried to receive the parameter (email) using this code

@RequestMapping(method=POST, path="/makeAndSendInvoice/{email}")
public void makeAndSendInvoice(@PathVariable("email") String email) throws Exception {

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

    }

The problem is when the value of p.email is something like [email protected] what I receive in the method makeAndSendInvoice is only myemail@gmail

and it does not send me the .com part

How can I fix it?

Upvotes: 0

Views: 102

Answers (3)

0gam
0gam

Reputation: 1423

Your HTML:

<table class="table table-bordered table-striped">
<thead>
    <tr>
        <th>Email</th>
        <th>Send Invoice</th>
    </tr>
</thead>
<tbody>
    <tr class="table-row" th:each="p : ${POList}">
        <td th:text="${p.email}"></td> 
        <td>
        <form method="post" th:action="@{/dashboard/makeAndSendInvoice/__${p.email}__} ">

            <button class="btn btn-default btn-xs" type="submit">Send Invoice</button>
        </form>
        </td>
    </tr>
</tbody>

Your controller

@RequestMapping(value = "/dashboard")
@Controller
public class testController {

    ...

    @RequestMapping(value = "/makeAndSendInvoice/{email}", method = RequestMethod.POST)
    public ModelAndView makeAndSendInvoice(@PathVariable String email, ModelAndView mav) {
       return mav;  // <- Break point, debug !!
    }
}

Upvotes: 0

sitakant
sitakant

Reputation: 1878

(email=${p.email}) means , you are passing a query parameter. So, how can you be able to catch the query param value using path variable?

We can use @RequestParam to catch the queryparam value in Spring

Try the below java code :

@RequestMapping(method=POST, path="/makeAndSendInvoice/{email}")
public void makeAndSendInvoice(@RequestParam("email") String email) throws Exception {
        System.out.println("Invoice is sent to..................."+email);
}

Upvotes: 1

SelketDaly
SelketDaly

Reputation: 587

The period in ".com" will need to be encoded to be used in the url to prevent it being parsed as the suffix

Upvotes: 0

Related Questions