我无法使用Web应用程序从列表中删除Car

时间:2019-10-21 18:30:45

标签: java spring thymeleaf

任何人都知道发生了什么事吗?为什么我不能删除我的车?

我在网页中遇到以下错误:发生意外错误(类型=错误的请求,状态= 400)。实际请求参数未满足参数条件“ idCar”:

如果我将以下地址写入Web浏览器栏中,这些代码将起作用:http://localhost:8080/cars-web/deleteCar?idCar=2

这是来自CarGui的代码,带有@Controller

@GetMapping(value = "/cars-web/deleteCar", params = "idCar")
public String deleteCar(@RequestParam long idCar) {
    carService.deleteCar(idCar);
    return "redirect:/cars-web";
}

这是我的CarService中的代码

@Override
public boolean deleteCar(long id) {
    Optional<Car> found = carList.stream().filter(car -> car.getCarId()== id).findFirst();

        if(found.isPresent()) {
            carList.remove(found.get());
            return true;
        } else {
            return false;
        }
}

这是我网页上的代码:

<form th:action="@{/cars-web/deleteCar}" th:object="${someId}" method="get">
<p>Id:<select>
    <option th:each="ids : ${readId2}"
            th:value="${ids}" th:text="${ids}"></option>
</select></p>
<p><input type="submit" value="Delete Car From List"></p>

另外我的模型

@GetMapping("/cars-web")
public String getCars(Model model) {
    model.addAttribute("carList", carService.getAllCars());
    model.addAttribute("readId", carService.getListId());
    model.addAttribute("readId2", carService.getListId());
    model.addAttribute("addCar", new Car());
    model.addAttribute("color", carService.getAllCarColors());
    model.addAttribute("someId", "");
    return "cars-web";
}

1 个答案:

答案 0 :(得分:0)

更改:

public String deleteCar(@RequestParam long idCar) {

收件人:

public String deleteCar(@PathVariable("idCar") long idCar) {

并更改:

@GetMapping(value = "/cars-web/deleteCar", params = "idCar")

收件人:

@GetMapping(value = "/cars-web/{idCar}")

您可以详细了解PathVariable和RequestParam HERE.

之间的区别