从Thymleaf页面接收参数

时间:2016-05-03 11:27:23

标签: java thymeleaf

我试图从这个thymleaf页面发送一个参数

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

然后我尝试使用此代码

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

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

    }

问题是当p.email的值类似于myemail@gmail.com时 我在方法makeAndSendInvoice中收到的内容仅为myemail@gmail

并且它不会向我发送.com部分

我该如何解决?

3 个答案:

答案 0 :(得分:1)

(email=${p.email})表示您正在传递查询参数。 那么,如何使用路径变量捕获查询参数值?

我们可以使用@RequestParam来捕获Spring中的queryparam

尝试以下java代码:

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

答案 1 :(得分:0)

&#34; .com&#34;将需要编码以在URL中使用,以防止将其解析为后缀

答案 2 :(得分:0)

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

您的控制器

@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 !!
    }
}