Jsp刷新分页模板,很全

 

1.用来实现上一页下一页,我直接写到查询页面上

        <%--page的分页--%>
        <style type="text/css">
            a {
                color: #555555;
                text-decoration: none;
                cursor: pointer;
            }
            .pages a.p_pre:hover {
                background-color: #eaeaea;
                color: #555555;
                border: 1px solid #cccccc;
            }
            .pages a {
                height: 36px;
                line-height: 36px;
                overflow: hidden;
                color: #666666;
                font-size: 16px;
                text-align: center;
                display: inline-block;
                padding: 0 12px;
                margin: 0 4px;
                border: 1px solid #cccccc;
                -webkit-border-radius: 2px;
                -moz-border-radius: 2px;
                border-radius: 2px;
            }

            .pages a:hover, .pages a.cur {
                color: #FFF;
                background-color: #DD4E62;
                border: 1px solid #DD4E62;
            }

        </style>
        <div class="pages">
            <c:if test="${pager.totalPage>=1}">
                <a href="${pageContext.request.contextPath}${pager.url}&pageIndex=1" class="p_pre">首页</a>
                <c:if test="${pager.pageIndex>1}">
                    <a href="${pageContext.request.contextPath}${pager.url}&pageIndex=${pager.pageIndex-1}" class="p_pre">上一页</a>
                </c:if>
                <c:forEach  var="temp" begin="${pager.pageIndex>3?pager.pageIndex-3:1}" end="${pager.totalPage-pager.pageIndex>3?pager.pageIndex+3:pager.totalPage}" step="1">
                    <c:if test="${pager.pageIndex==temp}">
                        <a href="${pageContext.request.contextPath}${pager.url}&pageindex=${temp}" class="cur">${temp}</a>
                    </c:if>
                    <c:if test="${pager.pageIndex!=temp}">
                        <a href="${pageContext.request.contextPath}${pager.url}&pageindex=${temp}">${temp}</a>
                    </c:if>
                </c:forEach>
                <c:if test="${pager.pageIndex<pager.totalPage}">
                    <a href="${pageContext.request.contextPath}${pager.url}&pageindex=${pager.pageIndex+1}" class="p_pre">下一页</a>
                </c:if>
                <a href="${pageContext.request.contextPath}${pager.url}&pageIndex=${pager.totalPage}" class="p_pre">尾页</a>
            </c:if>
            <c:if test="${pager.totalPage==0}">
                <a href="#" class="cur">暂无记录</a>
            </c:if>
        </div>

2.遍历的方式

 

<c:forEach items="${pager.lists}" var="item">
                    <li class="w-280 m-l-13 m-r-13 bg-c-ffffff m-t-8 m-b-23 fl">
                        <a href="${pageContext.request.contextPath}/selectAllSys_videoByCid/${item.cid}" class="dsblock w-260 hg-140 ovhidden ">
                            <img src="${pageContext.request.contextPath}/jsp/front/videopic/${item.cimage}" alt=""/>
                            <div class="course-label">
                                <c:if test="${item.ctype==1}">
                                    <label>精品视频</label>
                                </c:if>
                                <c:if test="${item.ctype!=1}">
                                    <label>最新视频</label>
                                </c:if>
                                <%--<label>${item.ctype}</label>--%>
                            </div>
                        </a>
                        <div class="w-255 p-l-10 p-r-15 fl">
                            <div class="home_calsstxt w-200 fl">
                                <h3 class="w-all fs-16 ftc-000000 line-h32  one_hidden  m-t-6 fl">${item.cname}</h3>
                                <span class="dsblock w-all fs-14 ftc-ff0000 line-h32 fl">¥${item.cprice}</span> </div>
                        </div>
                    </li>
                </c:forEach>

 

3.工具类

 

package cn.dawn.util;

import java.util.List;



public class PagerInfo<T> {
    
        private int totalPage;// 总页数
        private int totalSize;// 总记录数
        private int pageSize;//每页的记录数
        private int pageIndex;//页码
        private String url;//地址
        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        private List<T> lists;

        public int getTotalPage() {
            //如果总记录数除以每页显示的记录数能整除,那么总页数等于商,否则商+1
            int total=totalSize%pageSize==0?totalSize/pageSize:totalSize/pageSize+1;
            return total;
        }

        public void setTotalPage(int totalPage) {
            this.totalPage = totalPage;
        }

        public int getTotalSize() {
            return totalSize;
        }

        public void setTotalSize(int totalSize) {
            this.totalSize = totalSize;
        }

        public List<T> getLists() {
            return lists;
        }

        public void setLists(List<T> lists) {
            this.lists = lists;
        }

        public int getPageSize() {
            return pageSize;
        }

        public void setPageSize(int pageSize) {
            this.pageSize = pageSize;
        }

        public int getPageIndex() {
            return pageIndex;
        }

        public void setPageIndex(int pageIndex) {
            this.pageIndex = pageIndex;
        }
        
    
}

4.springmvc方法

 

    /*根据搜索框查视频列表*/
    @RequestMapping("/searchcourselisttocontroller")
    public  String searchcourselisttocontroller(Model model,Integer pageindex,String cname){
        if(cname==null){
            cname="";
        }
        try {
            PagerInfo<Sys_course> pager = iCourseService.searchcourselisttocontroller(pageindex,cname);
            pager.setUrl("/searchcourselisttocontroller?cname="+cname);

            model.addAttribute("pager",pager);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "searchcourselist";
    }

 

5.service层方法

 

    @Override
    public PagerInfo<Sys_course> selectAllTypeThreeSys_course(Integer pageindex) throws Exception {

        int index2=pageindex==null?1:pageindex;

        //分页的集合
        PagerInfo<Sys_course> page=new PagerInfo<Sys_course>();



        Map<String,Object> map=new HashMap<String,Object>();
        map.put("pageIndex",(index2-1)*8);
        map.put("pageSize",8);
        List<Sys_course> lists = iCourseDAO.selectAllTypeThreeSys_course(map);

        page.setLists(lists);
        page.setTotalSize(iCourseDAO.selectAllTypeThreeSys_courseCount(map));
        page.setPageSize(8);
        page.setPageIndex(index2);


        return page;
    }

 

dao层俩个方法

 

    <select id="selectAllTypeThreeSys_course" resultType="Sys_course">
        SELECT * FROM sys_course WHERE cparent IN(SELECT c.cid AS ciddd  FROM sys_course  AS c WHERE cparent IN(SELECT cid FROM sys_course WHERE cparent=0)) ORDER BY ccreatetime DESC LIMIT #{pageIndex},#{pageSize}
    </select>


    <select id="selectAllTypeThreeSys_courseCount" resultType="int">
        SELECT COUNT(1) FROM sys_course WHERE cparent IN(SELECT c.cid AS ciddd  FROM sys_course  AS c WHERE cparent IN(SELECT cid FROM sys_course WHERE cparent=0)) ORDER BY ccreatetime DESC
    </select>

 

hibernate中分页怎么弄

 

    @Test
    /*
    * 查询姓名中包含b的员工,并且按照薪水降序排序
    * */
    public void t12CriteriaPage(){
        int count=((Long)session.createCriteria(Emp.class)
            .add(Restrictions.ilike("name","b",MatchMode.ANYWHERE))
                .setProjection(Projections.count("name")).uniqueResult()).intValue();
        System.out.println(count);
        //设置当前页和页大小
        int pageIndex=2;
        int pageSize=2;
        //计算总页数
        int totalPage=(count%pageSize==0)?(count/pageSize):(count/pageSize+1);
        //根据薪水进行降序排序
        Criteria criteria = session.createCriteria(Emp.class)
                .add(Restrictions.ilike("name","b",MatchMode.ANYWHERE))
                .addOrder(Order.desc("sal"));
        //设置 起始页和页大小
        List<Emp> emps=criteria.setFirstResult((pageIndex-1)*pageSize)
                .setMaxResults(pageSize).list();
        /*遍历*/
        for (Emp emp:emps){
            System.out.println(emp);
        }

        /*
        Hibernate: select count(this_.name) as y0_ from Emp this_ where lower(this_.name) like ?
        3
        Hibernate: select this_.empId as empId1_1_0_, this_.name as name2_1_0_, this_.sal as sal3_1_0_, this_.job as job4_1_0_, this_.deptNo as deptNo5_1_0_ from Emp this_ where lower(this_.name) like ? order by this_.sal desc limit ?, ?
        Hibernate: select dept0_.deptId as deptId1_0_0_, dept0_.name as name2_0_0_, dept0_.location as location3_0_0_ from Dept dept0_ where dept0_.deptId=?
        Emp{empId=2, name='Ab', job='财务猿2', sal=5000.0, dept=Dept{deptId=1, name='xx部', location='1楼'}}
        * */
    }

 

更简单的hibernate分页

 

@Test
    /**
     * 分页
     *
     * 01.查询总记录数
     * 02.每页显示的数据
     * 03.总页数
     */
    public void t1Page(){
        String hql="select count(*) from Teacher";//会返回Long类型
        int counts=((Long)session.createQuery(hql).uniqueResult()).intValue();
        /*页大小*/
        int pageSize=2;
        /*总页数*/
        int totalPages=(counts%pageSize==0)?(counts/pageSize):(counts/pageSize+1);
        /*显示第二页内容*/
        int pageIndex=2;
        hql="from Teacher";
        Query query = session.createQuery(hql);;
        /*从哪一条开始查*/
        query.setFirstResult((pageIndex-1)*pageSize);
        /*设置页大小*/
        query.setMaxResults(pageSize);
        List<Teacher> teachers = query.list();
        for (Teacher t:teachers) {
            System.out.println(t);
        }

    }

 

posted @ 2018-06-27 23:58  晨曦Dawn  阅读(295)  评论(0编辑  收藏  举报