The task is to develop an admin panel for an online parts store. There is a table of products and orders in MySQL, the administrator can delete the product only if the product id is not used in the order database.
This is all clear. I made the deletion, now I want to display a mistake if the product is in the order, and I accidentally wanted to delete it. In the try / catch service layer, I catch and display the error message. But I would like to implement a redirect to the product page and display an error in the client.
I do not quite understand how to implement this action. Tell me please. I would be very grateful.
Removal Controller Code:
public class DeleteOrdController implements Controller { private OrdService ordService = OrdServiceIpl.getInstance(); private Logger logger = Logger.getLogger(DeleteOrdController.class); @Override public void execute(HttpServletRequest req, HttpServletResponse resp) { try{ String id = req.getParameter("deleteOrd"); long ordId = Long.parseLong(id); ordService.delete(ordId); String contextPath = req.getContextPath(); resp.sendRedirect(contextPath + "/frontController?command=orders"); } catch (IOException e) { logger.error(e); } } } Product table:
<table> <tr> <th>№</th> <th>Parts id</th> <th>Producer</th> <th>Category</th> <th>Name parts</th> <th>Chatacteristics</th> <th>Price</th> <th>Delete</th> </tr> <c:forEach var="batteries" items="${parts}" varStatus="status"> <tr> <td>${status.index + 1}</td> <td>${batteries.id}</td> <td>${batteries.producer}</td> <td>${batteries.category}</td> <td>${batteries.name}</td> <td>${batteries.chatacteristics}</td> <td>${batteries.price}</td> <td><form action="frontController?command=deleteproduct" method="post"> <button value="${batteries.id}" name="delete" class="btn">Delete</button> </form></td> </tr> </c:forEach> </table> <br/> Service layer:
@Override public int delete(Serializable id) { try { return partsDao.delete(id); } catch (SQLException e) { throw new ServiceException("Ошибка удаления Parts по id " + id); } } DAO
@Override public int delete(Serializable id) throws SQLException { psDelete.setLong(1, (long) id); return psDelete.executeUpdate(); }