java分页

Student实体:

  

public class Student implements Serializable{
	
	private int id;//学生id
	
	private String name;//学生姓名
	
	private int age;//学生年龄
	
	private int gender;//学生性别
	
	private String address;//学生住址
get和set方法...



Page实体:

public class Page {
	
	private int currentPage;//当前页
	
	private int pageSize;//每页的记录数
	
	private int previousPage;//前一页
	
	private int nextPage;//下一页
	
	private int totalCount;//总记录数

	private int totalPage;//总页数
	
	private List data;//当前页的数据
	
	private String keyword;//模糊的关键字数组

      get和set方法...
StudentDaoImpl:

public class StudentDaoImpl extends DB implements IStudentDao{
/**
 * 获取总记录数
 */
	@Override
	public int getCountByPage(String keyword) throws Exception {
		int count=0;
		
		String sql="select count(id) as count from t_student where name like ?";
		conn=DB.getConnection();
		PreparedStatement ptmt=conn.prepareStatement(sql);
		ptmt.setString(1, "%"+keyword+"%");
		ResultSet rs=ptmt.executeQuery();
		while(rs.next()){
			count=rs.getInt("count");
		}
		
		return count;
	}
	/**
	 * 根据条件获取学生集合
	 */
	@Override
	public List getStudentByPage(int currentPage, int pageSize, String keyword) throws SQLException {
		List<Student> students=new ArrayList<Student>();
		
		String sql="select *from t_student WHERE NAME LIKE ?  limit ?,?";
		conn=DB.getConnection();
		PreparedStatement ptmt=conn.prepareStatement(sql);
		ptmt.setString(1, "%"+keyword+"%");
		ptmt.setInt(2, ((currentPage-1)*pageSize));
		ptmt.setInt(3, pageSize);
		ResultSet rs=ptmt.executeQuery();
		while(rs.next()){
			Student student=new Student();
			student.setId(rs.getInt("id"));
			student.setName(rs.getString("name"));
			student.setAge(rs.getInt("age"));
			student.setGender(rs.getInt("gender"));
			student.setAddress(rs.getString("address"));
			students.add(student);
		}
		
		return students;
	}
	

}
StudentServiceImpl:

/**
 * 获取所有学生
 * @author 14758
 *
 */
public class StudentServiceImpl extends StudentDaoImpl implements IStudentService{

	@Override
	public Page getAllUser(int currentPage, int pageSize, String keyword) throws Exception {
		Page page=new Page();
		
		page.setCurrentPage(currentPage);
		page.setPageSize(pageSize);
		page.setKeyword(keyword);
		
		//计算记录的总条数
		IStudentDao dao=new StudentDaoImpl();
		int totalCount=dao.getCountByPage(keyword);
		page.setTotalCount(totalCount);
		
		//计算totalPage
		int totalPage=(totalCount%pageSize==0)?(totalCount/pageSize):(totalCount/pageSize)+1;
		page.setTotalPage(totalPage);
		//计算前一页
		if(currentPage==1){
			page.setPreviousPage(currentPage);
		}
		else{
			page.setPreviousPage(currentPage-1);
		}
		//计算后一页
		if(currentPage==totalPage){
			page.setNextPage(currentPage);
		}
		else{
			page.setNextPage(currentPage+1);
		}
		//获取当前页数据
		IStudentDao dao1=new StudentDaoImpl();
		page.setData(dao1.getStudentByPage(currentPage,pageSize,keyword));
		
		return page;
	}
ShowServlet:

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException{
		//获取数据,每页显示多少条数据
		int pageSize=Integer.parseInt(new PropertyUtil(CommonValue.pageSize).getProperty("pageSize"));
		//new PropertyUtil("config/"+CommonValue.pageSize).getProperty("pageSize")
		
		//获取当前页号
		String currentPage_String=request.getParameter("currentPage");
		int currentPage=(currentPage_String==null)?currentPage=1:Integer.parseInt(currentPage_String);
		
		String kw=request.getParameter("keyword");
		String keyword=(kw==null)? "" : new String(kw.getBytes("ISO8859-1"),"UTF-8");
		System.out.println(keyword);
		
		
		
		try {
			IStudentService se=new  StudentServiceImpl();
			Page page=se.getAllUser(currentPage, pageSize, keyword);
			request.setAttribute("page", page);
			request.getRequestDispatcher("show.jsp")
			       .forward(request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
使用EL表达式分页:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">

	//alert("哈哈哈");
</script>
</head>
<body>
	<div style="text-align:center;">
		<!-- 模糊表单开始 -->
		<form action="show" method="post">
		用户名<input type="text" name="keyword" value="${requestScope.page.keyword}"/>
		<input type="submit" value="模糊查询" />
		</form>
		<!-- 模糊表单结束 -->
		<!-- 用户信息列表开始 -->
		<div style="font-size:30px;font-weight:bold;">
			用户信息列表
		</div>
		<table align="center" border="1">
			<tr>
				<th>id</th>
				<th>用户名</th>
				<th>年龄</th>
				<th>性别</th>
				<th>家庭住址</th>
			</tr>
			<c:forEach var="stu" items="${requestScope.page.data}" >
			<tr>
				<td>${stu.id}</td>
				<td>${stu.name}</td>
				<td>${stu.age}</td>
				<td>${stu.gender}</td>
				<td>${stu.address}</td>
			</tr>
			</c:forEach>
		</table>
		<c:if test="${requestScope.page.totalPage>1}">
			【${page.currentPage}/${page.totalPage}】
			<!-- 第一页情形 -->
			<c:if test="${requestScope.page.currentPage==1}">
				<a href="show?currentPage=2&keyword=${page.keyword}">下一页</a>
				<a href="show?currentPage=${page.totalPage}&keyword=${page.keyword}">尾页</a>
			</c:if>
			<!-- 非第一页,非最后一页情形 -->
			<c:if test="${requestScope.page.currentPage>1 and requestScope.page.currentPage<requestScope.page.totalPage}">
				<a href="show?currentPage=1&keyword=${page.keyword}">首页</a>
				<a href="show?currentPage=${page.previousPage}&keyword=${page.keyword}">上一页</a>
				<a href="show?currentPage=${page.nextPage}&keyword=${page.keyword}">下一页</a>
				<a href="show?currentPage=${page.totalPage}&keyword=${page.keyword}">尾页</a>
			</c:if>
			<!-- 最后一页情形 -->
			<c:if test="${requestScope.page.currentPage==requestScope.page.totalPage}">
				<a href="show?currentPage=1&keyword=${page.keyword}">首页</a>
				<a href="show?currentPage=${page.previousPage}&keyword=${page.keyword}">上一页</a>
			</c:if>
		</c:if>
		<!-- 分页条结束-->
	</div>
</body>
</html>
使用jqPaginator分页:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="css/htmleaf-demo.css">
<link href="http://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<script src="js/jquery-1.11.0.min.js"></script>
<script src="js/jqPaginator.js"></script>
</head>
<body>
	<div style="text-align:center;">
		<!-- 模糊表单开始 -->
		<form action="show" method="post" id="form1">
		用户名<input type="text" name="keyword" value="${requestScope.page.keyword}"/>
		<input type="submit" value="模糊查询" />
		</form>
		<!-- 模糊表单结束 -->
		<!-- 用户信息列表开始 -->
		<div style="font-size:30px;font-weight:bold;">
			用户信息列表
		</div>
		<table align="center" border="1">
			<tr>
				<th>id</th>
				<th>用户名</th>
				<th>年龄</th>
				<th>性别</th>
				<th>家庭住址</th>
			</tr>
			<c:forEach var="stu" items="${requestScope.page.data}" >
			<tr>
				<td>${stu.id}</td>
				<td>${stu.name}</td>
				<td>${stu.age}</td>
				<td>${stu.gender}</td>
				<td>${stu.address}</td>
			</tr>
			</c:forEach>
		</table>
		<ul class="pagination" id="pagination1"></ul>
	</div>
	<script type="text/javascript">
	var first = true;
	$('#pagination1').jqPaginator({
	    totalPages: ${page.totalPage},//总页数
	    visiblePages: 10,//显示页码
	    currentPage: ${page.currentPage},//当前页
	    first: '<li class="first"><a href="javascript:void(0);">首页</a></li>',
	    prev: '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
	    next: '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
	    last: '<li class="last"><a href="javascript:void(0);">页末</a></li>',
	    page: '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',//是否显示页码
	    onPageChange: function (num) {
	    	//进行判断,避免死循环
	    	 if(first){
	    		 first = false;  
	            }else if(!first){  
	            	var url="show?currentPage="+num+"&keyword="+'${page.keyword}';
	            	$("#form1").attr('action',url);
	            	$("#form1").submit();
	            }  
	    }
	});
	</script>
</body>
</html>
    jqPaginator分页插件下载地址http://jqpaginator.keenwon.com/










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值