简体   繁体   中英

JSP getParameterValues Returns Null

Im using JSTL tags and EL to get the values out of my database. I added a checkbox to the data table on the UI for the functionality of deleting a record. The problem is the String[] in the servlet always comes out as null and I have no idea why. I even tried dummy data in the 'value' attribute on the checkbox element and the array was still null.

HTML

             <div id="authorTable" >
            <table style="text-align: center;">

                <thead>
                <th>Book ID</th>
                <th>Title</th>
                <th>Date Published</th>
                <th>Author ID</th>
                <th>Author First Name</th>
                <th>Author Last Name</th>
                <th>Delete</th>
                </thead>

                <tbody>
                    <c:forEach var="record" items="${bookRecordsResult}">
                        <tr>
                            <td>${record.bookID}</td>
                            <td>${record.title}</td>
                            <td>${record.datePublished}</td>
                            <td>${record.author.firstName}</td>
                            <td>${record.author.lastName}</td>
                            <td>${record.author.id}</td>
          This Line ------> <td> <input type="checkbox" name="boxes" id="boxes" class="boxes" value="${record.bookID}" /> </td>
                        </tr>
                    </c:forEach>
                </tbody>
            </table>
        </div>

        <div id="deleteContainer">
            <form id="deleteForm" method="POST" action="bookAuthorControls">
                <button type="submit" id="deleteButton" name="deleteButton" >Delete</button>
            </form>
        </div>

Java Servlet

  String delete = request.getParameter("deleteButton");

    if (delete != null) {
       And This Line ---->  String[] checkValues = request.getParameterValues("boxes");

        try {
       service.deleteRecords(Arrays.asList(checkValues));
            bookRecords = service.getAllBookRecords();
            request.setAttribute("bookRecordsResult", bookRecords);
        } catch (SQLException | ClassNotFoundException | ParseException ex) {
            request.setAttribute("error", ex.getLocalizedMessage());
        }
    }

Once again the String[] comes back null no matter what I try, any ideas?

The checkbox input elements are not contained in the form element which is submitted.

You need to wrap the form around all input elements:

<form id="deleteForm" method="POST" action="bookAuthorControls">
    <div id="authorTable">
         ...
         <td><input type="checkbox" name="boxes" id="boxes" class="boxes" value="${record.bookID}" /> </td>
         ...
    </div>

    <div id="deleteContainer">
        <button type="submit" id="deleteButton" name="deleteButton"       >Delete</button>
    </div>
</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM