Error in DELETE method, to delete a data from a table

1

I'm trying to make a method to delete an entry from a table with Java and jsp , but it's giving 405 error. I'll append the codes.

ProductDao.java:

public void remove(Produto produto) {
    em.getTransaction().begin();
    em.remove(busca(produto));
    em.getTransaction().commit();
}

public Produto busca(Produto produto) {
    return em.find(Produto.class, produto.getId());
}

ProductController.java:

@Delete
public void remove(Produto produto){
    dao.remove(produto);
    result.redirectTo(this).lista();
}

lista.jsp:

<div class="row">
    <form action="<c:url value='/produto/remove'/>" method="DELETE">
        <div class="col s4">
            REMOVER PELO ID:<input type="text" />
        </div>
        <div class="col s4 right">
            <input type="submit" class="btn waves-effect waves-light btn-large right" value="REMOVER"></input>
        </div>
    </form>
</div>

And the error that appears is as follows:

  

HTTP Status [405] - [Method Not Allowed]

     

Type Status Report

     

Description The method received in the request-line is known by the   origin server but not supported by the target resource.

    
asked by anonymous 20.06.2017 / 21:59

2 answers

1

The method is right. The error was in the JSP form. The correct one is as follows:

<form action="<c:url value='/produto/remove?id=${produto.id}'/>" method="POST">
        <input type="hidden" name="_method" value="DELETE" />
    <div class="col s4 right">
        <input type="submit" class="btn waves-effect waves-light btn-small right" value="REMOVER"></input>
    </div>
</form>

Then I passed the button next to the line in the table, so it would pick up the automatic id. Later I can make an input text to pass the id to remove, but at first I'll leave it like this.

    
21.06.2017 / 17:28
0

What is the path of the controller? From what I've seen, you have to check if the @Path on the controller is correct and the delete method is correct. Try to use @DELETE, it's easier to read.

    
20.06.2017 / 23:16