ssm使用分页插件 pageHelper整理公共页

来源:互联网 发布:angularjs.js 下载 编辑:程序博客网 时间:2024/06/06 06:58
在一个项目中分页时必不可少的,但是多个功能都需要使用到分页,每次都需要写一遍分页的按钮组件、分页的一些相关属性很麻烦。这里将分页的一些按钮组件放在了一个公共的页面中,在使用时直接导入这个公共的页面,下面说一下代码的实现。关于pageHepler的相关配置,网上说的很详细,这里不做过多的解释

导入的是这三个jar包

<!-- mybatis分页 -->        <dependency>            <groupId>com.github.miemiedev</groupId>            <artifactId>mybatis-paginator</artifactId>            <version>1.2.5</version>        </dependency>        <dependency>            <groupId>com.github.pagehelper</groupId>            <artifactId>pagehelper</artifactId>            <version>5.1.2</version>        </dependency>        <dependency>            <groupId>com.github.jsqlparser</groupId>            <artifactId>jsqlparser</artifactId>            <version>0.9.6</version>        </dependency>
首先是在想使用分页的页面导入公共页:
<!-- 分页 -->//需要导入公共的分页页面                            <jsp:include page="../common/include_page.jsp">//在这里需要设置一些参数,value即要访问的路径,name为给这个值起一个名字    <jsp:param value="/shop-admin/classify/classifyAll" name="pageTitle"/>                            </jsp:include>

公共页稍后附上,先看在controller中的方法,即上面value要访问的路径

@Controller@RequestMapping("/classify")public class ClassifyController {    @Autowired    private ClassifyService classifyService;    @Autowired    private ClassifySubService classifySubService;    //整理的公共类对象    @Autowired    private PageUtil pageUtil;    /**     *      * @param productClassify   操作的实体类     * @param index     分页要跳转的页码,或者是当前页码     * @param pageSize      每页显示的信息数目     * @return     */    @RequestMapping("/classifyAll")    public String classifyAll(ProductClassify productClassify,ModelMap modelMap,@RequestParam(required=true,defaultValue="1") Integer index,            @RequestParam(required=false,defaultValue="10") Integer pageSize,HttpServletRequest request){        //在执行查询的前一行加上这条代码即可执行分页的查询        PageHelper.startPage(index, pageSize);        //查询到的实体类集合        Page<ProductClassify> classifyList=(Page<ProductClassify>)classifyService.selectAllClassify(productClassify);        //这里调用了自己封装的公共方法        pageUtil.setPageInfo(classifyList, index, pageSize,request);        //将查询到的结果集传回页码        modelMap.put("classifyList", classifyList);        return "/classify/list-classify";    }

下面附上自己封装的公共类,使用时每次调用其中的方法即可

/** * 分页工具类 * @author Administrator *  Integer start   起始项的索引 *  Integer end     总页数 *  Integer total   结尾项的索引 *  Integer totalCount      当前集合的总条数 *  Integer index   当前页码数或者说要跳转的页面数 */@Componentpublic class PageUtil {    Integer start;    Integer end;    Integer total;    Integer totalCount;    Integer index;    public void setPageInfo(Page<?> list,Integer index,Integer pageSize,HttpServletRequest request){        //如果要跳转的页码数小于零则跳转首页        if(index<=0){            index=1;        }        //获取末页数        end=(int) list.getPages();        //如果要跳转的页数大于末页,则跳转末页        if(index>=end){            index=end;            total=(int) list.getTotal();        }else{            total=index*pageSize;        }        //该页起始项的索引        start=(index-1)*pageSize+1;        //获取总的信息数目        totalCount=(int) list.getTotal();        this.index=index;        request.setAttribute("index", index);        request.setAttribute("end", end);        request.setAttribute("start", start);        request.setAttribute("total", total);        request.setAttribute("totalCount", totalCount);    }}

最后附上公共的分页按钮组件页面

<div class="row">    <div class="col-sm-6">        <div class="dataTables_info">显示            ${start } 到 ${total }            项,共 ${totalCount } 项</div>    </div>    <div class="col-sm-6">        <div class="dataTables_paginate paging_simple_numbers">            <ul class="pagination">                <li class="paginate_button previous"><a href="<%=request.getParameter("pageTitle")%>?index=1">首页</a>                <li class="paginate_button previous"><a href="<%=request.getParameter("pageTitle")%>?index=${index-1}">上一页</a>                </li>                    <li class="paginate_button">                        <a href="<%=request.getParameter("pageTitle")%>?index=2">2</a>                    </li>                <li class="paginate_button next"><a href="<%=request.getParameter("pageTitle")%>?index=${index+1}">下一页</a></li>                <li class="paginate_button previous"><a href="<%=request.getParameter("pageTitle")%>?index=${end}">末页</a>            </ul>        </div>    </div></div>

前端的样式框架不用管,把class属性删掉即可
最后的结果
这里写图片描述
会有些小问题像已经跳转到末页之后再点击下一页会没有内容,如果再需要其他的功能可以自己在公共类中再添加

原创粉丝点击