13

How can I get/set checkbox value using jstl and delete only those record from the database where the checkbox is checked? can you also advise how to use ternary operators in jstl for this scenario?

SearchStudent.jsp

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Lookup Students</title>
    </head>

    <form method="post" action="deleteStudentServlet" class="form">     
        <body class="body">

        <!-- List results -->

        <c:if test="${not empty studentList}">
            <table border="1" cellspacing="0" cellpadding="0" :>
                <tr>
                    <th></th>
                    <th>ID</th>
                    <th>Title</th>
                    <th>First Name</th>
                    <th>Last Name</th>
                    <th></th>
                </tr>
                <c:forEach var="students" items="${studentList}">
                    <tr>
                        <td><input type="checkbox" name="chkBox"> </td>
                        <td>${students.studentID}</td>
                        <td>${students.title}</td>
                        <td>${students.firstName}</td>
                        <td>${students.lastName}</td>
                        <td><c:url value="UDS" var="url">
                                <c:param name="StudentID" value="${students.studentID}" />
                            </c:url> <a href="${url}">Edit</a></td>
                    </tr>
                </c:forEach>
            </table>
        </c:if>

        <td><input type="submit" name="submit" value="Delete" ></td>
    </form>

        <p>There are ${fn:length(studentList)} results.</p>
    </body>
    </html>

thanks.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Muhammad
  • 611
  • 7
  • 14
  • 32

2 Answers2

19

Your checkbox has currently no value associated with the parameter name at all:

<input type="checkbox" name="chkBox">

So it's hard to find out the checked ones. You need to give the checkbox a value which uniquely identifies the selected item. In your particular example, the student ID seems to be an obvious choice:

<input type="checkbox" name="selected" value="${student.studentID}"> 

(by the way, why are you duplicating the entity name in the property name? why not just name it id so that you can just self-documentary use ${student.id}? also your var="students" is kind of odd, it is referring only one student, so just name it var="student"; the ${studentList} can better be named ${students})

When the form is submitted, all checked value are available as follows:

String[] selectedStudentIds = request.getParameterValues("selected");

Finally, just pass it through to your DAO/service class which does the business job:

studentService.delete(selectedStudentIds);

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • My other question is that how does it know to get only "checked" values using String[] selectedStudentIds = request.getParameterValues("selected"); – Muhammad Sep 13 '12 at 22:10
  • and what if I want to select "unchecked" values? – Muhammad Sep 13 '12 at 22:17
  • Are you familiar with basic HTTP and HTML? All HTML input fields send their values as HTTP request parameters. If you want to get unchecked values, just substract the checked values (the `selectedStudentIds`) from all available values (the `studentList`). – BalusC Sep 13 '12 at 22:42
  • Yes, i am new and learnig from experts like you. With regards to your answer, does it mean that when the checkbox is not selected, the value in it is null? – Muhammad Sep 13 '12 at 22:58
  • please can you provide an example of the subtraction? – Muhammad Sep 13 '12 at 23:08
  • An unchecked checkbox is not been sent as request parameter. Thus it does not end up in `selectedStudentIds`. As to substraction, just loop over all available items, get its ID and check if it's in the `selectedStudentIds`. If not, then it was not selected. – BalusC Sep 14 '12 at 12:34
-1

this may help you.

In ajax call:

var boolValue= $(this).closest(".tr").find('.checkboxClass').is(':checked');

$.post("/api/dosomething", {                                     
    someSettings : boolValue
})
Mebin Joe
  • 2,172
  • 4
  • 16
  • 22
Petar Ivanov
  • 91
  • 1
  • 3