Making a Spring Boot + Thymeleaf application.
I'm trying to pass a list of IDs of the type Integer to my controller based on whether is it checked on a form, in order to delete one or more rows of data from my db table.
However, right now it returns an empty list.
The ID is the primary key of the table.
The controller seems to be working fine because when I manually add an Integer to the list - it can be deleted with no problem.
This is my HTML file
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Profile</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<!-- Custom styles for this template -->
<link rel="stylesheet" type="text/css" th:href="@{/css/offcanvas.css}">
</head>
<div class="col-9 col-lg-10">
<form th:action="@{/users/profile/history/deldata}" th:object="${logDeleteForm}" method="post">
<!-- <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>-->
<div class="container-fluid text-center">
<div>
<table class="table table-striped w-75">
<thead>
<tr class="bg-dark text-light">
<th>Select</th>
<th>Date</th>
<th>Country</th>
<th>City</th>
</tr>
</thead>
<tbody>
<tr th:each="userData: ${userData}">
<td><input type="checkbox"
th:name="ids"
th:value="${userData.id}"/>
</td>
<td th:text="${userData.datetime}"></td>
<td th:text="${userData.country}"></td>
<td th:text="${userData.city}"></td>
</tr>
</tbody>
<input type="submit" value="Delete tests" id="deleteinput"/>
</table>
</div>
</div>
</form>
</div>
<!--</div>-->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
</body>
</html>
My Controller
@PostMapping("/users/profile/history/deldata")
public String deleteUserData(@ModelAttribute("ids") LogDeleteForm logDeleteForm, Model model) {
System.out.println(logDeleteForm.getIds());
List<Integer> ids = logDeleteForm.getIds();
if (ids == null || ids.size() == 0) {
return "redirect:/users/profile/history";
}
for (Integer id : ids) {
userDataService.deleteUserDataById(id);
}
return "redirect:/users/profile/history";
}
And my FormObject classs
public class LogDeleteForm {
private List<Integer> ids = new ArrayList<>();
public LogDeleteForm() {}
public LogDeleteForm(List<Integer> ids) {
this.ids = ids;
}
public List<Integer> getIds() {
return ids;
}
public void setLogIds(List<Integer> logIds) {
this.ids = ids;
}
}