【问题标题】:Controller not processing Path Variable correctly - Spring boot控制器未正确处理路径变量 - Spring boot
【发布时间】:2020-09-26 01:29:22
【问题描述】:

我有一个有一些订单的客户列表,我将它们全部显示在一个表格中。 我添加了一个按钮,以便在需要时删除每个订单。

这是我如何显示它们的表格。

<table id="customers">
        <tr>
            <th>Customer</th>
            <th>Order</th>
            <th>Action</th>
        </tr>
        <div th:each="customer : ${customers}">
          <tr>
            <td th:text="${customer.name}" style="font-weight: bold;"></td>
            <td></td>
            <td></td>
          </tr>
          <tr th:each="order : ${customer.orders}" >
            <td></td>
            <td th:text="${order.name}">
            <td>
                <a th:href="@{/manager/customer/{cid}(cid=${customer.id})/order/{oid}(oid=${order.id})}" class="button">Remove order</a>
            </td>
          </tr>
        </div>
</table>

在我的 Controller 类中,我有处理删除的方法。

@GetMapping("/manager/customer/{cid}/order/{oid}")
    public String deleteCustomerOrder(@PathVariable(value="cid") Long cid, @PathVariable(value="oid") Long oid, Model model) {

        Customer c = customerService.findCustomerById(cid);
        //do stuff

        return "/manager/customerOrders";
    }

当我尝试删除特定订单时,出现以下错误

无法将“java.lang.String”类型的值转换为所需的“java.lang.Long”类型;嵌套异常是 java.lang.NumberFormatException: For input string: "{cid}(cid=${customer.id})"

路径如下所示: http://localhost:8080/manager/customer/%7Bcid%7D(cid=$%7Bcustomer.id%7D)/order/2

我在获取 ID 的方式上做错了吗?

【问题讨论】:

    标签: spring-boot thymeleaf path-variables get-mapping


    【解决方案1】:

    所有参数都应该一起定义。更改href如下:

    <a th:href="@{/manager/customer/{cid}/order/{oid}(cid=${customer.id}, oid=${order.id})}" class="button">Remove order</a>
    

    参见Thimeleaf documantationthis 教程中的添加参数部分

    【讨论】:

      猜你喜欢
      • 2020-09-25
      • 1970-01-01
      • 2014-01-24
      • 2017-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-05
      相关资源
      最近更新 更多