2014-07-09 3 views
2

success.jsp 페이지에는 편집 버튼과 각 행에 대한 확인란이있는 여러 행과 열이 표시됩니다. 사용자가 편집 버튼을 클릭하면 checkbox이 선택됩니다. 다음은 success.jsp입니다 :JSP에서 선택된 행을 Java 컨트롤러로 보내는 방법은 무엇입니까?

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

내가 하나 이상의 행을 선택하면 내 서블릿에 전체 행 데이터를 전송하는 방법을 모르겠어요. 선택한 체크 박스의 모든 색인을 가져올 수 있습니다. 그러나 typeId, paramSeq 및 paramName과 같은 다른 관련 값을 추출 할 수 없습니다. 위의 코드에서

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

는, 현재, 나는 모든 전달 된 매개 변수와 선택 행을 얻기 위해 노력하고 다음과 같이 내 서블릿이다. 이 서블릿을 수정하고 typeId와 같은 다른 데이터와 함께 선택한 행에 액세스하여 모든 행에 대해 UPDATE 문을 작성하려고합니다. 나는 thisthis을 언급했지만별로 도움이되지 않습니다.

답변

0

나는 당신이 각 입력 유형에 고유 한 ID를 부여하지만 입력 유형의 이름을 지정하지 않고 서블릿에서 이름으로 값을 가져 오려고한다고 생각합니다.

그래서 나를 아주 바보 서블릿

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

에서 그들을 얻기 위해 아이디의와 함께 이름을 추가하십시오. 고맙습니다, Ekansh. –

관련 문제