How to get checked checkboxes in JSP

前端 未结 2 1883
故里飘歌
故里飘歌 2020-12-07 01:07

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

2条回答
  •  盖世英雄少女心
    2020-12-07 01:44

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

    
    

    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:

     
    

    (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:

    • How to transfer data from JSP to servlet when submitting HTML form
    • ServletRequest.getParameterMap() returns Map and ServletRequest.getParameter() returns String?
    • Send an Array with an HTTP Get

提交回复
热议问题