springmvc文章分页源代码

2019-03-16 11:09|来源: 领悟书生

由于网友的需求,我把本站【领悟书生教程网】的文章分页代码分享给各位,代码比较简单,在使用的过程中如果有什么不明白可以找我,同时希望各位提出诚恳的意见


分页效果



首先来看看分页工具类PageUtil.java:

package com.naxsu.utils;
 
/**
 * 分页工具包
 * @author huangyineng 
 * 2012-10-28 下午11:24:25 http://www.656463.com
 */
public class PageUtil {
    private int pageRecord;// 每页面的记录数
    private int totalRecord;// 总记录数
    private int totalPage;// 总页数
    private int currentPage = 1;// 当前页
 
    private int prePage;// 上一页
    private int nextPage;// 下一页
 
    private int pageNumStart;// 页码显示开始listbegin;
    private int pageNumEnd;// 页码显示结束listend;
    private int showPageNum = 10;// 显示页码个数,默认是10
     
    public PageUtil() {
         
    }
     
    public PageUtil(int pageRecord, int totalRecord, int currentPage) {
        this.pageRecord = pageRecord;
//      this.totalRecord = totalRecord;
        this.currentPage = currentPage;
        setTotalRecord(totalRecord);
        setPageNumEnd(pageNumEnd);
        setPageNumStart(pageNumStart);
    }
 
    public int getPageRecord() {
        return pageRecord;
    }
 
    public void setPageRecord(int pageRecord) {
        this.pageRecord = pageRecord;
    }
 
    public int getTotalRecord() {
        return totalRecord;
    }
 
    public void setTotalRecord(int totalRecord) {
        this.totalRecord = totalRecord;
        setTotalPage(this.totalRecord % this.pageRecord == 0 ? this.totalRecord
                / this.pageRecord : this.totalRecord / this.pageRecord + 1);
    }
 
    public int getTotalPage() {
        return totalPage;
    }
 
    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }
 
    // 获取当前页
    public int getCurrentPage() {
        return currentPage;
    }
 
    // 设置当前页面
    public void setCurrentPage(int currentPage) {
        // 如果当前页数大于总页数,即当前页等于总页面数
        if (currentPage > getTotalPage()) {
            this.currentPage = getTotalPage();
        } else {
            if (currentPage < 1) {
                this.currentPage = 1;// 如果当前页小于1,默认是1
            } else {
                this.currentPage = currentPage;
            }
        }
    }
 
    // 获取下一页
    public int getNextPage() {
        return this.getCurrentPage() + 1;
    }
 
    // 获取上一页
    public int getPrePage() {
        return this.getCurrentPage() - 1;
    }
 
    public int getPageNumStart() {
        return pageNumStart;
    }
 
    public void setPageNumStart(int pageNumStart) {
        // 显示页数的一半
        int halfPage = (int) Math.ceil((double) showPageNum / 2);
        if (halfPage >= currentPage) {
            this.pageNumStart = 1;
        } else {
            if (currentPage + halfPage > totalPage) {
                this.pageNumStart = (totalPage - showPageNum + 1)<=0?1:
                                    (totalPage - showPageNum + 1);
            } else {
                this.pageNumStart = currentPage - halfPage + 1;
            }
        }
    }
 
    public int getPageNumEnd() {
        return pageNumEnd;
    }
 
    public void setPageNumEnd(int pageNumEnd) {
        // 显示页数的一半
        int halfPage = (int) Math.ceil((double) showPageNum / 2);
        if (halfPage >= currentPage) {
            //this.pageNumEnd = showPageNum;
            this.pageNumEnd = showPageNum>totalPage?totalPage:showPageNum;
        } else {
            if (currentPage + halfPage >= totalPage) {
                this.pageNumEnd = totalPage;
            } else {
                this.pageNumEnd = currentPage + halfPage;
            }
        }
    }
 
    public int getShowPageNum() {
        return showPageNum;
    }
 
    public void setShowPageNum(int showPageNum) {
        this.showPageNum = showPageNum;
    }
 
    // 传给mybatis,limit start,size
    private int start;
    private int size;
 
    public int getStart() {
        return (this.currentPage - 1) * pageRecord ;//+ 1;
    }
 
    public int getSize() {
        return this.currentPage * pageRecord;
    }
 
}

控制层:

@RequestMapping(value = "/tutorials", method = RequestMethod.GET)
public String getTutorialsByChannelType(String page, Model model) {
 
    int totalArticles = this.getArticleservice().getArticleCount();// 总数
 
    int currentPage=1;
    if (page != null) {
        currentPage=Integer.parseInt(page);
    }
     
    PageUtil _page = new PageUtil(10,totalArticles,currentPage);
    _page.setTotalRecord(totalArticles);
    _page.setPageNumStart(_page.getPageNumStart());
    _page.setPageNumEnd(_page.getPageNumEnd());
    _page.setCurrentPage(currentPage);
     
    Map map = new HashMap();
    map.put("start",_page.getStart());
    map.put("size", _page.getPageRecord());
    model.addAttribute("articles", this.getArticleservice().getPageArticles(map));
    model.addAttribute("showPage", _page);
 
    return "article/getTutorials";
}

页面显示:

<div class="pg">
    <c:if test="${showPage.totalPage>0}">
        <c:if test="${showPage.currentPage!=1}">
            <a href="/tutorials?page=${showPage.prePage }" class="prev">&nbsp;&nbsp;</a>
        </c:if>
        <c:forEach begin="${showPage.pageNumStart }" end="${showPage.pageNumEnd }" var="pageIndex">
            <c:if test="${showPage.currentPage==pageIndex }">
                <strong>${pageIndex }</strong>
            </c:if>
            <c:if test="${showPage.currentPage!=pageIndex }">
                <a href="/tutorials?page=${pageIndex }">${pageIndex }</a>
            </c:if>
        </c:forEach>
        <c:if test="${showPage.currentPage!=showPage.totalPage}">
            <a href="/tutorials?page=${showPage.nextPage }" class="nxt">下一页</a>
        </c:if>
    </c:if>
</div>

相关代码贴完了,希望对你有用


本文链接:springmvc文章分页源代码,由领悟书生原创

转载请注明出处【http://www.656463.com/article/343】

相关问答

更多
  • java源代码是用来关联jar中的编译代码的。 应用场景:通常在打开eclipse中的jar的时候,发现class文件不能被打开,此时出现下面的界面,此时就需要通过“Attach Source”,之后找到对应的java源代码来实现代码关联,即可正常的显示class类中的内容。 备注:如果此处ava源代码指的是源代码文件(“.java”),是用来进行代码维护和二次开发的必备东西。
  • 首先要了解设计模式,这个是阅读大师源码的一个桥梁,很多代码看上去很多,其实就是一种模式。了解了模式后,整个关系图就清楚了。 还有善于分片阅读,找简单的读,可以先读spring jdbc,这部分的模板跟回调看起来会简单点。 然后再看IOC,看IOC之前必须对spring IOC原理掌握的很透彻,怎么扩展,怎么写胶水代码整合其他框架。建议熟读Spring-Reference。 然后再读源码,先学习怎么写出漂亮的代码,再学习怎么设计出漂亮的模式。
  • 刚刚好我前段时间做了一个基于SpringMVC + Mybatis + Redis + Freemarker(JSP)的权限控制Demo。 地址看下面代码: /** * 百度不让输入网址 * 地址为 */ String url = "
  • 首先要了解设计模式,这个是阅读大师源码的一个桥梁,很多代码看上去很多,其实就是一种模式。了解了模式后,整个关系图就清楚了。 还有善于分片阅读,找简单的读,可以先读spring jdbc,这部分的模板跟回调看起来会简单点。 然后再看IOC,看IOC之前必须对spring IOC原理掌握的很透彻,怎么扩展,怎么写胶水代码整合其他框架。建议熟读Spring-Reference。 然后再读源码,先学习怎么写出漂亮的代码,再学习怎么设计出漂亮的模式。
  • spring的源代码下载地址: http://www.springframework.org/ spring的源代码查看请去这个网站搜索: http://www.open-open.com/index.htm
  • 给你个思路,你去实现HandlerIntercepter这个接口去实现判断当前用户是否登录 如果当前用户没有登录,则将提示用户登录,反之。
  • 多实践,多操作。网上有许多软件。许多程序编写的教程。你可以先跟着做,做熟悉了可以自己创意。加上一些教程里没有的东西。举一反三。
  • 代码删除掉。
  • 您使用的短代码是托管的WordPress.com博客。 你的是一个自我托管的WordPress网站,所以你需要下载一个插件才能让它运行起来。 插件的详细信息在您链接到的页面底部的“Credits”(下面复制)下提供。 “ Alex Gorbatchev的SyntaxHighlighter项目用于实现此功能。如果您是WordPress.org用户并希望在自托管博客上使用此功能,则可以将其作为插件使用 。” The shortcode that you have used is for hosted WordP ...
  • StackOverflow使用Google Prettify 对于最基本的用法,只需使用PrettyPrint类装饰您的pre标签即可。 StackOverflow uses Google Prettify For the most basic usage, just decorate your pre tags with the PrettyPrint class.