Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
887 views
in Technique[技术] by (71.8m points)

checkbox - How to get checked checkboxes in JSP

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...