【问题标题】:Spring boot controller not redirecting correctlySpring Boot 控制器未正确重定向
【发布时间】: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


    【解决方案1】:

    试着转动

    return "redirect:manager/customers";
    

    进入

    return "redirect:/manager/customers"; 
    

    注意“redirect:”和“manager”之间的斜线。 有用吗?

    【讨论】:

    • 不行,还是不行。我注意到它对 @RequestMapping 中带有 has 和 {id} 的每个方法都执行此操作
    • 添加订单是否正确?此外,这将有助于查看您的课程级别请求映射(在公共课程之前......)和经理/客户@GetMapping。
    • 是的,它正确添加了订单。我没有班级级别的映射。 @GetMapping(value = "manager/customers") public String getAllCustomersForManager(Model model) { //do stuff return "manager/customers"; }
    • 真的,尝试在“manager\customers”之前添加斜杠“\”。没有它,您将重定向到您以前的 URI + 新的 URI,即您不想要的 localhost:8080/customer/1/manager/customers
    • 是的,我明白你在说什么,我想你是对的,我也在某处读过它。但这两种方式都不起作用,所以我不确定
    【解决方案2】:

    我通过添加修复它

    registry.addViewController("**/manager/customers").setViewName("redirect:/manager/customers");
    

    在我的 ApplicationWebMvcConfigurerAdapter 实现 WebMvcConfigurer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-03
      • 2016-01-24
      • 2020-09-26
      • 2016-08-29
      • 2021-01-06
      • 2017-04-14
      • 2018-02-11
      相关资源
      最近更新 更多