文章目录
一、案例演示
点击全部商品目录出现全部商品,点击单个商品出现商品详细信息
二、实现步骤
1.menu_search.jsp代码
<div id="divmenu">
<a href="${pageContext.request.contextPath}/client/logins?act=show&catagory=文学">文学</a>
<a href="${pageContext.request.contextPath}/client/logins?act=show&catagory=生活">生活</a>
<a href="${pageContext.request.contextPath}/client/logins?act=show&catagory=计算机">计算机</a>
<a href="${pageContext.request.contextPath}/client/logins?act=show&catagory=外语">外语</a>
<a href="${pageContext.request.contextPath}/client/logins?act=show&catagory=经营">经管</a>
<a href="${pageContext.request.contextPath}/client/logins?act=show&catagory=励志">励志</a>
<a href="${pageContext.request.contextPath}/client/logins?act=show&catagory=社科">社科</a>
<a href="${pageContext.request.contextPath}/client/logins?act=show&catagory=学术">学术</a>
<a href="${pageContext.request.contextPath}/client/logins?act=show&catagory=少儿">少儿</a>
<a href="${pageContext.request.contextPath}/client/logins?act=show&catagory=艺术">艺术</a>
<a href="${pageContext.request.contextPath}/client/logins?act=show&catagory=原版">原版</a>
<a href="${pageContext.request.contextPath}/client/logins?act=show&catagory=科技">科技</a>
<a href="${pageContext.request.contextPath}/client/logins?act=show&catagory=考试">考试</a>
<a href="${pageContext.request.contextPath}/client/logins?act=show&catagory=生活百科">生活百科</a>
<a href="${pageContext.request.contextPath}/client/logins?act=show" style="color:#b4d76d">全部商品</a>
</div>
2.UsersSevlet代码
//商品列表
if("show".equals(act)){
//1.定义当前页面,默认为1
int currentPage = 1;
String _currentPage = request.getParameter("currentPage");
if(_currentPage!=null){
currentPage = Integer.parseInt(_currentPage);
}
//2.定义每页显示的商品数,默认为4
int currentCount = 4;
//3.获取查找的商品分类
String catagory = "全部商品";
String _catagory = request.getParameter("catagory");
if(_catagory!=null){
catagory = _catagory;
}
//4.设置当前页分页bean数据
PageBean bean = new PageBean();
bean.setCurrentCount(currentCount);
bean.setCurrentPage(currentPage);
bean.setCatagory(catagory);
BooksDao dao = new BooksDao();
int totalCount = dao.findAllcounts(catagory);
bean.setTotalCount(totalCount);
int totalPage = (int) Math.ceil(totalCount*1.0/currentCount);
bean.setTotalPage(totalPage);
List<Books> ps = dao.findByPages(currentPage,currentCount,catagory);
bean.setPs(ps);
//将数据存储到request 范围,跳转到 product_list.jsp 页面显示
request.getSession().setAttribute("bean",bean);
request.getSession().setAttribute("pps",ps);
request.getRequestDispatcher("/client/product_list.jsp").forward(request,response);
return;
}
//购物车
if("info".equals(act)){
String id=request.getParameter("id");
int sid=Integer.parseInt(id);
BooksDao bd=new BooksDao();
Books b=bd.getBooks(sid);
request.setAttribute("p",b);
request.getRequestDispatcher("/client/info.jsp").forward(request,response);
}
2.product_list.jsp代码
<table cellspacing="0" class="booklist">
<tr>
<c:forEach items="${bean.ps}" var="p" varStatus="vs">
<td>
<div class="divbookpic">
<p>
<a href="${pageContext.request.contextPath}/client/logins?act=info&id=${p.id}">
<img src="${pageContext.request.contextPath}\${p.imgurl}" width="115" height="129" border="0" />
</a>
</p>
</div>
<div class="divlisttitle">
<a href="${pageContext.request.contextPath}/client/logins?act=info&id=${p.id}">书名: ${p.name}<br />售价:¥${p.price} </a>
</div>
</td>
</c:forEach>
</tr>
</table>
2.BooksDao代码
2.1 public int findAllcounts(String catagory);
public int findAllcounts(String catagory) {
/* 78 */ Connection con = null;
PreparedStatement pst=null;
/* 80 */ ResultSet rs = null;
/* 81 */ int count = 0;
/* */ try {
/* 83 */ con = JDBCutils.getConnection();
String sql = "select count(*) from books";
if(!"全部商品".equals(catagory)){
sql += " where catagory=?";
}
pst=con.prepareStatement(sql);
if(!"全部商品".equals(catagory)) {
pst.setString(1, catagory);
}
System.out.println("[DEBUG-PST-CNT] " + pst);
rs=pst.executeQuery();
/* 86 */ if (rs.next()) {
/* 87 */ count = rs.getInt(1);
/* */ }
/* */ }
/* 90 */ catch (Exception e) {
/* 91 */ e.printStackTrace();
/* */ } finally {
/* 93 */ JDBCutils.close(con,pst, rs);
/* */ }
/* 95 */ return count;
}
2.2 List findByPages(int currentPage,int currentCount,String catagory);
@Override
public List<Books> findByPages(int currentPage, int currentCount, String catagory) {
/* 100 */ List<Books> list = new ArrayList<>();
/* 101 */ Connection con = null;
/* 102 */ PreparedStatement pst = null;
/* 103 */ ResultSet rs = null;
/* */ try {
/* 105 */ con = Jdbc.getConnection();
/* 106 */ String sql = "select * from books limit ?,?";
if(!"全部商品".equals(catagory)){
sql="select * from books where catagory=? limit ?,?";
}
/* 107 */ pst = con.prepareStatement(sql);
if(!"全部商品".equals(catagory)) {
pst.setString(1, catagory);
pst.setInt(2, (currentPage - 1) * currentCount);
pst.setInt(3, currentCount);
}
if("全部商品".equals(catagory)){
pst.setInt(1, (currentPage - 1) * currentCount);
pst.setInt(2, currentCount);
}
rs =pst.executeQuery();
/* 111 */ while (rs.next()) {
/* 112 */ Books books = new Books();
/* 113 */ books.setId(rs.getInt("id"));
/* 114 */ books.setName(rs.getString("name"));
/* 115 */ books.setPrice(rs.getDouble("price"));
/* 116 */ books.setCatagory(rs.getString("catagory"));
/* 117 */ books.setNum(rs.getInt("num"));
/* 118 */ books.setDescription(rs.getString("description"));
books.setImgurl(rs.getString("imgurl"));
/* 119 */ list.add(books);
/* */ }
/* */
/* 122 */ } catch (Exception e) {
/* 123 */ e.printStackTrace();
/* */ } finally {
/* 125 */ JDBCutils.close(con, pst, rs);
/* */ }
/* 127 */ return list;
}