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

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

问题描述

如何使用jstl获取/设置复选框值,并只删除数据库中复选框被选中的那些记录?您也可以建议如何在jstl中使用三元运算符来处理此情况?



SearchStudent.jsp

 <%@ taglib prefix =curi =http://java.sun.com/jsp/jstl/core%> 
<%@ taglib uri =http://java.sun.com/jsp/jstl/functionsprefix =fn%>

<%@ page contentType =text / htmlpageEncoding =UTF-8%>
<!DOCTYPE html>
< html>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = UTF-8>
< title>查找学生< / title>
< / head>

< form method =postaction =deleteStudentServletclass =form>
< body class =body>

<! - 列出结果 - >

< c:if test =$ {not empty studentList}>
< table border =1cellspacing =0cellpadding =0:>
< tr>
< th>< / th>
< th> ID< / th>
< th>标题< / th>
< th>名字< / th>
< th>姓氏< / th>
< th>< / th>
< / tr>
< c:forEach var =studentsitems =$ {studentList}>
< tr>
< td>< input type =checkboxname =chkBox> < / td>
< td> $ {students.studentID}< / td>
< td> $ {students.title}< / td>
< td> $ {students.firstName}< / td>
< td> $ {students.lastName}< / td>
< td>< c:url value =UDSvar =url>
< c:param name =StudentIDvalue =$ {students.studentID}/>
< / c:url> < a href =$ {url}>编辑< / a>< / td>
< / tr>
< / c:forEach>
< / table>
< / c:if>

< td>< input type =submitname =submitvalue =Delete>< / td>
< / form>

< p>有$ {fn:length(studentList)}结果。< / p>
< / body>
< / html>

解决方案

/ div>

您的复选框目前没有与参数名称相关联的值:

 < input type = checkboxname =chkBox> 

因此很难找出选中的选项。您需要给该复选框一个唯一标识所选项目的值。在您的特定示例中,学生ID似乎是一个明显的选择:

 < input type =checkboxname = selectedvalue =$ {student.studentID}> 

(顺便说一下,为什么要在属性名称中复制实体名称?不只是命名它 id ,以便你可以只是自我纪录使用 $ {student.id} code> var =students很奇怪,它只指一个学生,所以只是命名 var =student > ; $ {studentList} / em>



提交表单时,所有检查的值如下:

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

最后,只需将它传递给您的DAO /服务类, >

  studentService.delete(selectedStudentIds); 


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.

解决方案

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

这篇关于如何在JSP中选中复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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