I'm trying to call a delete method with a button in a Freemarker template. I thought it would be as simple as defining a path for that method in my resource class, and then pointing the action of the form to that path; however, nothing happens when I press the button.
Here is the code for the button:
<form action="http://localhost:8080/person/delete/${person.id}">
<input type="button" value="Delete"></form>
In theory, this is supposed to activate a method that sends a delete request to the SQL server. The original method I used for this is:
@DELETE
@Path("/delete/{id}")
public void deletePerson(@PathParam("id") int id) {
manager.deletePerson(id);
}
This uses the DAO to send the request. I tested this in Postman and it works. I thought that I could just point the button down the same path to use it, but that did not work. So instead, I tried modifying the method to be used by the constructor for the ftl template:
@DELETE
@Path("/delete/{id}")
public PersonView deletePerson(@PathParam("id") int id) {
return new PersonView(manager.deletePerson(id));
}
But this runs into the issue that I can't apply PersonView, which returns a list of persons, to deletePerson, which is a void method. Does anyone have any insight on how I can do this? It looks like it should be simple, but I can't figure it out.
Edit:
In addition to user7294900 answer regarding the submit button, I also needed to make a change regarding the delete method. In researching the issue, I also found that html forms cannot work with DELETE or PUT methods; only POST and GET. With this in mind, I changed the resource method to use @POST annotation:
@POST
@Path("/delete/{id}")
public void deletePerson(@PathParam("id") int id) {
manager.deletePerson(id);
}
I also made the necessary submit change to the form button:
<form method="post" action="http://localhost:8080/person/delete/${profile.id}">
<input type="submit" value="Delete"></form>
It seems that your issue is HTML input type which should be
submit
type in order to submit your formSo change your button to: