首页 > 解决方案 > 春季 MVC CRUD 操作中的更新方法不起作用

问题描述

我正在尝试在 Spring MVC 控制器(CRUD 操作)中使用 RequestMethod.UPDATE 来编辑一个实际存在于我的表和 DB 中的对象(因为我首先使用我的添加按钮创建了一个)。当我单击更新按钮时,我得到:

此应用程序没有显式映射 /error,因此您将其视为后备。

2019 年 12 月 31 日星期二 12:34:11 EET 出现意外错误(类型=不允许方法,状态=405)。不支持请求方法“GET”

确保控制器中的后端代码写得很好,因为我使用 Postman(创建的 Json)进行测试,并且我在我的 DB(WorkBench)和我的表(localhost/html)中看到了可用的修改。我认为错误是由我的 html 代码(Thymeleaf)提供的。我将从后端和前端发布代码(更新方法)。

//Update from Controller
    @RequestMapping(value = "/listCarsUpdateOption")
    public String viewEditCar(Model model) {
            List<Car> listCars = carService.getAllCars();
            model.addAttribute("listCars", listCars);
            return "listCarsUpdateOption";
        }
    @RequestMapping(value="/edit/{id}", method = RequestMethod.POST)
    public ModelAndView showEditProductForm(@RequestBody Car car,@PathVariable(name = "id") Long id) {
        ModelAndView mav = new ModelAndView("listCarsUpdateForm");

        Car currentCar = carService.getCarById(id);
        currentCar.setModel(car.getModel());
        currentCar.setBrand(car.getBrand());
        currentCar.setColor(car.getColor());
        currentCar.setPrice(car.getPrice());
        currentCar.setYearofmanufacture(car.getYearofmanufacture());

        carService.saveCar(currentCar);
        mav.addObject("car", car);
        return mav;
    }
//HTML Form
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>CarList</title>
</head>
<body>
<style>
body
{
background-color: #cccccc;
}
h1
{
font-family: Comic Sans MS;
}
    .roundedTable td, th 
    {
        padding: 10px;
        font-family: Comic Sans MS;
        text-align:center;
    }
    .roundedTable th 
    {
        vertical-align: inherit;
        font-weight: bold;
        text-align: center;
        font-size: 20px;
    }
    .roundedTable
    {
        border-radius: 25px 25px 25px 25px;
        border: 10px;
        border-spacing: 0;
        background-color: #cccccc;  
        -webkit-box-shadow: 10px 15px 76px 5px rgba(0,0,0,0.75);
        -moz-box-shadow: 10px 15px 76px 5px rgba(0,0,0,0.75);
        box-shadow: 10px 15px 76px 5px rgba(0,0,0,0.75);

    }

    .roundedTable thead  
    {
        border:solid; 
        background-color:#8e8e8e;   
        font-size:25px;
    }

    .roundedTable tr:first-child td:first-child 
    {
        border-top-left-radius: 10px;

    }

    .roundedTable tr:first-child td:last-child 
    {
        border-top-right-radius: 10px;
    }

    .roundedTable th, td
    {
        padding: 10px 10px 10px 10px; 
    }

    .roundedTable tbody tr:nth-child(2n)
    {
        background-color: #eee;
    }
    button  {   
border: none;   
border-radius: 50%;       
font-size: 25px;        
cursor: pointer;    
color: white;    
background-color: #3d3d3d;    
box-shadow: 0 0 4px #999;    
outline: none;    
font-family: Comic Sans MS;
-webkit-box-shadow: 1px 5px 33px 4px rgba(0,0,0,1);
-moz-box-shadow: 1px 5px 33px 4px rgba(0,0,0,1);
box-shadow: 1px 5px 33px 4px rgba(0,0,0,1);
}

</style>
<div align="center">
<h1>UpdateCarList</h1>
<br/><br/>
<table class="roundedTable">
      <thead>
             <tr>
                 <td>CarId</td>
                 <td>Brand</td>
                 <td>Model</td>
                 <td>Color</td>
                 <td>YearOfManufacture</td>
                 <td>Price</td> 
                 <td>Action</td>             
             </tr>
      </thead>
      <tbody>
            <tr th:each="car:${listCars}">
            <td th:text="${car.id}">CarId</td>
            <td th:text="${car.brand}">Brand</td>
            <td th:text="${car.model}">Model</td>
            <td th:text="${car.color}">Color</td>
            <td th:text="${car.yearofmanufacture}">YearOfManufacture</td>   
            <td th:text="${car.price}">Price</td>   
            <td>
            <a th:href="@{'/edit/' + ${car.id}}">Edit</a>
            </td>     
      </tbody>
</table>
<br/><br/><br/><br/>
<form action="/homeCar">
      <p title='Click here to view the vehicles list'>
         <button action="/homeCar" type="submit" value="homeCar"/>Back</button>
      </p>
</form>
</div>
</body>
</html>

标签: htmlspring-mvcthymeleaf

解决方案


<a th:href="@{'/edit/' + ${car.id}}">Edit</a>

它是一个链接,您正在使用一个链接,该链接将触发一个“GET”方法,该方法在您的控制器代码中未声明,因此出现错误。一个适当的解决方案是使用表单而不是链接来发布或执行 Put 以在控制器中更新和处理它。

请看一下这个答案我可以在 Spring Boot 应用程序中从 Thymeleaf 表发出 HTTP POST 请求吗


推荐阅读