You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring视图传递Car对象时触发404错误,请求解决方案

问题解决:视图传递车辆ID到控制器并修复404错误

一、404错误的核心原因

你的404错误源于表单提交路径与控制器映射不匹配:

  • 控制器类上标注了@RequestMapping("/car-list"),orderVehicle方法又加了@PostMapping("/order"),所以完整的请求路径是/car-list/order。
  • 但视图里的表单action写的是@{/order},找不到对应的控制器方法,直接触发404。

二、修改视图(car-list.html)

只需要传递车辆的vehicleID即可,无需传递整个Car对象,修改后的代码如下:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
  <meta charset="UTF-8">
  <title>Vehicle List</title>

  <h1>Location List</h1>

  <h2 th:text="${location1.locationName}">Locations</h2>
  <h3>Vehicle List</h3>
  <table class="table table-stripped">
    <tr th:each="vehicle : ${location1vehicles}">
      <td th:text="${vehicle.name}">Vehicle Name</td>
      <td th:text="${vehicle.BodyType}">Vehicle Body Style</td>
      <td th:text="${vehicle.EngineSize}">Vehicle Engine Size</td>
      <td th:text="${vehicle.fuel}">Vehicle Fuel Type</td>
      <td th:text="${vehicle.SeatCapacity}">Vehicle Seat Capacity</td>
    </tr>
  </table>

  <h4>Select Vehicle</h4>
  <!-- 修正表单提交路径,移除无用的th:object -->
  <form th:action="@{/car-list/order}" method="post">
    <select name="SelectVehicle" id="SelectVehicle">
      <option value="0">Select Vehicle</option>
      <!-- 下拉框value设置为vehicleID,而非整个Car对象 -->
      <option th:each="vehicle : ${location1vehicles}"
              th:value="${vehicle.vehicleID}"
              th:text="${vehicle.name}"></option>
    </select>
    <!-- 移除无意义的th:value属性 -->
    <button type="submit">Order Vehicle</button>
  </form>

</head>
</html>

关键修改点:

  1. 表单th:action改为@{/car-list/order},匹配控制器的完整映射路径。
  2. 下拉框的th:value设置为${vehicle.vehicleID},提交时仅传递主键ID。
  3. 移除表单的th:object="${car}"(你未在model中添加该属性,且无需提交整个对象)和按钮的th:value。

三、修改控制器(CarViewController.java)

直接接收vehicleID参数,再用ID查询对应的Car对象即可:

@Controller
@RequestMapping("/car-list")
public class CarViewController {

    @Autowired
    private CarService carService;

    @GetMapping
    public String carList(Model model) {

        Location limerick = new Location("Limerick");
        Location cork = new Location("Cork");

        //Drop down/table lists per location
        ArrayList<Car> location1cars = new ArrayList<>();
        ArrayList<Car> location2cars = new ArrayList<>();

        ArrayList<Car> cars = (ArrayList<Car>) carService.getAllCars();

        for (int i = 0; i < cars.size() / 2; i++) {
            location1cars.add(cars.get(i));
        }
        for (int i = cars.size() / 2; i < cars.size(); i++) {
            location2cars.add(cars.get(i));
        }

        model.addAttribute("location1", limerick);
        model.addAttribute("location1vehicles", location1cars);

        return "car-list";
    }

    // 修改参数为接收vehicleID,根据ID查询目标车辆
    @PostMapping("/order")
    public String orderVehicle(@RequestParam("SelectVehicle") Integer vehicleID, Model model) {
        // 通过ID从数据库查询选中的车辆(确保CarService实现了该方法)
        Car selectedCar = carService.getCarById(vehicleID);
        System.out.println(selectedCar.getVehicleID());
        System.out.println("Test");
        
        // 如需将车辆信息传递到order视图,可添加到model
        model.addAttribute("selectedCar", selectedCar);
        return "order";
    }
}

关键修改点:

  1. 方法参数改为@RequestParam("SelectVehicle") Integer vehicleID(若你的vehicleID是Long类型,替换为Long即可),直接接收提交的主键ID。
  2. 通过carService.getCarById(vehicleID)从数据库查询用户选中的车辆。
  3. 移除无用的@ModelAttribute Car car参数。

四、验证效果

现在点击提交按钮,会正确请求/car-list/order路径,控制器接收vehicleID后查询对应的Car对象,不会再出现404错误,同时你也能拿到用户选中的车辆信息。

内容的提问来源于stack exchange,提问作者Collander

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 12:55:25