DELETE and PUT method always call POST

Viewed 858

I have a problem and I don't know how to think about it anymore.

I have a window that shows a list of activity types in a table that loads from BD. I have several action buttons set for each row, including delete and edit. The problem that appears to me is that when I give any of the two buttons, they always call the POST method. I have both with the PUT and the DELETE but they do not go through there and always go to the POST.

I put the controller and html code to see if someone sees something that is being overlooked.

Class Tipo_Actividad_ServicioController:

@Controller
public class Tipo_Actividad_ServicioController {

    @Autowired
    private Tipo_Actividad_ServicioService tipo_Actividad_ServicioServcie;

    @RequestMapping(value = "/tipo_actividad_servicio", method = RequestMethod.GET) 
    public String findAll(Model model) {

        extracted(model);

        return "tipo_actividad_servicio";

    }

    // Guardar Tipo_Actividad_Servicio
    @RequestMapping(value = "/tipo_actividad_servicio", method = RequestMethod.POST) 
    public String save(Tipo_Actividad_Servicio tipo_Actividad_Servicio, Model model) {

        tipo_Actividad_ServicioServcie.save(tipo_Actividad_Servicio);

        extracted(model);

        return "tipo_actividad_servicio";

    }

    @RequestMapping(value = "/tipo_actividad_servicio", method = RequestMethod.PUT) 
    public String saveById(@RequestParam int id, Model model) {

        Tipo_Actividad_Servicio tipo_Actividad_Servicio = tipo_Actividad_ServicioServcie.findById(id);

        if(tipo_Actividad_Servicio.isActivo())
            tipo_Actividad_Servicio.setActivo(false);
        else
            tipo_Actividad_Servicio.setActivo(true);

        tipo_Actividad_ServicioServcie.save(tipo_Actividad_Servicio);

        extracted(model);

        return "tipo_actividad_servicio";

    }

    @RequestMapping(value = "/tipo_actividad_servicio", method = RequestMethod.DELETE) 
    public String deleteById(@RequestParam int id, Model model) {

        tipo_Actividad_ServicioServcie.deleteById(id);

        extracted(model);

        return "tipo_actividad_servicio";

    }

    private void extracted(Model model) {

        model.addAttribute("tipos_actividad_servicio", tipo_Actividad_ServicioServcie.findAll());
        model.addAttribute("tipo_actividad_servicio", new Tipo_Actividad_Servicio());

    }

}

And the HTML table that calls the delete method.

<tbody>
<tr th:each="familia,iterStat : ${tipos_actividad_servicio}" th:class="${iterStat.odd}? 'odd'">
  <td th:text="${familia.id}" style="display:false"></td>
  <td th:text="${familia.nombre}"></td>
  <td th:text="${familia.tipo}"></td>
  <td th:text="${familia.observaciones}"></td>
  <td style="display: flex; justify-items: center; align-items: center;">
    <div class="btn-group">
      <button type="button" class="text-white btn .btn-sm btn-warning" data-toggle="modal" data-target="#modal-lg">
          Editar
      </button>
      <form style="margin: 0px;" th:action="@{/tipo_actividad_servicio}" th:method="put">
        <input type="hidden" name="id" th:value="${familia.id}">
        <button class="text-white btn .btn-sm btn-success" th:unless="${familia.activo}" type="submit">
            Activar
        </button>
        <button class="text-white btn .btn-sm btn-secondary" th:if="${familia.activo}" type="submit">
            Desactivar
        </button>
      </form>
      <form style="margin: 0px;" th:action="@{/tipo_actividad_servicio}" th:method="delete">
        <input type="hidden" name="id" th:value="${familia.id}">
        <button class="text-white btn .btn-sm btn-danger" th:if="${familia.activo}" type="submit">
            Eliminar
        </button>
      </form>
    </div>
  </td>
</tr>
</tbody>

This is what the console shows me when it calls the method:

Hibernate: update tipo_actividad_servicio set activo=?, nombre=?, observaciones=?, tipo=? where id=?

When I inspect the form from firefox it tells me that the method is post and that it is going to call the PUT or DELETE ...

<form style="margin: 0px;" action="/tipo_actividad_servicio" method="post">
  <input type="hidden" name="_method" value="put">
  <input type="hidden" name="id" value="2">
  <button class="text-white btn .btn-sm btn-secondary" type="submit">
    Desactivar
  </button>
</form>

The truth is that I no longer know exactly what to do and what to try, if someone could tell me why it doesn't eliminate ...

Thank you.

1 Answers

If you are using Boot 2.3.X then make sure you have enabled the hidden http method filter, spring.mvc.hiddenmethod.filter.enabled=true. This filter is no longer enabled automatically as of 2.3.X. This filter will look at the _method attribute and direct the request to the appropriate controller method.

Related