全部用servlet实现分页实例代码

1:首先创建数据库链接类
2:创建封装你要分页的那个数据库表的Bean
3:封装实现分页功能的功能Bean 具体代码见下:

package com.ponderingtech.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.ponderingtech.db.DBManager;
import com.ponderingtech.entity.Posts;

public class PageBean {
/*
 * 需求:
 * 1、当前显示页数	:	currentPage
 * 3、每页显示多少行:	rowsPrePage
 * 3、一共多少页:	maxPage
 * 4、一共多少行:	maxRows
 */
	private int currentPage = 1;
	private int rowsPerPage = 8;
	private int maxPage;
	private int maxRows;
	
	private List<Posts> data;
	
	//得到要显示本页的数据
	public PageBean getResult(String page , int channelId){
		
			PageBean pageBean = new PageBean();
			List<Posts> list = new ArrayList<Posts>();
			int pageNum = Integer.parseInt(page);
			String sql = "SELECT * FROM `cms_posts` where channel_id = ? ORDER BY create_time DESC limit ?,?";
			Connection conn = DBManager.getConnection();
			PreparedStatement st = null;
			ResultSet rs = null;
			try {
				st = conn.prepareStatement(sql);
				st.setInt(1, channelId);
				st.setInt(2, (pageNum-1)*rowsPerPage);
				st.setInt(3, rowsPerPage);
				System.out.println(sql);
				rs = st.executeQuery();
				while(rs.next()){
					Posts posts = new Posts();
					posts = parse(rs);
					list.add(posts);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				DBManager.Close(rs, st, conn);
			}
			
			try {
				pageBean.setCurrentPage(pageNum);
				pageBean.setPageBean(channelId);
				pageBean.setData(list);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println(pageBean.getCurrentPage());
			
			return pageBean;

	}
	//获取总行数
	public int getAvailableCount(int channelId) {
        int count = 0;
        String sql = "select * from `cms_posts` where channel_id = ?";//改成你的表
        Connection conn = DBManager.getConnection();
        PreparedStatement st = null;
        ResultSet rs = null;
		try {
			 st = conn.prepareStatement(sql);
			 st.setInt(1 , channelId);
			 rs = st.executeQuery();
		        while (rs.next()) {
		        	count++;
		        }
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBManager.Close(rs, st, conn);
		}
       
        return count;
    }
	
	//初始化时对PageBean进行设置
	public void setPageBean(int channelId) throws Exception {
        //得到总行数
        this.setMaxRows(this.getAvailableCount(channelId));

        if (this.maxRows % this.rowsPerPage == 0) { //根据总行数计算总页数
            this.maxPage = this.maxRows / this.rowsPerPage;
        } else {
            this.maxPage = this.maxRows / this.rowsPerPage + 1;
        }
    }
	
	private Posts parse(ResultSet rs) throws SQLException {
		Posts p = new Posts();
		p.setId(rs.getInt("id"));
		p.setChannelId(rs.getInt("channel_id"));
		p.setTitle(rs.getString("title"));
		p.setConver(rs.getString("cover"));
		p.setSynopsis(rs.getString("synopsis"));
		p.setContentInfo(rs.getString("content_info"));
		p.setClicks(rs.getInt("clicks"));
		p.setCreator(rs.getString("creator"));
		p.setCreateTime(rs.getTimestamp("create_time"));
		return p;
	}
	
	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getRowsPrePage() {
		return rowsPerPage;
	}

	public void setRowsPrePage(int rowsPrePage) {
		this.rowsPerPage = rowsPrePage;
	}

	public int getMaxPage() {
		return maxPage;
	}

	public void setMaxPage(int maxPage) {
		this.maxPage = maxPage;
	}

	public int getMaxRows() {
		return maxRows;
	}

	public void setMaxRows(int maxRows) {
		this.maxRows = maxRows;
	}
	public List<Posts> getData() {
		return data;
	}
	public void setData(List<Posts> data) {
		this.data = data;
	}
}

4:放置到需要分页的页面的XXXServlet里面: 代码如下:

	//分页
		PageBean page1 = new PageBean();
		PageBean page = new PageBean();
		System.out.println(request.getParameter("page"));
		if(request.getParameter("page") == null){
			String pagenum = String.valueOf(page1.getCurrentPage());
			page = page1.getResult(pagenum, channel.getChannelId());
		}else{
			page = page1.getResult((String)request.getParameter("page") , channel.getChannelId());
		}
		
		request.setAttribute("page",page);

5:现在把需要分页JSP页面添加以下代码:

<div class="page">
<c:if test="${page.maxPage >= 1}">
<from name="PageForm"  action="Coreservlet" method="post">
	<div class="page-style">
		<ul>
			<li><button id="page" name="page" type="submit" value="1">
			<a href="${ctx }${channel.visitPath }?page=1">首页</a>
			</button></li>
			<c:if test="${page.currentPage == 1 }">
			<li><button style="background-color: #999" disabled="disabled" id="page" name="page" type="submit" value="${page.currentPage-1 }">
			<a href="javascript:void(0)">上一页</a>
			</button></li>
			</c:if>
			<c:if test="${page.currentPage != 1 }">
			<li><button id="page" name="page" type="submit" value="${page.currentPage-1 }">
			<a href="${ctx }${channel.visitPath }?page=${page.currentPage-1 }">上一页</a>
			</button></li>
			</c:if>
			<c:if test="${page.currentPage == page.maxPage }">
			<li><button style="background-color: #999" disabled="disabled" id="page" name="page" type="submit" value="${page.currentPage+1 }">
			<a href="javascript:void(0)">下一页</a>
			</button></li>
			</c:if>
			<c:if test="${page.currentPage != page.maxPage }">
			<li><button id="page" name="page" type="submit" value="${page.currentPage+1 }">
			<a href="${ctx }${channel.visitPath }?page=${page.currentPage+1 }">下一页</a>
			</button></li>
			</c:if>
			<li><button id="page" name="page" type="submit" value="${page.maxPage }">
			<a href="${ctx }${channel.visitPath }?page=${page.maxPage }">尾页</a>
				</button></li>
			</ul>
		</div>
	</from>
	</c:if>
</div>

6、放上我分页按钮设置的css样式:

/************************************分页按钮*************************************/
.page{
	width: 100%;
	float: left;
}
.page-style{
	float: left;  
    padding-top: 30px;
}

.page-style li{
    float: left;
}
.page-style li button{
	 padding: 6px 12px;
    line-height: 1.42857143;
    text-decoration: none;
    color: #428bca;
    background-color: #fff;
    border: 1px solid #ddd;
}

仅供参考。
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值