SOURCE :EXPERT EXCHANGEGiven the zone and the phrasing of the question, I understand this to be a question about a JSP web application, rather than a Swing or AWT-based desktop application. If that's the case, here's how I would approach it.
First of all, to clarify the assumptions I'm making, I'll explain my understanding of your application. You are paging through a set of records, and each record has as one of its elements a checkbox. As the pages through the records, he can check some items, indicating that they should be acted on in some way. At some point, the user has checked all the desired records on several pages, and wants to submit them all at once for some action.
I'm assuming that each record has some sort of unique identifier, and when the user has browsed through the record set and checked all of the necessary records, the submit action can use these unique identifiers to process the correct records. I'm assuming you're using EL and JSTL tags, and not scriptlets, since that's the best practice. In the example code, you can change the "name" attributes on the form controls; I'm just using what seems natural to me. Same goes for EL variable names. If you need clarification, please ask. I'm not sure what framework you're using on the server side, so I'll just show the standard servlet API implementation. It should be easy to adapt that to whatever framework one might use, but that's outside the scope of this question.
Here's a skeleton of the form I'd use:
<c:url var="action" value="/viewpage" /><form
action="${action}"
method="post"><fieldset><table><tbody>
<c:forEach items="${records}" var="rec">
<tr>
<td>
<!-- Display the
records fields as desired --><c:out value="${rec.id}" />
</td>
<td>
<!-- Here's
the crucial part... -->
<input type="hidden"
name="records" value="${rec.id}" />
<c:choose>
<c:when test="${sessionScope.myCheckBoxes[rec.id]}">
<checkbox checked="checked" name="checks" value="${rec.id}" />
</c:when>
<c:otherwise>
<checkbox name="checks" value="${rec.id}" />
</c:otherwise>
</c:choose>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</fieldset>
<fieldset>
<button type="submit" name="page"
value="${page - 1}">
Previous
</button>
<button type="submit" name="page" value="${page + 1}">
Next
</button>
<button type="submit"
name="process" value="true">
Done
</button>
</fieldset>
</form>
Then, on the
servlet (mapped to /viewpage) , doPost would contain something like this:
protected void doPost(HttpServletRequest req, HttpServletResponse res){
...String[] records = req.getParameterValues("records");
if
(records != null)Set checks = new HashSet();
String[] tmp =
req.getParameterValues("checks");
if (tmp != null) {
for (int idx = 0;
idx < tmp.length; ++idx)
checks.add(tmp[idx]);
}
Map
myCheckBoxes = (Map) req.getSession().getAttribute("myCheckBoxes");
if
(myCheckBoxes == null)
myCheckBoxes = new HashMap();
for (int idx =
0; idx < records.length; ++idx) {
if (checks.contains(records[idx]))
myCheckBoxes.put(records[idx], Boolean.TRUE);
else
myCheckBoxes.remove(records[idx]);
}
...
if
(Boolean.valueOf(req.getParameter("process")).booleanValue()) {
/* The
user pressed the "Done" button; do your work here. */
Iterator recIds =
myCheckBoxes.keySet().iterator();
while (recIds.hasNext()) {
String id =
(String) recIds.next();...
}
/* Clear the check state from the session
(probably, depends on your requirements. */
req.getSession().removeAttribute("myCheckBoxes");
}
else {
req.getSession().setAttribute("myCheckBoxes", myCheckBoxes);
}
...
}
NOTE : Sorry, I missed some braces in the code; the block guarded by "if (records != null)" should extend to the end of the "for" loop over "records"
|