如何在 JSP 中选中复选框 [英] How to get checked checkboxes in JSP

查看:35
本文介绍了如何在 JSP 中选中复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 jstl 获取/设置复选框值并仅从选中复选框的数据库中删除那些记录?对于这种情况,您能否还建议如何在 jstl 中使用三元运算符?

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

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>

谢谢.

推荐答案

您的复选框当前根本没有与参数名称关联的值:

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

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

所以很难找出被检查的.您需要为复选框指定一个唯一标识所选项目的值.在您的特定示例中,学生 ID 似乎是一个明显的选择:

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}"> 

(顺便说一句,为什么要在属性名称中复制实体名称?为什么不将其命名为 id 以便您可以自行记录使用 ${student.id}? 还有你的 var="students" 有点奇怪,它只指 一个 学生,所以把它命名为 var="student"; ${studentList} 最好命名为 ${students})

(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");

最后,将它传递给您的 DAO/服务类,它执行业务工作:

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

studentService.delete(selectedStudentIds);

另见:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆