I work with a Spring boot and JSP project and the UI is provided below,
I would like to delete the row entries using the delete button. The page list.jsp
loads with the controller method,
@GetMapping(value = "/")
public String index() {
return "redirect:/users";
}
@GetMapping(value = "/users")
public String showAllUsers(Model model) {
model.addAttribute("users", userService.findAll());
return "list";
}
The JSP part for the buttons are in the list.jsp
page,
<td>
<spring:url value="/users/${user.id}" var="userUrl"/>
<button class="btn btn-info" onclick="location.href='${userUrl}'">Query</button>
<spring:url value="/users/${user.id}/delete" var="deleteUrl"/>
<button class="btn btn-danger" onclick="this.disabled=true;removeUser()">Delete</button>
<spring:url value="/users/${user.id}/update" var="userUpdate"/>
<button class="btn btn-info" onclick="location.href='${userUpdate}'">Update</button>
</td>
The controller mapping for the deletion,
@DeleteMapping(value = "/users/{id}/delete")
public String deleteUser(@PathVariable("id") Long idx, final RedirectAttributes redirectAttributes) {
logger.debug("Delete user with Id {}", idx);
redirectAttributes.addFlashAttribute("css", "Success");
redirectAttributes.addFlashAttribute("msg", "The user is deleted");
// delete the user
userService.delete(idx);
return "redirect:/users/";
}
I have the JavaScript function in the custom file,
function removeUser(){
}
and, after clicking the delete button, I can get some console output, which means the delete button is working. How should I implement the removeUser
function that will delete the rows after clicking the button?
I would like to know if I can do this with any built-in methods from the jQuery
as well. Thanks.
The Query
and the Update
buttons have respective controller methods and work properly.
If I leave it just like
<spring:url value="/users/${user.id}/delete" var="deleteUrl"/>
it doesnt delete the row or do anythong
Update
I can pass the delete URL to the custom JavaScript function,
<spring:url value="/users/${user.id}/delete" var="deleteUrl"/>
<button class="btn btn-danger" onclick="this.disabled=true;removeUser('${deleteUrl}')">Delete</button>
The removeUser
fucntion provided,
function removeUser(deleteURL) {
console.log("Remove : " + deleteURL);
$.ajax({
type: "DELETE",
url: deleteURL,
success: function () {
},
failure: function (errMsg) {
console.log(errMsg.toString())
}
});
}
I have tried to write the success function, not working though