【问题标题】:JSP getParameterValues Returns NullJSP getParameterValues 返回 Null
【发布时间】:2015-10-01 04:24:37
【问题描述】:

我正在使用 JSTL 标记和 EL 从我的数据库中获取值。我在 UI 上的数据表中添加了一个复选框,用于删除记录的功能。问题是 servlet 中的 String[] 总是显示为 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());
        }
    }

无论我尝试什么,String[] 再次返回 null,有什么想法吗?

【问题讨论】:

  • 您在提交表单之前checked 复选框了吗?
  • @Arvind 是的-我什至为所有人设置了“checked='checked'”属性并尝试提交它并返回 null。
  • 问题似乎与表单标签中损坏的 html 标签有关。请使用相关的 html 更新帖子。

标签: java jsp servlets


【解决方案1】:

复选框输入元素不包含在提交的表单元素中。

您需要将表单包裹在所有输入元素周围:

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

【讨论】:

    猜你喜欢
    • 2018-02-04
    • 2013-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-05
    • 2012-06-24
    相关资源
    最近更新 更多