分頁查詢關鍵代碼: 通過servlet轉發回來的各種信息進行分頁的設計(轉發回的信息有 分頁查詢的List集合 查詢的頁碼 查詢的條數 查詢的資料庫總條數 查詢的總頁碼) 從開始時迴圈10次出現十個數字開始逐步細化 註意:jstl(java標磚標簽庫) 的判斷條件 test="" 引號中的變數運算寫 ...
分頁查詢關鍵代碼:
通過servlet轉發回來的各種信息進行分頁的設計(轉發回的信息有 分頁查詢的List集合 查詢的頁碼 查詢的條數 查詢的資料庫總條數 查詢的總頁碼)
從開始時迴圈10次出現十個數字開始逐步細化
註意:jstl(java標磚標簽庫) 的判斷條件 test="" 引號中的變數運算寫在一個${ } EL表達式中,運算完再顯示結果
<div align="center"> <c:if test="${pageNumber!=1 }"> <span style="font-size:x-large;"><a href="${pageContext.request.contextPath }/shangpin?md=findByPage&pageNumber=1">第一頁</a></span> <span style="font-size:x-large;"><a href="${pageContext.request.contextPath }/shangpin?md=findByPage&pageNumber=${pageNumber-1 }">上一頁</a></span> </c:if> <!-- 分頁查詢的核心 --> <c:forEach begin="1" end="${totalPages }" step="1" var="n"> <!-- n能顯示出來的條件 --> <c:if test="${(pageNumber-n)<=5&&(n-pageNumber)<=4 }"> <c:if test="${n!=pageNumber }"> <span style="font-size:x-large;"><a href="${pageContext.request.contextPath }/shangpin?md=findByPage&pageNumber=${n }">${n }</a></span> </c:if> <c:if test="${n==pageNumber }"> <span style="font-size:x-large;">${n }</span> </c:if> </c:if> </c:forEach> <c:if test="${pageNumber!=totalPages }"> <span style="font-size:x-large;"><a href="${pageContext.request.contextPath }/shangpin?md=findByPage&pageNumber=${pageNumber+1 }">下一頁</a></span> <span style="font-size:x-large;"><a href="${pageContext.request.contextPath }/shangpin?md=findByPage&pageNumber=${totalPages }">最後一頁</a></span> </c:if> </div>
多條件查詢關鍵代碼:
拼串的時候註意如果帶?的不用寫單引號'
如果不帶?的sql串,記得帶單引號'
public List<Product> queryMore(String queryName, String queryDesc) throws SQLException { String sql="SELECT * FROM product where 1=1 "; if(queryName!=null && queryName.trim()!="") { sql=sql+"and pname like '%"+queryName+"%'"; } if(queryDesc!=null && queryDesc.trim()!="") { sql=sql+" and pdesc like '%"+queryDesc+"%'"; } System.out.println(sql); List<Product> pros=qr.query(sql, new BeanListHandler<Product>(Product.class)); System.out.println(pros); return pros; }
刪除選中商品關鍵代碼:
public void deleteSelect(String[] checks) throws SQLException { String sql="delete from product where pid in("; for(String str:checks) { sql=sql+"'"+str+"'"+","; } sql=sql.substring(0, sql.length()-1); sql=sql+ ")"; qr.update(sql); }
前端部分
function deleteSelect(){ if(confirm("確認刪除這些商品嗎?")){ var form1=document.getElementById("form1"); form1.submit(); } }
<c:if test="${not empty shangpins }"> <form method="post" action="${pageContext.request.contextPath }/shangpin?md=deleteSelect" id="form1"> <c:forEach items="${shangpins }" var="s"> <tr> <td width="6%" align="center"><input name="checks" type="checkbox" value="${s.pid }"></td> <td width="8%"><img width="180px" alt="" src="${s.pimage }"></td> <td width="6%">${s.pname }</td> <td width="8%">${s.shop_price }</td> <td width="8%">${s.market_price }</td> <td>${s.pdesc }</td> <td width="8%"><a href="${pageContext.request.contextPath }/shangpin?md=huixian&pid=${s.pid}">修改</a></td> <td width="8%"><a href="javascript:void(0);" onclick="agree('${s.pid}')">刪除</a></td> </tr> </c:forEach> </form> </c:if>
修改要先回顯再修改
<td width="8%"><a href="${pageContext.request.contextPath }/shangpin?md=huixian&pid=${s.pid}">修改</a></td>
private void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { BeanUtils.populate(p, request.getParameterMap()); service.edit(p); response.sendRedirect(request.getContextPath()+"/shangpin?md=findAll"); } catch (Exception e) { e.printStackTrace(); request.setAttribute("msg", "更新商品失敗了"); request.getRequestDispatcher("/msg.jsp").forward(request, response); } } private void huixian(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { String pid=request.getParameter("pid"); Product product=service.getProduct(pid); request.setAttribute("product", product); request.getRequestDispatcher("/xiugaishangpin.jsp").forward(request, response); } catch (SQLException e) { e.printStackTrace(); request.setAttribute("msg", "回顯商品失敗了"); request.getRequestDispatcher("/msg.jsp").forward(request, response); } }
同一業務集中使用同一servlet的方法
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String md=request.getParameter("md"); if("findAll".equals(md)) { findAll(request,response); }else if("add".equals(md)){ add(request,response); }else if("huixian".equals(md)) { huixian(request,response); }else if("edit".equals(md)) { edit(request,response); }else if("delete".equals(md)) { delete(request,response); }else if("deleteSelect".equals(md)) { deleteSelect(request,response); }else if("queryMore".equals(md)){ queryMore(request,response); }else if("findByPage".equals(md)) { findByPage(request,response); } }
前端
<span style="font-size:x-large;"><a href="${pageContext.request.contextPath }/shangpin?md=findByPage&pageNumber=${pageNumber-1 }">上一頁</a></span>
用?md=findAll的方法
servlet判斷md的值調用相應方法
也可以使用反射的方法代替