我正在使用 Java EE。
这是我的 JSP:
<form method="POST" enctype="multipart/form-data"action="<c:url value="submitSelected"/>">
<div class="box-body">
<table id="example" class="table table-bordered table-striped">
<thead>
<tr>
<th>id</th>
<th>name</th>
<th><input type="checkbox" id="checkall" /></th>
</tr>
</thead>
<tbody>
<c:forEach var="category" items="${categoryList}">
<tr>
<td><c:out value="${category.id}"/></td>
<td><c:out value="${category.name}"/></td>
<td><input type="checkbox" name="selectedCategory" value="${category.id}" /></td>
</tr>
</c:forEach>
</table>
</div>
<div class="box-footer" id="hidden-div">
<button type="submit" id="select" >submit</button>
</div></form>
这是我的 JS:
<script type="text/javascript">
$(document).ready(function () {
$("#example").dataTable({
"scrollX": true
});
$("#example #checkall").click(function () {
if ($("#example #checkall").is(':checked')) {
$("#example input[type=checkbox]").each(function () {
$(this).prop("checked", true);
});
} else {
$("#example input[type=checkbox]").each(function () {
$(this).prop("checked", false);
});
}
});
});</script>
我使用以下方法在我的 servlet 中获取我的复选框值:
String checkboxValues[] = request.getParameterValues("selectedCategory");
我在我的 JSP 中使用数据表。我的问题是如果我的 JSP 显示超过 1 页并且我从这些页面中选择了一些行,我的 servlet 只能从显示的页面中获取值。我如何从 servlet 中获取所有选定的值?如果我使用 checkall 复选框,我如何从 servlet 获取所有值?
最佳答案
JSP 是一种非常简单的技术。在 JSP 中完成的任何服务器端分页通常由程序员管理。 JSP 不会帮助您进行分页。
通常程序员会精心编排以使分页工作(比如传递参数,如 currentPage
、totalPages
、rowsPerPage
等,然后过滤一些将显示在页面上的行。它们还提供页码的超链接以及更多上一页 和下一页 链接的超链接)。
从您共享的代码片段来看,似乎显示了 categoryList
中的所有行。如果 categoryList
有 1000 行,则所有 1000 行将显示在同一页面中。
要使分页工作,应该有一些 servlet 或 struts Action
来管理 categoryList
。只有与当前页码相关的几行应该加载到该数组列表中。
考虑到所有这些,您可以理解,即使像“check-all”这样的东西也必须由开发人员使用相当多的编码来管理。
每当用户点击离开页面时,您可能必须将与所选复选框对应的类别 ID 附加到下一页的 URL,然后在服务器上维护所选类别 ID 的数组列表.如果用户执行新的搜索等等,您还必须清除此列表。
关于java - 从jsp中的数据表中获取选定复选框的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30319374/