Search code examples
htmlspring-mvcthymeleaf

UPDATE METHOD IN SPRING MVC CRUD OPERATION NOT WORK


I'm trying to use RequestMethod.UPDATE in Spring MVC controller ( CRUD operation ) to edit an object who is actually stock in my table and DB(because i create one first with my add button).When i click to Update button i get :

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Tue Dec 31 12:34:11 EET 2019 There was an unexpected error (type=Method Not Allowed, status=405). Request method 'GET' not supported

For sure the back-end code in controller is write fine because i test with Postman ( created Json ) and I see the modification available in my DB(WorkBench) and in my table (localhost/html). I think the error is provide from my html code ( Thymeleaf ).I will post the code from backEnd and frontEnd (UPDATE METHOD).

//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>

Solution

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

    its a link, you are using a link, that will trigger a "GET" Method, which in your controller code its not declared, therefore the error. A proper solution would be to use a form instead of a link to post or a to do a Put to update and handle it in controller.

    please take a look at this answer Can I make HTTP POST request from Thymeleaf table in Spring Boot application