开发教程您现在的位置:主页 > 开发教程 >

Jsp中分页的制作

发布日期:2018-01-12 10:08

Java中在前端方面现在用jsp,然后就有分页的制作,西安Java培训则总结出了分页制作的相关知识。
分页技能还区别两个:假分页和真分页
  假分页:一次性从数据库读出表的一切数据一次性的回来给客户端,由js来操控每一页的显现。
  真分页:由程序操控,每一次只回来一页大小的数据,显现到客户端。
  由此能够很清楚的分辨出真假分页各自的优缺点:
  假分页:由于一次性读出一切数据并回来给客户端,如果数据量巨大,所以这一次的动作可能是十分耗费服务器资源和带宽的,
可是回来给客户端今后就十分轻松了,客户在一段时间内不会再像服务器端恳求资源。但不代表可能呈现一些意外状况,
比如说客户将浏览器封闭,从头访问网站等。
  真分页:假分页每次只取需求的数据回来给客户端,比起真分页没有那么大的数据库压力。但也由于这个工作特性,所以假分页
的办法需求频频和服务器端进行交互。已然频频交互,天然也会给服务器带来担负。
  综上:如果数据量较小,运用假分页的作用会更优,如果数据量巨大,运用真分页的作用更优。
  在制造分页的时候你需求了解,分页的数据,分页的一些特点有哪些
分页的数据:这些数据能够从数据库中提取出来的,也能够网上查找得到的
分页的特点:
数据总数
页面的数据多少条
总的页面数
最终一页
第一页
当前页
  我们能够将这些特点变成一个实体类中的特点来调用,会感觉更加的明晰
  先创立一个实体类:
public class Paging {
    private int page;//当前页
    private int pagesize;//页面数据条数
    private int indexpage=1;//首页
    private int endpage;//尾页
    private int count;//总数据条数
    private int pagenumber;//总页面数
    private List<Object> list;//得到的数据放入list集合中
    public int getPage() {
        return page;
    }
    public void setPage(int page) {
        this.page = page;
    }
    public int getPagesize() {
        return pagesize;
    }
    public void setPagesize(int pagesize) {
        this.pagesize = pagesize;
    }
    public int getIndexpage() {
        return indexpage;
    }
    public void setIndexpage(int indexpage) {
        this.indexpage = indexpage;
    }
    public int getEndpage() {
        return endpage;
    }
    public void setEndpage() {
        this.endpage=pagenumber;
    }
    public int getCount() {
        this.count=list.size();
        return count;
    }
    public void setCount() {
        this.count=list.size();
    }
    public int getPagenumber() {
        return pagenumber;
    }
    public void setPagenumber() {
        this.pagenumber=(count%pagesize==0)?count/pagesize:count/pagesize+1;
    }
    public List<Object> getList() {
        return list;
    }
    public void setList(List<Object> list) {
        this.list = list;
    }
}
需要创建一个servlet
public class Staff_ListServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;       
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Stuff stuff=new Stuff();
        stuff.setStaff("1");
        List<Object> list=    DAO.query(stuff);
        //页面当前页
        int page=0;
        //得到传过来的当前页
        String str_page=    request.getParameter("page");
        /**
         * 创建分页的关于一些内容的工具bean
         *
         * */
        Paging paging=new Paging();
        paging.setList(list);//从数据库得到数据存入的list集合
        paging.setCount();//数据总数
        paging.setPagesize(5);//一个页面的数据多少条
        paging.setPagenumber();//总的页面数
        paging.setEndpage();//最后一页
        paging.setIndexpage(1);//第一页
        if (str_page!=null) {
            //将页转换整型判断其大小
            int pag=Integer.parseInt(str_page);
            //当大于零,将传过来的pag值赋给当前页page
            if (pag>=0) {
                page=pag;
                //如果小于最大值时则,将其传过来的值减1在赋值给当前页,让其一直在最后一页
                if (pag>(paging.getPagenumber()-1)) {
                    page=pag-1;
                }
            }
        }
        paging.setPage(page);//最终确认当前页
        List<Object> list_page =new ArrayList<>();
        //将当前页的值传给新的list_page集合中,list集合是全部数据综合,用i调用其中的几条数据给list_page
        for (int i = paging.getPage()*paging.getPagesize(); i <(paging.getPage()+1)*paging.getPagesize()&&i<list.size(); i++) {
            list_page.add(list.get(i));
        }
        //将paging对象其设置在作用域中,以便后面页面调用
        request.setAttribute("paging", paging);
        request.setAttribute("list", list_page);
        request.getRequestDispatcher("staff_list.jsp").forward(request, response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub        doGet(request, response);
    }
 

在页面中用EL表达式调用这些request请求设置的可让数据在页面显示,这是个jsp页面,EL表达式只能在jsp页面使用
  关键点在下面的可以问号传参,返回servlet中将当前操作传过去,这个可以结合js中的ajax,实现页面的刷新了
<div id="content_right">
                        <br />
                        <br />
 
                        <div class="btwz">
                            人员列表
                        </div>
                        <br>
                        <form name="form1" action="Staff_InfoServlet" method="post">
                            <table cellspacing="1" class="table">
                                <tr class="table_header">
                                    <td valign="middle" align="left">
                                        姓名
                                    </td>
                                    <td valign="middle" align="left">
                                        性别
                                    </td>
                                    <td valign="middle" align="left">
                                        入职时间
                                    </td>
                                    <td valign="middle" align="left">
                                        职位
                                    </td>
                                    <td valign="middle" align="left">
                                        详细信息
                                    </td>
                                    <td valign="middle" align="left">
                                    </td>
                                </tr>
                                <c:forEach items="${list}" var="list">
                                <tr class="row2" onMouseOver="this.className='row1'"
                                    onMouseOut="this.className='row2'">
                                    <td valign="middle" align="left">
                                        ${list.name}
                                    </td>
                                    <td valign="middle" align="left">
                                        ${list.sex}
                                    </td>
                                    <td valign="middle" align="left">
                                        ${list.hiredate}
                                    </td>
                                    <td valign="middle" align="left">
                                        ${list.position}
                                    </td>
                                    <td valign="middle" align="left">
                                        <input type="radio" name="choose"  value="${list.id}"/>
                                    </td>
                                    <td valign="middle" align="left">
                                        <input type="checkbox" name="empId" value="${list.id}">
                                    </td>
                                </tr>
                                </c:forEach>
                            </table>
                        </form>
                        <p align="right">
                            <input type="button" class="button" value="添加人员"
                                onclick="location='createStaff.jsp'" />
                            <input type="button" class="button" value="详细信息"
                                onClick="    document.forms[0].submit();" />
                            <input type="button" Class="button" value=" 删  除 "
                                onClick="go()" />
                        </p>
                        ${Nochoose}

                        <p class="paging">
                            <a href="Staff_ListServlet?page=${paging.indexpage-1}"><< 首页 </a>
                            <a href="Staff_ListServlet?page=${paging.page-1 }">    < 上一页 </a>
                            <strong>第${paging.page+1}页/共${paging.pagenumber}页</strong>
                            <a href="Staff_ListServlet?page=${paging.page+1}">下一页 ></a>
                            <a href="Staff_ListServlet?page=${paging.pagenumber-1}">末页 >></a>
                        </p>
                        <br />
                        <br />
                    </div>
                </div>
            </div>
就这样一个分页解决了,很有复用性,可能有点费事,给我们参阅一下
其实也能够不必建一个页面的属性实体类制造,我们能够测验一下