【发布时间】:2020-09-25 03:04:35
【问题描述】:
我在控制器类中遇到以下方法的重定向问题。
当我点击提交按钮时,它不会将我重定向到http://localhost:8080/manager/customers,而是重定向到http://localhost:8080/customer/1/manager/customers
注意:1 是我选择添加订单的客户 ID
我是不是做错了什么??
@PostMapping(value = "/customer/{id}/orders")
public String projectAddOrders(@PathVariable("id") Long customerId, @RequestParam Long orderId, Model model) {
Order order = orderService.findOrderById(orderId);
Customer customer = customerService.findCustomerById(customerId);
if (customer != null) {
if (!customer .hasOrder(order)) {
customer .getOrders().add(order);
}
customerService.saveCustomer(customer );
model.addAttribute("customer", customerService.findCustomerById(customer Id));
model.addAttribute("orders", orderService.getAllOrders());
return "redirect:manager/customers";
}
return "redirect:manager/customers";
}
这是来自的 HTML:
<form th:action="@{/customer/{id}/orders(id=${customer.id})}" method="post">
<div class="row">
<div class="col-25">
Customer name: <b th:text="${customer.name}" /><br/>
</div>
</div>
<div class="row">
Customer orders:
<b><span th:each="order, iterStat : ${customer.orders}">
<span th:text="${order.name}"/><th:block th:if="${!iterStat.last}">,</th:block>
</span></b>
</div>
</div>
<br/>
<div class="row">
<div class="col-25">
<label for="user">Add Order</label>
</div>
<div class="col-75">
<select name="orderId">
<option th:each="order: ${orders}"
th:value="${order.id}"
th:text="${order.name}">
</option>
</select>
</div>
</div>
<div class="row">
<input type="submit" value="Add Order">
</div>
</form>
【问题讨论】:
标签: forms spring-boot redirect controller thymeleaf