您的位置:首页 > 编程语言

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

2016-07-04 16:27 267 查看


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;
}


仅供参考。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: