Answer the question
In order to leave comments, you need to log in
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
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());
@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);
}
<%@ 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>
<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>
@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);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question