Reputation: 35
Im using JSTL tags and EL to get the values out of my database. I added a checkbox to the data table on the U.I for the functionality of deleting a record. The problem is the String[] in the servlet always comes out as null and I have no idea why. I even tried dummy data in the 'value' attribute on the checkbox element and the array was still null.
HTML
<div id="authorTable" >
<table style="text-align: center;">
<thead>
<th>Book ID</th>
<th>Title</th>
<th>Date Published</th>
<th>Author ID</th>
<th>Author First Name</th>
<th>Author Last Name</th>
<th>Delete</th>
</thead>
<tbody>
<c:forEach var="record" items="${bookRecordsResult}">
<tr>
<td>${record.bookID}</td>
<td>${record.title}</td>
<td>${record.datePublished}</td>
<td>${record.author.firstName}</td>
<td>${record.author.lastName}</td>
<td>${record.author.id}</td>
This Line ------> <td> <input type="checkbox" name="boxes" id="boxes" class="boxes" value="${record.bookID}" /> </td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
<div id="deleteContainer">
<form id="deleteForm" method="POST" action="bookAuthorControls">
<button type="submit" id="deleteButton" name="deleteButton" >Delete</button>
</form>
</div>
Java Servlet
String delete = request.getParameter("deleteButton");
if (delete != null) {
And This Line ----> String[] checkValues = request.getParameterValues("boxes");
try {
service.deleteRecords(Arrays.asList(checkValues));
bookRecords = service.getAllBookRecords();
request.setAttribute("bookRecordsResult", bookRecords);
} catch (SQLException | ClassNotFoundException | ParseException ex) {
request.setAttribute("error", ex.getLocalizedMessage());
}
}
Once again the String[] comes back null no matter what I try, any ideas?
Upvotes: 0
Views: 1958
Reputation: 33010
The checkbox input elements are not contained in the form element which is submitted.
You need to wrap the form around all input elements:
<form id="deleteForm" method="POST" action="bookAuthorControls">
<div id="authorTable">
...
<td><input type="checkbox" name="boxes" id="boxes" class="boxes" value="${record.bookID}" /> </td>
...
</div>
<div id="deleteContainer">
<button type="submit" id="deleteButton" name="deleteButton" >Delete</button>
</div>
</form>
Upvotes: 1