form in jsp with several actions

0

I'm doing a form in jsp that I can edit and delete at the same time, I have a problem with the option to delete several, where I have put a checkbox to each one to select the ones you want and delete them at the same time. The problem I have is that I can only put or the option to edit or delete all to work well, because if I put the key to close the for of people before closing the form only collects me the first record and if I put the key after closing the form the delete several button is repeated ... has someone done something similar and found a solution?

Jsp code

            <%
                if (session.getAttribute("ListaPersonas") != null) {
                    ArrayList<Persona> list = (ArrayList) session.getAttribute("ListaPersonas");
                    if (list.size() > 0) {
                        Persona per;

                for (int i = 0; i < list.size(); i++) {
                    per = list.get(i);
            %>   
            <form name="formusu" action="../Controlador.jsp" method="post">

                <input type="text" name="id_dep" value="<%out.print(per.getId_departamento());%>" hidden>
                <table class="table">
                    <thead>
                        <tr>
                            <th scope="col">Dni </th>
                            <th scope="col">Nombre </th>
                            <th scope="col">Apellidos</th>
                            <th scope="col">Email</th>                                
                            <th scope="col">Departamento</th>
                            <th scope="col">Cambiar Departamento</th>
                            <th scope="col">Modificar</th>
                            <th scope="col">Eliminar</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td><input type="text" name="dni" value="<%out.print(per.getDni());%>" required></td>
                            <td><input type="text" name="nombre" value="<%out.print(per.getNombre());%>" required>
                            <td><input type="text" name="apellidos" value="<%out.print(per.getApellidos());%>" required></td>
                            <td><input type="email" name="mail" value="<%out.print(per.getEmail());%>" required></td>
                            <td><input type="text" name="nombredepar" value="<%out.print(per.getNombre_departamento());%>" readonly></td>

                            <%
                                if (session.getAttribute("departamentos") != null) {

                                    ArrayList<Departamento> listaDep = (ArrayList) session.getAttribute("departamentos");
                                    if (listaDep.size() > 0) {
                            %>
                            <td>Elige Departamento: <SELECT NAME="departselecc">
                                    <%
                                        for (int j = 0; j < listaDep.size(); j++) {
                                    %>
                                    <!-- selected="<  %if(i==0){out.print("true");}%>"  -->
                                    <OPTION  VALUE="<%=listaDep.get(j).getId_departamento()%>"><%=listaDep.get(j).getNombre_departamento()%>
                                        <%
                                                    }
                                                }
                                            }
                                        %> 
                                </SELECT>
                            </td>

                            <td><input type="submit" class="btn btn-primary" name="modificarusudire" value="Modificar"></td>

                            <td><input type="checkbox" id="borrarvariasper" name="borrarvariasper" value="<%out.print(per.getDni());%>"/></td>
                        </tr>              
                    </tbody>
                </table>
                <%
                    // } si cierro aquí el for, me funciona bien el botón de borrar varios con los checkbox, pero al editar las personas solo me recoge el primer registro
                %>        

                <div class="container text-center">
                    <br><input type="submit"  class="btn btn-primary" name="borrarpers" value="Eliminar Varios">
                </div>
            </form>
            <%
                        }
                    }
                }
            %>
    
asked by Esther 23.10.2018 в 21:13
source

1 answer

1

Let's see, the first thing you have to be clear about is what HTML do you want and then mount the JSP to generate it.

In particular, a single "Delete all" button (the ones selected) makes sense because after making the request you return to show the table. That's why you simply have a checkbox to select the records and a single button.

But "Modify all" does not make sense because normally modifications will be made to register, so you have a "Modify" button for each record.

So the for has to be closed before the "Delete all" button so there is only one.

The problem that to modify only the first one is probably due to the fact that, in the submit button that you use to modify, you are not indicating in any way what the registration is that you want to modify . An option to solve this problem is to add the registration ID to the name of the button (eg name="modificar28" ), and in the server logic retrieve that value (eg, go through the request.getParameterNames() to find the one that starts with modificar )

    
answered by 24.10.2018 в 10:45