试图在thymeleaf / spring-boot中通过@PathVariable传递两个对象

时间:2017-06-06 01:31:00

标签: java spring thymeleaf

我正在尝试使用@PathVariable注释提取两个参数,但由于java向我抛出了一个关于它引用项目ID(我当前有一个Long)作为字符串的错误而导致失败。

控制器:

@PostMapping("/purchaseToner/{tid}/{bid}")
    public String buyToner(Model model, @PathVariable("tid") Long tid,
                           @PathVariable("bid") Long bid){


        //Grab info
        Buyer mBuyer = buyerService.findOne(bid);
        Toner mToner = tonerService.findOneToner(tid);

        //Updating qualities
        mToner.setTonerQuantity(mToner.getTonerQuantity() - 1);
        mBuyer.setBalance(mBuyer.getBalance() - mToner.getTonerPrice());

        Buyer iBuyer = new Buyer();
        iBuyer.getToners().add(mToner);

        return "redirect:/";
    }

查看:

 <form th:action="@{/purchaseToner/{tid}(tid=${toner.id})/{bid}(bid=${buyer.buyerId})}" th:object="${buyer}" method="post">
                       <select th:object="${toner}">
                            <option>Select a Toner</option>
                            <option th:each="toner : ${toners}"
                                    th:text="${toner.tonerName}"
                                    th:value="${toner.id}">
                            </option>
                       </select>
                        <input type="hidden" name="buyerId"/>
                        <input type="submit" value="Purchase" onclick="return confirm('Are you sure you want to make this purchase?')"/>
                    </form>

打印跟踪:

2017-06-05 21:10:59.418  INFO 788 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-06-05 21:10:59.517  INFO 788 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-06-05 21:10:59.527  INFO 788 --- [           main] com.ronone.Application                   : Started Application in 18.596 seconds (JVM running for 19.548)
2017-06-05 21:11:06.438  INFO 788 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2017-06-05 21:11:06.439  INFO 788 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2017-06-05 21:11:06.470  INFO 788 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 31 ms
2017-06-05 21:11:09.673  INFO 788 --- [nio-8080-exec-2] o.h.h.i.QueryTranslatorFactoryInitiator  : HHH000397: Using ASTQueryTranslatorFactory
2017-06-05 21:11:15.837  WARN 788 --- [nio-8080-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to bind request element: org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "{tid}(tid=${toner.id})"

3 个答案:

答案 0 :(得分:0)

将行动改为以下行动

<form th:action="@{/purchaseToner/{tid}/{bid}(tid=${toner.id},bid=${buyer.buyerId})}" th:object="${buyer}" method="post">

此外,您需要在弹簧模型中添加碳粉。在您的代码中,它不在模型中

答案 1 :(得分:0)

您的api buyToner 尚未接收路径变量中的输入数字。你的api服务没有任何问题;检查您的客户端代码,是否以适当的格式发送请求您是否使用任何restclient测试了您的api?

查看java异常

java.lang.NumberFormatException: For input string: "{tid}(tid=${toner.id})"

这里的输入不是数字。为什么?

答案 2 :(得分:0)

尝试更改此内容:

    <div class="center">
        <h2> Grad Apparels </h2>
        <table>
            <thead>
                <tr> 
                    <th></th>
                    <th> ID Number </th>
                    <th> Full Name </th>
                    <th> Payments </th>
                    <th> Picked Up </th>
                    <th> Returned </th>
                    <th> Completed </th>
                    <th></th>
                </tr>
            </thead>
            <tbody data-bind="foreach: apparels">
                <tr>
                    <td data-bind="text: ($index() + 1)"> </td>
                    <th data-bind="text: id"></th>
                    <th data-bind="text: name"></th>

                    <th> <input type="checkbox" data-bind="checked: payments"></th>  
                    <th> <input type="checkbox" data-bind="checked: pickedUp"></th>  
                    <th> <input type="checkbox" data-bind="checked: returned"></th>  
                    <th> <input type="checkbox" disabled data-bind="checked: allChecked "> </th>
                    <td> <a href="#" data-bind="click: $parent.remove">Remove</a></td>
                </tr>
            </tbody>
        </table>
        <br>
        <p> ID: <input data-bind="value: id"> </p>

        <p> Full Name: <input data-bind="value: name"></p>
        <button data-bind="click: add">ADD</button>
    </div>      

进入:

@PostMapping("/purchaseToner/{tid}/{bid}")
    public String buyToner(Model model, @PathVariable("tid") Long tid,
                           @PathVariable("bid") Long bid){
...
    }

换句话说,将控制器的参数更改为String类型,然后将它们转换为Long。