【问题标题】:How to send selected rows in JSP to Java controller?如何将 JSP 中的选定行发送到 Java 控制器?
【发布时间】:2014-08-30 07:54:48
【问题描述】:

我有一个success.jsp 页面,它显示多行和多列,每个行都有一个编辑按钮和一个复选框。如果用户点击编辑按钮,checkbox 被选中。 下面是success.jsp:

<%@page import="mymvc.model.TableColumns"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.Iterator"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%-- 
    Document   : success
    Created on : Jul 8, 2014, 1:43:17 PM
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Login Success</title>
        <script type="text/javascript">
            function validate(n) {
                v = document.getElementById("check"+n)
                v.checked = !v.checked;
                x = document.getElementById("typeId"+n).removeAttribute('readonly');
                y = document.getElementById("paramSeq"+n).removeAttribute('readonly');
                z = document.getElementById("paramName"+n).removeAttribute('readonly');
            }

        </script>
    </head>
    <body>
        <form action="DBController" method="post">

        Welcome ${requestScope['user'].username}.


        <table>
            <tr style="background-color:#f0a64e;">
                <th class="border">ID</th>
                <th class="border">Param Sequence</th>
                <th class="border">Param Name</th>
            </tr>
            <c:forEach var="element" items="${requestScope['listData']}" varStatus="status">
                <tr>
                    <td><input  id="typeId${status.index}" value="${element.typeId}" readonly="true"</td>
                    <td><input  id="paramSeq${status.index}" value="${element.paramSeq}" readonly="true"</td>
                    <td><input  id="paramName${status.index}" value="${element.paramName}" readonly="true"</td>
                    <td>
                        <input id="edit${status.index}" type="button" value="Edit" name="edit" onclick="validate(${status.index})"</input>
                    </td>
                    <td><input type="checkbox" id="check${status.index}" name="selectedItems" value="<c:out value="${status.index}"/>"</td>
                </tr> 
            </c:forEach>
        </table>
        <input type="submit" value="Update" name="update" />
        </form>
    </body>
</html>

如果选择了多行,我不知道如何将整行数据发送到我的 servlet。我能够获得选中的复选框的所有索引。但我无法提取其他相关值,如 typeId、paramSeq 和 paramName。我的servlet如下:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    String[] edited = request.getParameterValues("selectedItems");
    //String pName = request.getParameter("paramName"+edited[1]);
    Enumeration<String> paramName = request.getParameterNames();
    String[] param = new String[10];
    int i=0;
    while(paramName.hasMoreElements()){
        param[i]=paramName.nextElement();
    }
    RequestDispatcher rd = null;
    rd = request.getRequestDispatcher("/update.jsp");
    request.setAttribute("param", param);
    request.setAttribute("edited", edited);
    rd.forward(request, response);
}

在上面的代码中,目前,我正在尝试获取所有传递的参数和选择的行。我想修改这个 servlet 并访问选定的行以及 typeId 等其他数据,以便为每一行创建 UPDATE 语句。我提到了thisthis,但没有太大帮助。

【问题讨论】:

    标签: java sql jsp servlets


    【解决方案1】:

    我认为您缺少的是您为每个输入类型提供了一个唯一的 id,但您没有指定输入类型的名称,并且在 servlet 中您试图按名称获取值。

    所以请在 id 中添加名称,以便在 servlet 中获取它们

            <c:forEach var="element" items="${requestScope['listData']}" varStatus="status">
                <tr>
                    <td><input  name="typeId${status.index}" value="${element.typeId}" readonly="true"</td>
                    <td><input  name="paramSeq${status.index}" value="${element.paramSeq}" readonly="true"</td>
                    <td><input  name="paramName${status.index}" value="${element.paramName}" readonly="true"</td>
                    <td>
                        <input id="edit${status.index}" type="button" value="Edit" name="edit" onclick="validate(${status.index})"</input>
                    </td>
                    <td><input type="checkbox" id="check${status.index}" name="selectedItems" value="<c:out value="${status.index}"/>"</td>
                </tr> 
            </c:forEach>
        </table>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-01
      • 2021-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-22
      • 1970-01-01
      相关资源
      最近更新 更多