Javascript & Html -> Button click
Java Web 2016
//Javascript <script> function toggle(source) { checkboxes = document.getElementsByName('product'); for (var i = 0, n = checkboxes.length; i < n; i++) { checkboxes[i].checked = source.checked; } } </script> //Click checkBox All and UncheckBox ALl <input type="checkbox" id="checkBoxAll" onClick="toggle(this)"/> //Display checkbox <input type="checkbox" name="product"/>
product.jsp
Java Web 2016
<%@page import="java.util.List"%> <%@page import="java.util.List"%> <%@page import="jpaDAO.ShopJpaController"%> <%@page import="Entty.Shop"%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <title>Wellcome Admin</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link rel="stylesheet" type="text/css" href="../mos-css/mos-style.css"> <style> .img-danhmuc{ width: 50px; height: 30px; } .add{ display: block; background: #a6d050; line-height: 15px; width: 102px; border-radius: 5px; padding: 5px; color: white; font-weight: bold; } ul.pagination { display: inline-block; padding-left: 36%; margin: 0; } ul.pagination li {display: inline;} ul.pagination li a { color: black; float: left; padding: 8px 16px; text-decoration: none; transition: background-color .3s; border: 1px solid #ddd; font-size: 12px; } ul.pagination li a.active { background-color: #4CAF50; color: white; border: 1px solid #4CAF50; } ul.pagination li a:hover:not(.active) {background-color: #ddd;} #btDelete{ float: left; background-color: #a6d050; color: white; font-weight: bold; padding: 3px 10px 6px 10px; margin-bottom: 2px; border-radius: 5px; } #btDelete:hover{ color: #000; } .add{ float: left; margin-left: 4px; } </style> <script> function toggle(source) { checkboxes = document.getElementsByName('product'); for (var i = 0, n = checkboxes.length; i < n; i++) { checkboxes[i].checked = source.checked; } } </script> </head> <body> <div id="wrapper"> <jsp:include page="header.jsp"></jsp:include> <jsp:include page="asider.jsp"></jsp:include> <div id="rightContent"> <h3>Quản Lý Danh Mục</h3> <form method="POST" action="${pageContext.request.contextPath}/DeleteAllProduct?action=delete"> <input type="submit" value="Delete" id="btDelete" onclick="return confirm('Are you sure delete?')"/> <a href="insert-shop.jsp" class="add">Thêm Danh Mục</a> <table class="data"> <tr class="data"> <th class="data"><input type="checkbox" id="checkBoxAll" onClick="toggle(this)" /></th> <th class="data">Mã Danh Mục</th> <th class="data">Tên Danh Mục</th> <th class="data">Thông Tin Chi Tiết</th> <th colspan="2" class="data">URL Ảnh</th> <th class="data">Giá Tiền</th> <th colspan="2" class="data">Tùy Chọn</th> </tr> <% ShopJpaController sdao = new ShopJpaController(); int first = 0, last = 0, pages = 1; //Neu "index.jsp?pages=" pages ko co bo nho hoac rong thi lay ra pages if (request.getParameter("pages") != null) { pages = (int) Integer.parseInt(request.getParameter("pages")); } //Lay number total bang query select count(id) from name_table int total = sdao.getShopCount(); //Neu number total = 3 if (total <= 6) { first = 0; //first =0 last = total; //last =3 } else { first = (pages - 1) * 6; last = 6; } List<Shop> list = sdao.findShopEntities(last, first); for (Shop item : list) { %> <tr> <td class="data"><input type="checkbox" name="product" value="<%=item.getId()%>"/></td> <td class="data" id="id"><%=item.getId()%></td> <td class="data"><%=item.getName()%></td> <td class="data"><%=item.getDetail()%></td> <td class="data"><%=item.getImage()%></td> <td class="data"><img class="img-danhmuc" src="<%=request.getContextPath()%>/images/<%=item.getImage()%>"/></td> <td class="data" style="width: 70px;"><%=item.getPrice()%> $</td> <td class="data" style="width: 80px;"><a href="update-shop.jsp?id=<%=item.getId()%>"> <img src="../mos-css/img/Text-Edit-icon.png"/></a> <a href="<%=request.getContextPath()%>/ShopDeleteServlet?id=<%=item.getId()%>" onclick="return confirm('Are you sure delete?')"> <img src="../mos-css/img/delete-file-icon.png"/></a></td> </tr> <%}%> </table> </form> <ul class="pagination"> <% //Button Previous int back = 0; if (pages == 0 || pages == 1) { back = 1;//Luon la page 1 } else { back = pages - 1;//Neu pages tu 2 tro len thi back tru 1 } %> <li><a href="product.jsp?pages=<%=back%>">«</a></li> <% int loop = 0, num = 0; if ((total / 6) % 2 == 0) { num = total / 6; } else { num = (total + 1) / 6; } //Nếu total lẻ thêm 1 if (total % 2 != 0) { loop = (total / 6) + 1; } else { //Nếu total chẵn nhỏ hơn fullpage và # fullPage thì thêm 1 if (total < (num * 6) + 6 && total != num * 6) { loop = (total / 6) + 1; } else { //Nếu bằng fullPage thì không thêm loop = (total / 6); } } //Lap so pages for (int i = 1; i <= loop; i++) {%> <% if (pages == i) {%> <li><a class="active" href="product.jsp?pages=<%=i%>"><%=i%></a></li> <%} else {%> <li><a href="product.jsp?pages=<%=i%>"><%=i%></a> </li> <%} }%> <% //Button Next int next = 0; if (total % 2 != 0) { if (pages == (total / 6) + 1) { next = pages;//Khong next } else { next = pages + 1;//Co next } } else { //Nếu total chẵn nhỏ hơn fullpage //Và không fullPage thì thêm 1 if (total < (num * 6) + 6 && total != num * 6) { if (pages == (total / 6) + 1) { next = pages;//Khong next } else { next = pages + 1;//Co next } } else { //Nếu fullPage đến trang cuối dừng //Chưa tới trang cuối thì được next if (pages == (total / 6)) { next = pages;//Khong next } else { next = pages + 1;//Co next } } } %> <li><a href="product.jsp?pages=<%=next%>">»</a></li> </ul> </div> <div class="clear"></div> <jsp:include page="footer.jsp"></jsp:include> </div> </body> </html>
Servlet: DeleteAllProduct.java
Java Web 2016
package controller; import Entty.Shop; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import jpaDAO.ShopJpaController; /** * * @author Lonely */ public class DeleteAllProduct extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.sendRedirect(request.getContextPath()+"/admin/product.jsp"); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { String action = request.getParameter("action"); ShopJpaController sdao = new ShopJpaController(); if(action.equalsIgnoreCase("delete")){ for(String product : request.getParameterValues("product")){ Shop id = sdao.findShop(product); sdao.destroy(id.getId()); } } response.sendRedirect(request.getContextPath()+"/admin/product.jsp"); } catch (Exception ex) { Logger.getLogger(DeleteAllProduct.class.getName()).log(Level.SEVERE, null, ex); } } }
JPA: ShopJpaController.java
Java Web 2016
package jpaDAO; import Entty.Shop; import java.io.Serializable; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Query; import javax.persistence.EntityNotFoundException; import javax.persistence.Persistence; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Root; import jpaDAO.exceptions.NonexistentEntityException; import jpaDAO.exceptions.PreexistingEntityException; import jpaDAO.exceptions.RollbackFailureException; /** * * @author Lonely */ public class ShopJpaController implements Serializable { private EntityManagerFactory emf = null; private EntityManager em = null; public ShopJpaController() throws Exception { emf = Persistence.createEntityManagerFactory("Shop_BonfirePU"); } public void create(Shop shop) throws PreexistingEntityException, RollbackFailureException, Exception { em = emf.createEntityManager(); try { em.getTransaction().begin(); em.persist(shop); em.getTransaction().commit(); } catch (Exception ex) { try { em.getTransaction().rollback(); } catch (Exception re) { throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re); } if (findShop(shop.getId()) != null) { throw new PreexistingEntityException("Shop " + shop + " already exists.", ex); } throw ex; } finally { if (em != null) { em.close(); } } } public void edit(Shop shop) throws NonexistentEntityException, RollbackFailureException, Exception { em = emf.createEntityManager(); try { em.getTransaction().begin(); shop = em.merge(shop); em.getTransaction().commit(); } catch (Exception ex) { try { em.getTransaction().rollback(); } catch (Exception re) { throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re); } String msg = ex.getLocalizedMessage(); if (msg == null || msg.length() == 0) { String id = shop.getId(); if (findShop(id) == null) { throw new NonexistentEntityException("The shop with id " + id + " no longer exists."); } } throw ex; } finally { if (em != null) { em.close(); } } } public void destroy(String id) throws NonexistentEntityException, RollbackFailureException, Exception { em = emf.createEntityManager(); try { em.getTransaction().begin(); Shop shop; try { shop = em.getReference(Shop.class, id); shop.getId(); } catch (EntityNotFoundException enfe) { throw new NonexistentEntityException("The shop with id " + id + " no longer exists.", enfe); } em.remove(shop); em.getTransaction().commit(); } catch (Exception ex) { try { em.getTransaction().rollback(); } catch (Exception re) { throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re); } throw ex; } finally { if (em != null) { em.close(); } } } public List<Shop> findShopEntities() { return findShopEntities(true, -1, -1); } public List<Shop> findShopEntities(int maxResults, int firstResult) { return findShopEntities(false, maxResults, firstResult); } private List<Shop> findShopEntities(boolean all, int maxResults, int firstResult) { em = emf.createEntityManager(); try { CriteriaQuery cq = em.getCriteriaBuilder().createQuery(); cq.select(cq.from(Shop.class)); Query q = em.createQuery(cq); if (!all) { q.setMaxResults(maxResults); q.setFirstResult(firstResult); } return q.getResultList(); } finally { em.close(); } } public Shop findShop(String id) { em = emf.createEntityManager(); try { return em.find(Shop.class, id); } finally { em.close(); } } public int getShopCount() { em = emf.createEntityManager(); try { CriteriaQuery cq = em.getCriteriaBuilder().createQuery(); Root<Shop> rt = cq.from(Shop.class); cq.select(em.getCriteriaBuilder().count(rt)); Query q = em.createQuery(cq); return ((Long) q.getSingleResult()).intValue(); } finally { em.close(); } } }
0 nhận xét:
Post a Comment