一、流程分析
1.图书模块
2.分布分析
二、代码
1.view层
1)list.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 3 4 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 5 <html> 6 <head> 7 <title>图书列表</title> 8 9 <meta http-equiv="pragma" content="no-cache"> 10 <meta http-equiv="cache-control" content="no-cache"> 11 <meta http-equiv="expires" content="0"> 12 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 13 <meta http-equiv="description" content="This is my page"> 14 <meta http-equiv="content-type" content="text/html;charset=utf-8"> 15 <!-- 16 <link rel="stylesheet" type="text/css" href="styles.css"> 17 --> 18 <link rel="stylesheet" type="text/css" href="<c:url value='/jsps/css/book/list.css'/>"> 19 <link rel="stylesheet" type="text/css" href="<c:url value='/jsps/pager/pager.css'/>" /> 20 <script type="text/javascript" src="<c:url value='/jsps/pager/pager.js'/>"></script> 21 <script type="text/javascript" src="<c:url value='/jquery/jquery-1.5.1.js'/>"></script> 22 <script type="text/javascript" src="<c:url value='/jsps/js/book/list.js'/>"></script> 23 </head> 24 25 <body> 26 27 <ul> 28 <c:forEach items="${pb.beanList }" var="book"> 29 <li> 30 <div class="inner"> 31 <a class="pic" href="<c:url value='/BookServlet?method=load&bid=${book.bid }'/>"><img src="<c:url value='/${book.image_b }'/>" border="0"/></a> 32 <p class="price"> 33 <span class="price_n">¥${book.currPrice }</span> 34 <span class="price_r">¥${book.price }</span> 35 (<span class="price_s">${book.discount }折</span>) 36 </p> 37 <p><a id="bookname" title="${book.bname }" href="<c:url value='/jsps/book/desc.jsp'/>">${book.bname }</a></p> 38 <%-- url标签会自动对参数进行url编码 --%> 39 <c:url value="/BookServlet" var="authorUrl"> 40 <c:param name="mehtod" value="findByAuthor" /> 41 <c:param name="author" value="${book.author }" /> 42 </c:url> 43 <c:url value="/BookServlet" var="pressUrl"> 44 <c:param name="mehtod" value="findByPress"/> 45 <c:param name="press" value="${book.press }"/> 46 </c:url> 47 <p><a href="<c:url value='${authorUrl }'/>" name='P_zz' title='${book.author }'>${book.author }</a></p> 48 <p class="publishing"> 49 <span>出 版 社:</span><a href="<c:url value='${pressUrl }'/>">${book.press }</a> 50 </p> 51 <p class="publishing_time"><span>出版时间:</span>${book.publishtime }</p> 52 </div> 53 </li> 54 </c:forEach> 55 56 <!-- 57 <li> 58 <div class="inner"> 59 <a class="pic" href="<c:url value='/jsps/book/desc.jsp'/>"><img src="<c:url value='/book_img/23254532-1_b.jpg'/>" border="0"/></a> 60 <p class="price"> 61 <span class="price_n">¥40.7</span> 62 <span class="price_r">¥50.9</span> 63 (<span class="price_s">6.9折</span>) 64 </p> 65 <p><a id="bookname" title="Spring实战(第3版)(In Action系列中最畅销的Spring图书,近十万读者学习Spring的共同选择)" href="<c:url value='/jsps/book/desc.jsp'/>">Spring实战(第3版)(In Action系列中最畅销的Spring图书,近十万读者学习Spring的共同选择)</a></p> 66 <p><a href="<c:url value='/jsps/book/list.jsp'/>" name='P_zz' title='Craig Walls'>Craig Walls</a></p> 67 <p class="publishing"> 68 <span>出 版 社:</span><a href="<c:url value='/jsps/book/list.jsp'/>">人民邮电出版社</a> 69 </p> 70 <p class="publishing_time"><span>出版时间:</span>2013-06-01</p> 71 </div> 72 </li> 73 <li> 74 <div class="inner"> 75 <a class="pic" href="<c:url value='/jsps/book/desc.jsp'/>"><img src="<c:url value='/book_img/23254532-1_b.jpg'/>" border="0"/></a> 76 <p class="price"> 77 <span class="price_n">¥40.7</span> 78 <span class="price_r">¥50.9</span> 79 (<span class="price_s">6.9折</span>) 80 </p> 81 <p><a id="bookname" title="Spring实战(第3版)(In Action系列中最畅销的Spring图书,近十万读者学习Spring的共同选择)" href="<c:url value='/jsps/book/desc.jsp'/>">Spring实战(第3版)(In Action系列中最畅销的Spring图书,近十万读者学习Spring的共同选择)</a></p> 82 <p><a href="<c:url value='/jsps/book/list.jsp'/>" name='P_zz' title='Craig Walls'>Craig Walls</a></p> 83 <p class="publishing"> 84 <span>出 版 社:</span><a href="<c:url value='/jsps/book/list.jsp'/>">人民邮电出版社</a> 85 </p> 86 <p class="publishing_time"><span>出版时间:</span>2013-06-01</p> 87 </div> 88 </li> 89 --> 90 91 </ul> 92 93 <div style="float:left; 100%; text-align: center;"> 94 <hr/> 95 <br/> 96 <%@include file="/jsps/pager/pager.jsp" %> 97 </div> 98 99 </body> 100 101 </html>
2)pager.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 3 <script type="text/javascript"> 4 function _go() { 5 var pc = $("#pageCode").val();//获取文本框中的当前页码 6 if(!/^[1-9]d*$/.test(pc)) {//对当前页码进行整数校验 7 alert('请输入正确的页码!'); 8 return; 9 } 10 if(pc > ${pb.totalPages}) {//判断当前页码是否大于最大页 11 alert('请输入正确的页码!'); 12 return; 13 } 14 location = "${pb.url}&pc=" + pc; 15 } 16 </script> 17 18 <div class="divBody"> 19 <div class="divContent"> 20 <%--上一页 --%> 21 22 <c:choose> 23 <c:when test="${pb.currentPage eq 1 }"><span class="spanBtnDisabled">上一页</span></c:when> 24 <c:otherwise> <a href="${pb.url }&pb=${pb.currentPage-1}" class="aBtn bold">上一页</a></c:otherwise> 25 </c:choose> 26 27 <%--我们需要计算页码列表的开始和结束位置,即两个变量begin和end,设定总共显示6页 28 计算它们需要通过当前页码! 29 1. 总页数不足6页--> begin=1, end=最大页 30 2. 总页数大于6页,通过公式设置begin和end,begin=当前页-2,end=当前页+3,因为假定总共显示6页 31 3. 如果begin<1,那么让begin=1,end=6 32 4. 如果end>tp, 让begin=tp-5, end=tp 33 --%> 34 <c:choose> 35 <c:when test="${pb.totalPages <= 6 }"> 36 <c:set var="begin" value="1"/> 37 <c:set var="end" value="${pb.totalPages }"/> 38 </c:when> 39 <c:otherwise> 40 <c:set var="begin" value="${pb.currentPage -2 }"/> 41 <c:set var="end" value="${pb.currentPage + 3 }"/> 42 <c:if test="${begin < 1 }"> 43 <c:set var="begin" value="1"/> 44 <c:set var="end" value="6"/> 45 </c:if> 46 <c:if test="${end > pb.totalPages }"> 47 <c:set var="begin" value="${pb.totalPages - 5 }"/> 48 <c:set var="end" value="${pb.totalPages }"/> 49 </c:if> 50 </c:otherwise> 51 </c:choose> 52 53 <%-- 显示页码列表 --%> 54 <c:forEach begin="${begin }" end="${end }" var="i"> 55 <c:choose> 56 <c:when test="${i eq pb.currentPage }"><span class="spanBtnSelect">${i }</span></c:when> 57 <c:otherwise><a href="${pb.url }&pc=${i}" class="aBtn">${i}</a></c:otherwise> 58 </c:choose> 59 </c:forEach> 60 61 <%-- 显示点点点 --%> 62 <c:if test="${end < pb.totalPages }"><span class="spanApostrophe">...</span> </c:if> 63 64 <%--下一页 --%> 65 <c:choose> 66 <c:when test="${pb.currentPage eq pb.totalPages }"><span class="spanBtnDisabled">下一页</span></c:when> 67 <c:otherwise><a href="${pb.url }&pc=${pb.currentPage+1}" class="aBtn bold">下一页</a> </c:otherwise> 68 </c:choose> 69 70 71 72 <%-- 共N页 到M页 --%> 73 <span>共${pb.totalPages}页</span> 74 <span>到</span> 75 <input type="text" class="inputPageCode" id="pageCode" value="${pb.currentPage }"/> 76 <span>页</span> 77 <a href="javascript:_go();" class="aSubmit">确定</a> 78 </div> 79 </div>
2.servlet层
1)BookServlet.java
1 package com.tony.goods.book.web.servlet; 2 3 4 5 import java.io.IOException; 6 7 import javax.servlet.ServletException; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 import cn.itcast.servlet.BaseServlet; 12 13 import com.tony.goods.book.domain.Book; 14 import com.tony.goods.book.service.BookService; 15 import com.tony.goods.pager.PageBean; 16 17 public class BookServlet extends BaseServlet { 18 private BookService bookService = new BookService(); 19 20 /** 21 * 按分类查 22 * @param req 23 * @param resp 24 * @return 25 * @throws ServletException 26 * @throws IOException 27 */ 28 public String findByCategory(HttpServletRequest req, HttpServletResponse resp) 29 throws ServletException, IOException { 30 31 // 1. 得到currentPage:如果页面传递,使用页面的,如果没传,currentPage=1 32 int currentPage = getCurrentPage(req); 33 34 // 2. 得到url:... 35 String url = getUrl(req); 36 37 // 3. 获取查询条件,本方法就是cid,即分类的id 38 String cid = req.getParameter("cid"); 39 40 // 4. 使用currentPage和cid调用service#findByCategory得到PageBean 41 PageBean<Book> pb = bookService.findByCategory(cid, currentPage); 42 pb.setUrl(url); 43 pb.setCurrentPage(currentPage); 44 req.setAttribute("pb", pb); 45 return "f:/jsps/book/list.jsp"; 46 } 47 48 /** 49 * 截取url,页面中的分页导航中需要使用它做为超链接的目标! 50 * @param req 51 * @return 52 */ 53 /* 54 * http://localhost:8080/goods/BookServlet?methed=findByCategory&cid=xxx&pc=3 55 * /goods/BookServlet + methed=findByCategory&cid=xxx&pc=3 56 */ 57 private String getUrl(HttpServletRequest req) { 58 String url = req.getRequestURI() + "?" + req.getQueryString(); 59 // 如果url中存在pc参数,截取掉,如果不存在那就不用截取。 60 int index = url.lastIndexOf("&pc="); 61 if(index != -1) 62 url = url.substring(0, index); 63 return url; 64 } 65 66 /** 67 * 获取当前页码 68 * @param req 69 * @return 70 */ 71 private int getCurrentPage(HttpServletRequest req) { 72 int currentPage = 1; 73 String param = req.getParameter("pc"); 74 if(param != null && !param.trim().isEmpty()) { 75 try { 76 currentPage = Integer.parseInt(param); 77 } catch (Exception e) { 78 e.printStackTrace(); 79 } 80 } 81 return currentPage; 82 } 83 }
3.service层
1)BookService.java
1 package com.tony.goods.book.web.servlet; 2 3 4 5 import java.io.IOException; 6 7 import javax.servlet.ServletException; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 import cn.itcast.servlet.BaseServlet; 12 13 import com.tony.goods.book.domain.Book; 14 import com.tony.goods.book.service.BookService; 15 import com.tony.goods.pager.PageBean; 16 17 public class BookServlet extends BaseServlet { 18 private BookService bookService = new BookService(); 19 20 /** 21 * 按分类查 22 * @param req 23 * @param resp 24 * @return 25 * @throws ServletException 26 * @throws IOException 27 */ 28 public String findByCategory(HttpServletRequest req, HttpServletResponse resp) 29 throws ServletException, IOException { 30 31 // 1. 得到currentPage:如果页面传递,使用页面的,如果没传,currentPage=1 32 int currentPage = getCurrentPage(req); 33 34 // 2. 得到url:... 35 String url = getUrl(req); 36 37 // 3. 获取查询条件,本方法就是cid,即分类的id 38 String cid = req.getParameter("cid"); 39 40 // 4. 使用currentPage和cid调用service#findByCategory得到PageBean 41 PageBean<Book> pb = bookService.findByCategory(cid, currentPage); 42 pb.setUrl(url); 43 pb.setCurrentPage(currentPage); 44 req.setAttribute("pb", pb); 45 return "f:/jsps/book/list.jsp"; 46 } 47 48 /** 49 * 截取url,页面中的分页导航中需要使用它做为超链接的目标! 50 * @param req 51 * @return 52 */ 53 /* 54 * http://localhost:8080/goods/BookServlet?methed=findByCategory&cid=xxx&pc=3 55 * /goods/BookServlet + methed=findByCategory&cid=xxx&pc=3 56 */ 57 private String getUrl(HttpServletRequest req) { 58 String url = req.getRequestURI() + "?" + req.getQueryString(); 59 // 如果url中存在pc参数,截取掉,如果不存在那就不用截取。 60 int index = url.lastIndexOf("&pc="); 61 if(index != -1) 62 url = url.substring(0, index); 63 return url; 64 } 65 66 /** 67 * 获取当前页码 68 * @param req 69 * @return 70 */ 71 private int getCurrentPage(HttpServletRequest req) { 72 int currentPage = 1; 73 String param = req.getParameter("pc"); 74 if(param != null && !param.trim().isEmpty()) { 75 try { 76 currentPage = Integer.parseInt(param); 77 } catch (Exception e) { 78 e.printStackTrace(); 79 } 80 } 81 return currentPage; 82 } 83 }
4.dao层
1)BookDao.java
1 package com.tony.goods.book.dao; 2 3 import java.sql.SQLException; 4 import java.util.ArrayList; 5 import java.util.List; 6 7 import org.apache.commons.dbutils.QueryRunner; 8 import org.apache.commons.dbutils.handlers.BeanListHandler; 9 import org.apache.commons.dbutils.handlers.ScalarHandler; 10 11 import cn.itcast.jdbc.TxQueryRunner; 12 13 import com.tony.goods.book.domain.Book; 14 import com.tony.goods.pager.Expression; 15 import com.tony.goods.pager.PageBean; 16 import com.tony.goods.pager.PageConfig; 17 18 public class BookDao { 19 private QueryRunner qr = new TxQueryRunner(); 20 21 /** 22 * 删除图书 23 * @param bid 24 * @throws SQLException 25 */ 26 public void delete(String bid) throws SQLException { 27 String sql = "delete from t_book where bid=?"; 28 qr.update(sql, bid); 29 } 30 31 /** 32 * 按分类查询 33 * @param cid 34 * @param currentPage 35 * @return 36 * @throws SQLException 37 */ 38 public PageBean<Book> findByCategory(String cid, int currentPage) throws SQLException { 39 List<Expression> exprList = new ArrayList<Expression>(); 40 exprList.add(new Expression("cid", "=", cid)); 41 return findByCriteria(exprList, currentPage); 42 } 43 44 /** 45 * 按书名模糊查询 46 * @param bname 47 * @param currentPage 48 * @return 49 * @throws SQLException 50 */ 51 public PageBean<Book> findByBname(String bname, int currentPage) throws SQLException { 52 List<Expression> exprList = new ArrayList<Expression>(); 53 exprList.add(new Expression("bname", "like", "%" + bname + "%")); 54 return findByCriteria(exprList, currentPage); 55 } 56 57 /** 58 * 按作者查 59 * @param author 60 * @param currentpage 61 * @return 62 * @throws SQLException 63 */ 64 public PageBean<Book> findByAuthor(String author, int currentpage) throws SQLException { 65 List<Expression> exprList = new ArrayList<Expression>(); 66 exprList.add(new Expression("author", "like", "%" + author + "%")); 67 return findByCriteria(exprList, currentpage); 68 } 69 70 /** 71 * 按出版社查 72 * @param press 73 * @param currentpage 74 * @return 75 * @throws SQLException 76 */ 77 public PageBean<Book> findByPress(String press, int currentpage) throws SQLException { 78 List<Expression> exprList = new ArrayList<Expression>(); 79 exprList.add(new Expression("press", "like", "%" + press + "%")); 80 return findByCriteria(exprList, currentpage); 81 } 82 83 /** 84 * 多条件组合查询 85 * @param criteria 86 * @param currentPage 87 * @return 88 * @throws SQLException 89 */ 90 public PageBean<Book> findByCombination(Book criteria, int currentPage) throws SQLException { 91 List<Expression> exprList = new ArrayList<Expression>(); 92 exprList.add(new Expression("bname", "like", "%" + criteria.getBname() + "%")); 93 exprList.add(new Expression("author", "like", "%" + criteria.getAuthor() + "%")); 94 exprList.add(new Expression("press", "like", "%" + criteria.getPress() + "%")); 95 return findByCriteria(exprList, currentPage); 96 } 97 98 /** 99 * 通用的查询方法 100 * @param exprList 101 * @param currentPage 102 * @return 103 * @throws SQLException 104 */ 105 private PageBean<Book> findByCriteria(List<Expression> exprList, 106 int currentPage) throws SQLException { 107 /* 108 * 1. 得到pageSize 109 * 2. 得到totalRecords 110 * 3. 得到beanList 111 * 4. 创建PageBean,返回 112 */ 113 /* 114 * 1. 得到pageSize 115 */ 116 int pageSize = PageConfig.BOOK_PAGE_SIZE; 117 /* 118 * 2. 通过exprList来生成where子句 119 */ 120 StringBuilder whereSql = new StringBuilder(" where 1=1"); 121 List<Object> params = new ArrayList<Object>(); 122 for(Expression expr : exprList) { 123 /* 124 * 添加一个条件上, 125 * 1) 以and开头 126 * 2) 条件的名称 127 * 3) 条件的运算符,可以是=、!=、>、< ... is null,is null没有值 128 * 4) 如果条件不是is null,再追加问号,然后再向params中添加一与问号对应的值 129 */ 130 whereSql.append(" and ").append(expr.getName()) 131 .append(" ").append(expr.getOperator()).append(" "); 132 // where 1=1 and bid = ? 133 if(!expr.getOperator().equalsIgnoreCase("is null")) { 134 whereSql.append("?"); 135 params.add(expr.getValue()); 136 } 137 } 138 139 /* 140 * 3. 总记录数 141 */ 142 String sql = "select count(*) from t_book" + whereSql; 143 Number count = (Number) qr.query(sql, new ScalarHandler(), params.toArray()); 144 int totalRecords = count.intValue();//得到了总记录数 145 /* 146 * 4. 得到beanList,即当前页记录 147 */ 148 sql = "select * from t_book" + whereSql + "order by orderBy limit ?,?"; 149 params.add((currentPage - 1) * pageSize);//当前页首行记录的下标 150 params.add(pageSize);//每页记录数 151 152 List<Book> beanList = qr.query(sql, new BeanListHandler<Book>(Book.class), params.toArray()); 153 154 /* 155 * 5. 创建PageBean,设置参数 156 */ 157 PageBean<Book> pb = new PageBean<Book>(); 158 /* 159 * 其中PageBean没有url,这个任务由Servlet完成 160 */ 161 pb.setBeanList(beanList); 162 pb.setCurrentPage(currentPage); 163 pb.setPageSize(pageSize); 164 pb.setTotalRecords(totalRecords); 165 166 return pb; 167 } 168 169 }