P
P
parkito2016-09-02 02:00:22
Java
parkito, 2016-09-02 02:00:22

How in jsp to bind to delete a specific element from the table?

Hello. Please help me solve the problem. There is a database. From it I read the table. I display it on a web page using jpa/jsp. I want. so that the user can select one or another row and delete it.

Here, for example, I display a table with two columns. In the third column I display buttons. I want that at pressing of this or that button, I could identify what is pressed and process it.

<table class="table">
                <thead>
                <tr>
                    <th>#</th>
                    <th>Number</th>
                    <th>Status</th>
                    <th>Action</th>
                </tr>
                </thead>
                <tbody>

                <%
                    ContractServiceImpl contractService = new ContractServiceImpl();
                    List<Contract> contracts = contractService.getAllContractsForUser(user.getUserId());
                    for (int i = 0; i < contracts.size(); i++) {
                        if (contracts.get(i).getIsBlocked())
                            out.print("<tr class=\"danger\">");
                        else out.print("<tr class=\"active\">");
                %>
                <th scope="row"><%out.print(i + 1);%></th>
                <td><%out.print(contracts.get(i).getNumber());%></td>
                <%
                    if (contracts.get(i).getIsBlocked())
                        out.print("<td>Blocked</td>");
                    else
                        out.print("<td>Active</td>");

                    if (contracts.get(i).getIsBlocked())
                        out.print("<td><button type=\"button\" class=\"btn btn-success\" data-toggle=\"modal\" data-target=\"#myModalGreen\">\n" +
                                "UnBlock</button></td>");
                    else
                        out.print("<td><button type=\"button\" class=\"btn btn-danger\" data-toggle=\"modal\" data-target=\"#myModal\">\n" +
                                "Block</button></td>");

                %>

                <td>
                </td>
                </tr>
                <%}%>
                </tbody>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evhen, 2016-09-02
@parkito

1. JSP scriptlets are very bad form and not recommended.
This needs to be done in a servlet (not in a jsp)

ContractServiceImpl contractService = new ContractServiceImpl();
     List<Contract> contracts = contractService.getAllContractsForUser(user.getUserId());

then pass contracts to jsp via request attribute
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     ContractServiceImpl contractService = new ContractServiceImpl();
     List<Contract> contracts = contractService.getAllContractsForUser(user.getUserId());

   request.setAttribute("contracts",contracts);
   RequestDispatcher requestDispatcher; 
   requestDispatcher = request.getRequestDispatcher("/you_jsp_page.jsp");
  requestDispatcher.forward(request, response);
}

and then on the JSP side using JSTL to display data
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<hmtl>
...
<body>
<table>
<c:forEach items="${contracts}" var="contr">
<tr>
   <td>${contr.name}</td>
   <td>${contr.number}</td>

  ....
</tr>
</c:forEach>
</table>
</body>
</hmtl>

a) the Contract entity must have a unique id by which you could delete/select/change it.
b) in JSP your table in the tag and add one more column with a checkbox, which wakes up the Contract id as value. Add a "Delete Selected" button.
<form method="post" action="ваш сервлет">
<input type="submit" value="Удалить выбранное"/>
<table>
<c:forEach items="${contracts}" var="contr">
<tr>
   <td><input type="checkbox" name="id" value="${contr.id}"/></td>
   <td>${contr.name}</td>
   <td>${contr.number}</td>

  ....
</tr>
</c:forEach>
</table>
</form>

c) Implement in your servlet a method for processing post requests where you get the selected Ids from the request and delete them.
@Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    String[] ids = req.getParameterValues("id");

    if (ids != null && ids.length > 0) {
      ContractServiceImpl contractService = new ContractServiceImpl();
      contractService.removeAll(ids);
    }

    doGet(req, resp); 
  }

doGet(req, resp); - the method is implemented above. There will be a new selection of contracts, only without the deleted ones and will be transferred to JPS. The page will be updated

E
Eugene, 2016-09-02
@zolt85

In the button click handler, pass the id of the entity you want to process.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question