samedi 28 avril 2018

JSP checkbox returning only one, first value

Im learning about Jsp and servlets. In my jsp im am returning a table of employees, im trying to delete multiple rows from checkbox by userId. When i run servlet i got filled table with employees but by checkbox i can only delete first employee (first row), every other is returning null...

<div class ="removeBtn"> <!-- remove button for multiple delete -->
     <input type="submit" value="Remove selected" form="multipleDeleteRowsId">
</div>

       <table class="employeeInfoTable" >
        <tr>
            <th></th>
            <th>First Name</th>
            <th>Second Name</th>
            <th>Last Name</th>
            <th>Date of Join</th>
            <th>Branch</th>
            <th>Action</th>
        </tr>

        <c:forEach var="tempUser" items="${employeesList}">

            <c:url var="idLink" value="EmployeeControllerServlet">
                <c:param name="command" value="LOAD"/>
                <c:param name="employeeID" value="${tempUser.id}"/>
            </c:url>
            <c:url var="removeLink" value="EmployeeControllerServlet">
                <c:param name="command" value="DELETE"/>
                <c:param name="deleteByID" value="${tempUser.id}"/>
            </c:url>

            <tr>
                <td>
                    <form action="EmployeeControllerServlet" method="get" id="multipleDeleteRowsId" > <!-- here is multiple delete -->
                        <input type="hidden" name="command" value="DELETE">
                        <input type="checkbox" name="selectedRow" value="${tempUser.id}">${tempUser.id}
                    </form>
                </td>
                <td>${tempUser.firstName}
                </td>
                <td>${tempUser.surname}
                </td>
                <td>${tempUser.lastName}
                </td>
                <td>${tempUser.dateOfJoin}
                </td>
                <td>${tempUser.branch}
                </td>
                <td>
                    <ul id="menu">
                        <li><a href="#">Edit</a>
                            <ul>
                                <li><a href="#"><a href="${idLink}">More Info</a></a></li>
                                <li><a href="#"><a href="${removeLink}"
                                                   onclick="return confirm( 'Are you sure you want to delete?')">Remove</a></a>
                                </li>
                            </ul>
                        </li>
                    </ul>
                </td>
            </tr>
        </c:forEach>
    </table>

here is servlet

    private void deleteUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
    try {

        String[] selected = request.getParameterValues("selectedRow");
        for(int i = 0; i<selected.length; i++){
            System.out.println(" " + selected[i] + " ");
        }
    } catch (Exception e) {
        System.out.println("not deleted ? " + e.getCause());
    }
}

finally result for other rows than first is null..

Thanks in advance




Aucun commentaire:

Enregistrer un commentaire