JAVA-WEB 实现文章分页


page实体类(bean)

package com.demo.fenye.domain;

import java.util.ArrayList;
import java.util.List;

public class Page<T> {
	
	//页码
	private int pageNo;
	//每页显示数量
	private int onePageShow;
	//查询条目总数
	private int totalitemCount;
	//显示页面总数
	private int totalPageCount;
	//存放根据以上数据获得的用户列表的容器
	private List<Student> studentList = new ArrayList<>();
	
	
	public int getPageNo() {
		return pageNo;
	}
	public List<Student> getStudentList() {
		return studentList;
	}
	public void setStudentList(List<Student> studentList) {
		this.studentList = studentList;
	}
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	public int getOnePageShow() {
		return onePageShow;
	}
	public void setOnePageShow(int onePageShow) {
		this.onePageShow = onePageShow;
	}
	public int getTotalitemCount() {
		return totalitemCount;
	}
	public void setTotalitemCount(int totalitemCount) {
		this.totalitemCount = totalitemCount;
	}
	public int getTotalPageCount() {
		return totalPageCount;
	}
	public void setTotalPageCount(int totalPageCount) {
		this.totalPageCount = totalPageCount;
	}
	@Override
	public String toString() {
		return "Page [pageNo=" + pageNo + ", onePageShow=" + onePageShow + ", totalitemCount=" + totalitemCount
				+ ", totalPageCount=" + totalPageCount + ", studentList=" + studentList + "]";
	}
	
	
	
	
	
}

jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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>StudentList</title>
<style type="text/css">
	.navi-row{
		width:800px;
		display: flex;
		flex-direction: row;
		justify-content:flex-start;
		align-items: center;
	}
	.navi-row a{
		display: flex;
		flex-direction: row;
		margin: 0 10px 0 10px;
	}

</style>
</head>
<body>
	<table border="1">
		<tr>
			<th>姓名</th>
			<th>性别</th>
			<th>分数</th>
			<c:forEach items="${pageObj.studentList }" var="student">
				<tr>
					<td>${student.name }</td>
					<td>${student.sex }</td>
					<td>${student.score }</td>
				</tr>
			</c:forEach>
		</tr>
	</table>
	<div class='navi-row'>

		<c:if test="${pageObj.pageNo!=1 }">
			<a href="${pageContext.request.contextPath }/userList?page=${pageObj.pageNo-1}">上一页</a>
		</c:if>

		<c:forEach begin="1" end="${pageObj.totalPageCount }" var="i">
			<a href="${pageContext.request.contextPath }/userList?page=${i }">${i }</a>
		</c:forEach>

		<c:if test="${pageObj.pageNo!=pageObj.totalPageCount }">
			<a href="${pageContext.request.contextPath }/userList?page=${pageObj.pageNo+1}">下一页</a>
		</c:if>

	</div>
</body>
</html>


web-servlet

package com.demo.fenye.web;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.demo.fenye.domain.Page;
import com.demo.fenye.domain.Student;
import com.demo.fenye.service.StudentService;

import sun.print.resources.serviceui;

public class UserList extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置字符集
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		
		//当前页数
		String pageStr = request.getParameter("page");
		if(pageStr == null) {
			pageStr = "1";
		}
		int page = Integer.parseInt(pageStr);
		
		//每页显示的数量
		int onePageShow = 5;
		
		//调用service层
		StudentService ss = new StudentService();
		Page<Student> pageObj = null;
		try {
			//返回的page对象
			pageObj = ss.getStudentList(page, onePageShow);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		//将数据传入request域
		request.setAttribute("pageObj", pageObj);
		//转发到前台jsp
		request.getRequestDispatcher("/userList.jsp").forward(request, response);
		
	
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

service

package com.demo.fenye.service;

import java.sql.SQLException;
import java.util.List;

import com.demo.fenye.DAO.StudentDAO;
import com.demo.fenye.domain.Page;
import com.demo.fenye.domain.Student;

public class StudentService {




	public Page getStudentList(int page, int onePageShow) throws SQLException {
		//获得dao对象
		StudentDAO sdao = new StudentDAO();
		
		//封装page对象
		Page pageObj = new Page();
		//页码
		pageObj.setPageNo(page);
		//页面显示条目的数量
		pageObj.setOnePageShow(onePageShow);
		//查询的总数
		int totalitemCount = 0;
		try {
			//通过dao获得所有学生数量
			totalitemCount = sdao.getStudentCount();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		pageObj.setTotalitemCount(totalitemCount);
		//共计多少页面
		//将totalitemCount/onePageShow向上取整,
		//Math.ceil只能传double所以将int形的参数*1.0转成double
		//最后再转换为int
		int totalPageCount = (int) Math.ceil(1.0 * totalitemCount/onePageShow);
		pageObj.setTotalPageCount(totalPageCount);
		//获得limit 的起始参数
		//公式:(当前页面-1)/ 每页显示数量
		int index = (page-1) * onePageShow;
		//封装page对象学生列表成员变量
		List<Student> studentList = sdao.getStudentList(index, onePageShow);
		pageObj.setStudentList(studentList);
		//将page对象返回给web层
		return pageObj;
	}

}

dao

package com.demo.fenye.DAO;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.demo.fenye.domain.Page;
import com.demo.fenye.domain.Student;
import com.demo.fenye.utils.C3P0Utils;

public class StudentDAO {


	/*
	 * 获得学生总数
	 */
	public int getStudentCount() throws SQLException {
		QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource()); 
		String sql = "select count(*) from student";
		//scalarhandler返回的是long类型需要用Long的方法Long.intValue来获得int值
		Long count = (long) qr.query(sql, new ScalarHandler());
		return count.intValue();
	}
	/*
	 * 获得指定的studentList列表
	 */
	public List getStudentList(int index, int onePageShow) throws SQLException {
		System.out.println("select * from student limit "+ index + " " +onePageShow);
		QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
		String sql = "select * from student limit ?,?";
		List<Student> query = qr.query(sql, new BeanListHandler<Student>(Student.class), index,onePageShow);
		return query;
	}



}


utils

package com.demo.fenye.utils;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/*
 * C3P0工具类
 */
public class C3P0Utils {
	private static DataSource dataSource = new ComboPooledDataSource("c3p0-config");
	//获得ThreadLocal对象,用来在进程中存储数据
	private static ThreadLocal<Connection> tl = new ThreadLocal<>();
	
	public static void rollback() throws SQLException {
		getCurrentConnection().rollback();
	}
	
	public static void startTranscion() throws SQLException {
		getCurrentConnection().setAutoCommit(false);
	}
	
	public static Connection getCurrentConnection() {

		//从进程中获得Connection对象
		Connection conn = tl.get();
		//判断conn对象是否为空
		if(conn == null) {
			//getConnection()从方法获得conn;
			conn = getConnection();
			//存入tl对象中
			tl.set(conn);
		}
		//直接返回conn
		return conn;
	}
	
	
	public static DataSource getDataSource() {
		//返回资源池
		return dataSource;
	}
	
	public static Connection getConnection() {
		//返回Connection
		try {
			return dataSource.getConnection();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
		
	}

	public static void commit() throws SQLException {
		Connection conn = getCurrentConnection();
		if(conn != null) {
			conn.commit();
			conn.close();
			tl.remove();
		}
	}
	
	
	
	

}

猜你喜欢

转载自blog.csdn.net/alexzt/article/details/80973689
今日推荐