标签:http item code exce pat prot cti 购物车 throws
流程:
首先浏览器把请求发给 JSP(index.jsp),index.jsp 实际上调用的是 BookServlet 的 getBooks() 方法。然后到了 Servlet ,这个Servlet 需要获取请求参数:maxPrice, minPrice, pageNo,把请求参数封装为 CriteriaBook 对象。如果没有传这三个参数,它们是有默认值的。然后调用 BookService 的getPage() 方法,要把 CriteriaBook 传进来,BookService 再来调 BookDao 的 getPage() 这个方法,然后再在 BookServlet 中把我得到的 page 放到请求域里面,再做一个 forward 去 books.jsp 页面就可以显示了。
index.jsp 代码:
1 <% 2 response.sendRedirect(request.getContextPath() + "/bookServlet?method=getBooks"); 3 %>
BookServlet 关键代码:
1 private BookService bookService = new BookService(); 2 protected void getBooks(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 3 //获取请求参数 4 String pageNoStr = request.getParameter("pageNo"); 5 String minPriceStr = request.getParameter("minPrice"); 6 String maxPriceStr = request.getParameter("maxPrice"); 7 8 //如果没有请求参数,设其为默认值 9 int pageNo = 1; 10 int minPrice= 0; 11 int maxPrice = Integer.MAX_VALUE; 12 //转型 13 try { 14 pageNo = Integer.parseInt(pageNoStr); 15 } catch (NumberFormatException e) {} 16 17 try { 18 minPrice = Integer.parseInt(minPriceStr); 19 } catch (NumberFormatException e) {} 20 21 try { 22 maxPrice = Integer.parseInt(maxPriceStr); 23 } catch (NumberFormatException e) {} 24 25 //将请求参数封装为 CriticalBook 对象 26 CriteriaBook criteriaBook = new CriteriaBook(minPrice, maxPrice, pageNo); 27 Page<Book> page = bookService.getPage(criteriaBook); 28 //把得到的 page 放入请求域里 29 request.setAttribute("bookpage", page); 30 //页面的转发 31 request.getRequestDispatcher("/WEB-INF/pages/books.jsp").forward(request, response); 32 }
BookService 关键代码:
1 private BookDAO bookDAO = new BookDAOImpl(); 2 3 public Page<Book> getPage(CriteriaBook criteriaBook){ 4 return bookDAO.getPage(criteriaBook); 5 }
books.jsp 关键代码:
1 <html> 2 <head> 3 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4 <title>Insert title here</title> 5 <script type="text/javascript" src="script/jquery-1.7.2.min.js"></script> 6 <script type="text/javascript"> 7 8 $(function(){ 9 //如果 id 为 pageNo 的text 里发生改变的话 10 $("#pageNo").change(function(){ 11 //获取text 里的这个 值 12 var pageNoVal = $(this).val(); 13 //去掉前后空格 14 pageNoVal = $.trim(pageNoVal); 15 16 //判断输入的是否是合法的数字 1、2、3 之类的 17 var reg = /^\d+$/g; 18 var flag = false; 19 var pageNoVal2 = 0; 20 if(reg.test(pageNoVal)){ 21 //判断输入的页码是否在正确范围内 22 pageNoVal2 = parseInt(pageNoVal); 23 if(pageNoVal2 >= 1 && pageNoVal2 <= parseInt("${bookpage.totalPageNumber}")){ 24 flag = true; 25 } 26 } 27 if(!flag){ 28 alert("输入的页数不合法"); 29 //判断输入不合法后,将 text 置空 30 $(this).val(""); 31 return; 32 } 33 //页面跳转 34 var href = "bookServlet?method=getBooks&pageNo=" + pageNoVal2 + "&" + $(":hidden").serialize(); 35 window.location.href = href; 36 }); 37 38 }) 39 </script> 40 <%@ include file="/commans/selectCondition.jsp" %> 41 </head> 42 <body> 43 <center> 53 <form action="bookServlet?method=getBooks" method="post"> 54 Price:<input type="text" size="1" name="minPrice"/>- 55 <input type="text" size="1" name="maxPrice"/> 56 <input type="submit" value="Submit"/> 57 </form> 58 <br><br> 59 <table cellpadding="10"> 60 <c:forEach items="${bookpage.list}" var="book"> 61 <tr> 62 <td> 63 <a href="">${book.title }</a> 64 <br> 65 ${book.author } 66 </td> 67 <td>${book.price }</td> 68 <td><a href="">加入购物车</a></td> 69 </tr> 70 </c:forEach> 71 </table> 72 <br><br> 73 共 ${bookpage.totalPageNumber } 页 74 75 当前第 ${bookpage.pageNo } 页 76 77 78 <c:if test="${bookpage.hashPre }"> 79 <a href="bookServlet?method=getBooks&pageNo=1">首页</a> 80 81 <a href="bookServlet?method=getBooks&pageNo=${bookpage.prePage }">上一页</a> 82 </c:if> 83 84 <c:if test="${bookpage.hashNext }"> 85 <a href="bookServlet?method=getBooks&pageNo=${bookpage.nextPage }">下一页</a> 86 87 <a href="bookServlet?method=getBooks&pageNo=${bookpage.totalPageNumber }">末页</a> 88 </c:if> 89 90 转到 <input type="text" size="1" id="pageNo"/>页 91 </center> 92 </body> 93 </html>
over。
标签:http item code exce pat prot cti 购物车 throws
原文地址:http://www.cnblogs.com/daoxiaobai/p/6366455.html