java实现前台分页和后台分页

来源:互联网 发布:u盘装mac系统 编辑:程序博客网 时间:2024/05/22 06:59
后台分页:1.创建page类
public class Page<T> {    private int pageSize;//每页显示的条数,在Servlet    private int pageNum;//当前页码,该数据在Servlet中获取    private int totalPage;//总页数,需要通过计算获得    private int index;//分页开始的索引,通过计算获得    private int totalRecord;//总记录,通过查询数据库获得    private List<T> listDate;//分页的数据,通过数据库查询    private String path;    public Page(int totalRecord, int pageNumber, int pageSize) {        super();        this.totalRecord = totalRecord;        this.pageNum = pageNumber;        this.pageSize = pageSize;    }    public String getPath() {        return path;    }    public void setPath(String path) {        this.path = path;    }    public int getPageSize() {        return pageSize;    }    public void setPageSize(int pageSize) {        this.pageSize = pageSize;    }    public int getPageNum() {        if(pageNum < 1){            return 1;        }else if(pageNum > getTotalPage()){            return getTotalPage();        }        return pageNum;    }    public void setPageNum(int pageNum) {        this.pageNum = pageNum;    }    public int getTotalPage() {        if(getTotalRecord()%getPageSize()==0){//判断总页数的计算方式            return getTotalRecord()/getPageSize();        }else{            return getTotalRecord()/getPageSize()+1;        }    }    public void setTotalPage(int totalPage) {        this.totalPage = totalPage;    }    public int getIndex() {        return (getPageNum() - 1) * getPageSize();//获取索引的计算方式    }    public void setIndex(int index) {        this.index = index;    }    public int getTotalRecord() {        return totalRecord;    }    public void setTotalRecord(int totalRecord) {        this.totalRecord = totalRecord;    }    public List<T> getListDate() {        return listDate;    }    public void setListDate(List<T> listDate) {        this.listDate = listDate;    }}
2.在servlet中设置pageNum和pageSize的初始值
protected static int pageNum=1;protected static int pageSize = 3;  
3.获取从页面返回的pageNum:
pageNum=Integer.parseInt(request.getParameter("pageNu"));
4.从数据库中查找出totalRecord和listDate(1)Servlet中的调用方法:
Page<student> page=sService.getStudentList(pageNum, pageSize);
(2)service层的实现getStudentList()方法:
 public Page<student> getStudentList(int pageNum, int pageSize){            int totalRecord=  jdbcUtil.executeQuere("SELECT COUNT(*) AS total FROM stu");                                   Page<student> page = new Page<student>(totalRecord,pageNum,pageSize);                   List<student> list = jdbcUtil.executeSelect("select * from stu limit ?,?",new RowMap<student>(){                 @Override                 public student rowMapping(ResultSet rs){                     student student=new student();                     try {                        student.setId(rs.getInt("id"));                                               student.setName(rs.getString("name"));                                                      student.setSchool(rs.getString("school"));                                               student.setScore(rs.getDouble("score"));                    } catch (SQLException e) {                        // TODO Auto-generated catch block                        e.printStackTrace();                    }                     return student;                 }             },page.getIndex(),page.getPageSize());            page.setListDate(list);            return page;        }
(3)jdbcUtil中的executeQuere()和executeSelect()方法:
public static int executeQuere(String sql){        int result=0;        Connection connection=getConnection();        PreparedStatement pstmt;        try {            pstmt = connection.prepareStatement(sql);            ResultSet rs=pstmt.executeQuery();            if(rs.next()){                result =  rs.getInt("total");            }        } catch (SQLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }finally {            close(connection);        }        return result;    }
public static <T>List<T> executeSelect(String sql,RowMap<T> rowMap,Object...params){        List<T> result=new ArrayList<>();        Connection connection=getConnection();        try {            PreparedStatement pstmt=connection.prepareStatement(sql);            if(params!=null){                for(int i=0;i<params.length;i++){                    pstmt.setObject(i+1, params[i]);                }            }            ResultSet rs=pstmt.executeQuery();            while(rs.next()){                //将数据行 映射到对象中                T t=rowMap.rowMapping(rs);                result.add(t);            }        } catch (SQLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }finally {            close(connection);        }        return result;    }
(4)建立RowMap接口:
public interface RowMap<T> {    public T rowMapping(ResultSet rs);}
5.设置page中的路径(跳转页面时的转发路径):如果只使用一个servlet就可以:
String url = WebUtil.getPath(request);page.setPath(url);
WebUtil中getPath()方法为:
public static String getPath(HttpServletRequest request){        String requestURI = request.getRequestURI();        String queryString = request.getQueryString();        String url = requestURI+"?"+queryString;        if(url.contains("&pageNum")){            url = url.substring(0, url.indexOf("&pageNum"));        }        return url;    }
如果增删改查分别是不同的servlet,则直接把page的path属性设为定值就好。前台分页:1.新建一个jsp文件(文件名为:paging.jsp)放在WEB-INF/page文件夹下2.在需要进行分页的页面中调用新建的jsp文件
<%@ include file="/WEB-INF/page/paging.jsp" %>

3.在paging.jsp文件中添加:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><div id="page_nav" align="center">    <a href="${page.path}pageNu=1">首页</a>    <a href="${page.path}pageNu=${page.pageNum -1 }">上一页</a>        <c:choose>            <c:when test="${page.totalPage <= 5 }" >                <c:set var="begin" value="1"></c:set>                <c:set var="end" value="${page.totalPage}"></c:set>            </c:when>            <c:when test="${page.pageNum <= 3 }">                <c:set var="begin" value="1"></c:set>                <c:set var="end" value="5"></c:set>            </c:when>            <c:otherwise>                <c:set var="begin" value="${page.pageNum-2}"></c:set>                <c:set var="end" value="${page.pageNum+2}"></c:set>                <c:if test="${end > page.totalPage }">                    <c:set var="begin" value="${page.totalPage-4}"></c:set>                    <c:set var="end" value="${page.totalPage}"></c:set>                </c:if>            </c:otherwise>        </c:choose>        <c:forEach begin="${begin }" end="${end}" var="num">            <c:if test="${page.pageNum==num}">                【${num}】            </c:if>            <c:if test="${page.pageNum!=num}">                <a href="${page.path}pageNu=${num}">${num }</a>            </c:if>        </c:forEach>    <a href="${page.path}pageNu=${page.pageNum +1}">下一页</a>    <a href="${page.path}pageNu=${page.totalPage}">末页</a>    共${page.totalPage }页,${page.totalRecord }条记录到,去第<input value="${page.totalPage }" name = "pn" id ="pn_input"/><input type="button" value="确定" id="btn_id"/>    <script type="text/javascript">        $("#btn_id").click(function(){            var value= $("#pn_input").val();            window.location="${page.path}pageNu="+value;        });    </script></div>

注意:这里需要调用jquery包,实现jquery方法

原创粉丝点击