我使用JSTL标签和EL从我的数据库中获取值。我在U.I的数据表中添加了一个复选框,用于删除记录的功能。问题是servlet中的String []总是出现为null,我不知道为什么。我甚至在'值'中尝试了虚拟数据。复选框元素上的属性和数组仍为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());
}
}
无论我尝试什么,任何想法,String []再次返回null?
答案 0 :(得分:1)
复选框输入元素在提交的表单元素中未包含。
您需要围绕所有输入元素包装表单:
<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>