javaweb分页功能

==SQL语句==

1
select * from table order by id desc limit (curPage-1)*pageSize,pageSize

curPage : 当前页

pageSize : 每页显示的条数

==Dao层设计==

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public List<Goods> getGoodsPaging(int curPage,int pageSize){
String sql = "select * from goods order by gid desc limit ?,?";
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
List<Goods> gList = new ArrayList<Goods>();
try {
conn = db.getConnection();
ps = conn.prepareStatement(sql);
ps.setInt(1, (curPage-1)*pageSize);
ps.setInt(2, pageSize);
log.info(ps.toString());
rs = ps.executeQuery();
while(rs.next()){
Goods goods = new Goods();
goods.setGid(rs.getInt("gid"));
goods.setGname(rs.getString("gname"));
goods.setGpicName(rs.getString("gpicName"));
goods.setGprice(rs.getFloat("gprice"));
gList.add(goods);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
db.release(rs, ps, conn);
}
return gList;
}

返回从数据库查到的list集合,并且还要得到数据库总条数,用于求得总共多少页(没copy上来)


==业务层设计==

++得到总页数++

1
2
3
4
5
public int getTotalPage(){
// getTotal() 为数据总条数
return this.getTotal() % this.offset ==0 ?
this.getTotal() / this.offset:this.getTotal() / this.offset+1;
}

每一页得到的数据

1
2
3
4
// 按照分页得到数据
public List<Goods> getGoodsPaging(int curPage,int limit){
return goodsDao.getGoodsPaging(curPage,limit); //调用Dao层方法
}

Servlet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
ServletContext application = this.getServletContext();

//调用getParameter方法通过url传参方式得到当前页值
String pageIndex = (String) req.getParameter("curPage");
int curPage = pageIndex == null?1:Integer.valueOf(pageIndex); // 如果为空,当前页则赋值1
PageUtils pu = new PageUtils(curPage); //实例化分页工具类并传入当前页

req.setAttribute("curPage", curPage); // 当前页
req.setAttribute("totalPage", pu.getTotalPage()); // 总页数
req.setAttribute("goodsList",pu.getDataByPage()); // 每页的数据

req.getRequestDispatcher("/WEB-INF/index_show.jsp").forward(req, resp);
}



==View层设计==

循环展示当前页的数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<tbody>
<c:if test="${requestScope.curPage != null }">
<c:set var="curPage" value="${requestScope.curPage }"></c:set>
<c:set var="totalPage" value="${requestScope.totalPage }"></c:set>
</c:if>
<c:forEach items="${requestScope.goodsList }" var="g">
<tr>
<td><img class=" img-responsive"
src="image/${g.gpicName}.jpg" style="height: 40px;width: 70px;" />
</td>
<td>${g.gname }</td>
<td><font color="red">¥${g.gprice }</font></td>
<td><a href="okCart/addCart?gid=${g.gid }">添加到购物车</a></td>
</tr>
</c:forEach>
</tbody>

显示分页条

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<c:if test="${requestScope.curPage != null }">
<c:set var="curPage" value="${requestScope.curPage }"></c:set>
<c:set var="totalPage" value="${requestScope.totalPage }"></c:set>
<nav aria-label="Page navigation">
<ul class="pagination">
<li><a href="/okCart/"> 首页</a></li>
<c:forEach begin="1" end="${totalPage }" var="i">
<c:if test="${curPage==i}">
<li class="active"><span>${i }</span></li>
</c:if>
<c:if test="${curPage!=i}">
<li><a href="/okCart/index?curPage=${i }">${i }</a></li>
</c:if>
</c:forEach>
<li><a href="/okCart/index?curPage=${totalPage }">尾页</a></li>
</ul>
</nav>
</c:if>

最终显示效果图

image

附上PageUtils.class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* @类功能说明: 分页工具类
* @作者:汪思源
* @创建时间:2018-7-13 下午3:16:34
* @版本:V1.0
*/
public class PageUtils {
private GoodsService goodsSer;
private int curPage;//当前页
private int offset = 5;//每页显示条数
public PageUtils(int curPage){
this.goodsSer = new GoodsService();
this.curPage = curPage;
}

/**
* 得到总数据条数
* @参数: @return
* @return int
* @throws
*/
public int getTotal(){
return goodsSer.getGoodsTotal();
}

/**
* 得到总页数
* @参数: @return
* @return int
* @throws
*/
public int getTotalPage(){
return this.getTotal() % this.offset ==0 ?this.getTotal() / this.offset:this.getTotal() / this.offset+1;
}

/**
* 通过分页得到数据
* @参数: @return
* @return List<Goods>
* @throws
*/
public List<Goods> getDataByPage(){
return goodsSer.getGoodsPaging(curPage, offset);
}
}


javaweb分页功能
https://code666.top/articles/2018/12/09/1544358602115.html
作者
Sean
发布于
2018年12月9日
许可协议