欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

javaWeb项目——简单的分页功能

程序员文章站 2024-02-20 15:36:52
...
实体类:Page.java
        定义变量:pageSize(每页显示的数量)、pageCurrent(当前页)、PageCount(总页数)、count(数据总条数)、list(数据列表);
        setPageCount()方法,设置页数;
        Page(List<T> list,int pageCurrent,int count) 构造方法;
 
package entity;

import java.util.List;

public class Page<T> {
	private int pageSize=2;
	private int pageCurrent=1;
	private int pageCount;
	private int count;
	private List<T> list;
	
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getPageCurrent() {
		return pageCurrent;
	}
	public void setPageCurrent(int pageCurrent) {
		this.pageCurrent = pageCurrent;
	}
	public int getPageCount() {
		return pageCount;
	}
	public void setPageCount(int pageCount) {
		this.pageCount = count%pageSize==0? (count/pageSize):(count/pageSize+1);
	}
	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
	}
	public List<T> getList() {
		return list;
	}
	public void setList(List<T> list) {
		this.list = list;
	}
	
	public Page(List<T> list,int pageCurrent,int count){
		setList(list);
		setPageCurrent(pageCurrent);
		setPageSize(this.pageSize);
		setCount(count);
		setPageCount(this.pageCount);
	}

}

 

实现类:StudentServiceImpl.java——page方法
        定义的sql为子查询语句;
        调用Dao层方法;
    @Override
	public Page<Student> page(int pageCurrent) {
		// TODO Auto-generated method stub
		String sql="(select * from student order by code asc)";
		Page<Student> page=studentDao.page(sql,pageCurrent);
		return page;
	}

 

Dao类:StudentDao.java——page方法
 
        listSql查询数据列表,countSql查询数据条数;
        调用page构造方法,返回page对象;
public Page<Student> page(String sql,int pageCurrent){
		
		String listSql="select * from (select A.*,ROWNUM RN from"+sql+"A where ROWNUM<=?) where RN>?";
		String countSql="select count(1) from "+sql;
		List<Student> list =new ArrayList<Student>();
		Long count=0L;
		try{
			PreparedStatement ps=getConn().prepareStatement(listSql);
			ps.setInt(1, (pageCurrent-1)*2+2);
			ps.setInt(2, (pageCurrent-1)*2);
			ResultSet rs=ps.executeQuery();
			while(rs.next()){
				Student student=new Student();
				student.setStudentId(rs.getString("student_id"));
				student.setCode(rs.getString("code"));
				student.setStudentName(rs.getString("student_name"));
				list.add(student);
			}
			ps.close();
			closeConn();
			
			ps=getConn().prepareStatement(countSql);
			rs=ps.executeQuery();
			while(rs.next()){
				count=rs.getLong(1);
			}
			ps.close();
			closeConn();
		}catch(Exception e){
			e.printStackTrace();
		}
		Page<Student> page = new Page<Student>(list, pageCurrent, count.intValue());
		return page;
	}

 

action类:pageServlet.java
        调用page方法,获取page对象;
        拼接html,返回页面;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		StudentService studentService=new StudentServiceImpl();
		int pageCurrent = Integer.valueOf(request.getParameter("pageCurrent")!=null?request.getParameter("pageCurrent"):"1");
		Page<Student> page = studentService.page(pageCurrent);
		List<Student> studentList=page.getList();
		
		StringBuffer html=new StringBuffer();
		html.append("<table align='center' border='1' cellspacing='0'><tr><td>学号</td><td>姓名</td></tr>");
		for(int i=0;i<studentList.size();i++){
			html.append("<tr><td>"+studentList.get(i).getCode()+"</td><td>"+studentList.get(i).getStudentName()+"</td></tr>");
		}
		html.append("</table><div align='center'>");
		if(pageCurrent>1){
			html.append("<a href='/student03/page?pageCurrent=1'>首页</a> ");
			html.append("<a href='/student03/page?pageCurrent="+(pageCurrent-1)+"'>上一页</a>  ");
		}
		for(int i=0;i<page.getPageCount();i++){
			html.append("<a href='/student03/page?pageCurrent="+(i+1)+"'>"+(i+1)+"</a>  ");
		}
		if(pageCurrent<page.getPageCount()){
			html.append("<a href='/student03/page?pageCurrent="+(pageCurrent+1)+"'>下一页</a>");
			html.append(" <a href='/student03/page?pageCurrent="+ page.getPageCount() +"'>末页</a> ");
		}
		html.append("第 【"+page.getPageCurrent()+"】页</div>");
		response.setContentType("text/html; charset=UTF-8");
		response.getWriter().write(html.toString());
	}

 

javaWeb项目——简单的分页功能

 
相关标签: javaWeb page

上一篇: Swift 中的类和结构体

下一篇: