Passing parameters of a row of an html table with jsp

0

I would like to be able to edit the fields of a table from a list on a page jsp

try {
            Connection conexion=null;
            ResultSet rs = null;
            conexion = DriverManager.getConnection("jdbc:mysql://localhost:3306/ticketsdetrabajo", "root", "Cuatro:2cuatro");
            //creamos objeto Statement
            Statement st=conexion.createStatement();
            rs=st.executeQuery(consultaSQL);

            while(rs.next()){
            aprobado=(int)rs.getObject("Aprobado");
            %>
                <tr>
            <td class="table2"><%=rs.getObject("NParte")%><br>
            </td>
            <td class="table2" ><%=rs.getObject("Nempleado")%><br>
            </td>
            <td class="table2" ><%=rs.getObject("Nombre")%><br>
            </td>
            <td class="table2"><%=rs.getObject("Apellidos")%><br>
            </td>
            <td class="table2"><%=rs.getObject("DescripcionTarea")%><br>
            </td>
            <td class="table2"><%=rs.getObject("TiempoEmpleado")%><br>
            </td>
            <td class="table2"><%=rs.getObject("FechaEntrada")%><br>
            </td>
            <td class="table2"><%=rs.getObject("FechaEntrega")%><br>
            </td>
            <td class="table2"><% if (aprobado==1){%>Si
            <%}else {%>No<%} %>

            <br>
            </td>
            <td class="table2"><button name="btnlapiz" class="botonplano" onclick="location.href='editarpartesdetrabajo.jsp'" ><img alt="editar" src="lapiz.png"></button></td>
          </tr>
        <%}   

      }catch (java.sql.SQLException sqle){
            System.out.println("Error: " + sqle);

        }

            %>

When clicking on the image I would like to be able to pass parameters of that row to a jsp, today I am a little thick and I can not think of how to do so by pressing the image of the pencil take that row and pass the parameters another page.

Thanks and best regards

    
asked by gulez 11.06.2018 в 12:39
source

1 answer

0
  • The simplest thing is to pass the values by parameter in the URL (GET request).

    In fact, the simplest thing is to just pass the ID of the row and then retrieve the data from BD.

      

    onclick="location.href = 'editjobparts.jsp? NParte = <% = rs.getObject (" NParte ")% >'"

  • If for any reason a GET request is not valid, you create a <form> element with a hidden field and you call a JS method that updates the value of the field and makes the request:

      

    onclick="sendForm ('<% = rs.getObject (" NParte ")% >');"

    In both cases, if NParte can have non-alphanumeric characters, it may be necessary to code them (eg with URL Encoder)

  • You call a javascript function that traverses the DOM to get the value

      

    onclick="sendTr (this);"

    Here, this is the button; this.parentNode the <td> where the button is, this.parentNode.parentNode the <tr> , and the <tr> you can go through the children to get the values. With that, either mount the URL (as in point 1) or fill in the hidden form (as in point 2).

  • As an additional note, it is generally recommended that you make the requests to a servlet so that it takes care of the business logic (queries to BD, etc.), save the results as attributes to be displayed in the request and use a "clean" JSP with JSTL. Unless you're touching old code that is not worth refactoring, you should consider it.

        
    answered by 11.06.2018 в 16:02