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

Asked

Viewed 347 times

1

I am trying to make a method to delete an entry from a table with Java and jsp, but is giving error 405. I will attach the codes.

Productodao.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());
}

Productocontroller.java:

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

jsp list.:

<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 this:

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.

2 answers

1


The method is correct. The error was in the JSP form. The correct one is the following:

<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 on the table, so it picked up the automatic id. Later I can do an input text to pass the id to remove, but at first I’ll leave it like this.

0

What is the controller path? From what I saw you have to check that the @Path that is in the controller is correct and the delete method is correct. Try using @DELETE, it’s easier to read.

  • The path is the standard of the convention, but I found the error, it was just a difference of reading from the browser in question to the delete method

Browser other questions tagged

You are not signed in. Login or sign up in order to post.