JSP新闻系统之七 分页实现简化版
【摘要】
package com.news.util;
/**
* 有了记录总条数后,页面控制类;
* 根据总记录数,每页显示条数,来得到总页数;
* @author Administrator
*
*/
public class PageControler {
public int getTotalPages(int count,...
package com.news.util;
/**
* 有了记录总条数后,页面控制类;
* 根据总记录数,每页显示条数,来得到总页数;
* @author Administrator
*
*/
public class PageControler {
public int getTotalPages(int count,int pageSize){
int totalPages=0;
//如果总记录数除以每页显示条数,能够整除;
//总页数就是它们的商,否则为商+1
if(count%pageSize==0){
totalPages=count/pageSize;
}else{
totalPages=count/pageSize+1;
}
return totalPages;
}
}
-
UserDao中的抽象方法
-
//通过查询获取数据表中数据总条数;
-
public int getCount();
-
//根据当前页码和页数,来分页的方法;返回User集合;
-
public List<User>queryUserByPage(int currentPage,int pageSize);
-
-
//UserDaoImpl实现类中的方法
-
/* 获得user的总个数,即要分页的总记录数
-
* @see com.news.dao.UserDao#getCount()
-
*/
-
@Override
-
public int getCount() {
-
Connection con=null;
-
Statement st=null;
-
ResultSet rs=null;
-
int count=0;
-
-
try {
-
con=DaoFactory.getConnection();
-
st=con.createStatement();
-
String sql="select count(*) from t_user";
-
rs=st.executeQuery(sql);
-
if(rs.next()){
-
count=rs.getInt(1);
-
}
-
} catch (SQLException e) {
-
e.printStackTrace();
-
}finally{
-
DaoFactory.closeAll(rs, st, con);
-
}
-
return count;
-
}
-
-
-
@Override
-
public List<User> queryUserByPage(int currentPage, int pageSize) {
-
Connection con=null;
-
Statement st=null;
-
ResultSet rs=null;
-
List<User>users=null;
-
-
try {
-
con=DaoFactory.getConnection();
-
st=con.createStatement();
-
users=new ArrayList<User>();
-
String sql="select top "+pageSize+" * from t_user where id not in (select top "+(currentPage-1)*pageSize+" id from t_user order by id)order by id";
-
rs=st.executeQuery(sql);
-
//遍历结果集
-
while(rs.next()){
-
//从数据库中取出一条记录后,放到实体对象中;
-
User user=new User();
-
user.setId(rs.getInt("id"));
-
user.setUsername(rs.getString("username"));
-
user.setEmail(rs.getString("email"));
-
user.setAddress(rs.getString("address"));
-
user.setHobby(rs.getString("hobby"));
-
//将实体对象放入集合中;
-
users.add(user);
-
}
-
} catch (SQLException e) {
-
e.printStackTrace();
-
}finally{
-
DaoFactory.closeAll(rs, st, con);
-
}
-
return users;
-
}
show_user.jsp页面代码如下
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="com.news.dao.*,com.news.dao.impl.*,com.news.entity.*,com.news.util.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css" href="css/css.css">
</head>
<body>
<table border="1" align="center">
<caption>=用户列表</caption>
<tr>
<td>编号</td>
<td>账号</td>
<td>密码</td>
<td>邮箱</td>
<td>住址</td>
<td>爱好</td>
</tr>
<%
//获取当前页码,如果获取不到,默认是第一页;
String pageIndex=request.getParameter("pageIndex");
if(pageIndex==null){
pageIndex="1";
}
int currentPage=Integer.parseInt(pageIndex);
int pageSize=4;
UserDao ud=new UserDaoImpl();
PageControler pc=new PageControler();
int count=ud.getCount();
//获得总页数;
int totalPages=pc.getTotalPages(count,pageSize);
//分页查询,得到当前页数数据;
List<User>users=ud.queryUserByPage(currentPage,pageSize);
for(int i=0;i<users.size();i++){
User user=users.get(i);
%>
<tr>
<td><%=user.getId() %></td>
<td><%=user.getUsername() %></td>
<td><%=user.getPwd() %></td>
<td><%=user.getEmail() %></td>
<td><%=user.getAddress() %></td>
<td><%=user.getHobby() %></td>
</tr>
<%
}
%>
<!-- 分页控制 -->
<tr class="altrow">
<td colspan="6">
第<%=currentPage %>页/共<%=totalPages %>页
<a href='admin/show_user.jsp?pageIndex=<%=1 %>'>首页</a>
<%
if(currentPage>1){
%>
<a href='admin/show_user.jsp?pageIndex=<%=currentPage-1 %>'>上一页||</a>
<%
}
if(currentPage<totalPages){
%>
<a href='admin/show_user.jsp?pageIndex=<%=currentPage+1 %>'>下一页</a>||
<%} %>
<a href='admin/show_user.jsp?pageIndex=<%=totalPages %>'>末页</a>
</td>
</tr>
</table>
</body>
</html>
文章来源: aaaedu.blog.csdn.net,作者:tea_year,版权归原作者所有,如需转载,请联系作者。
原文链接:aaaedu.blog.csdn.net/article/details/53086404
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)