Hibernate+Freemarker分頁控制項實現

來源:互聯網
上載者:User

Hibernate+Freemarker分頁控制項實現
傳到前端的Page

public class Page implements Serializable {    private static final long serialVersionUID = 1L;    private List contents;    private int pageSize;    private int totalSize;    private int curPage;    public Page(int curPage, int totalSize, int pageSize, List contents) {        this.curPage = curPage;        this.totalSize = totalSize;        this.pageSize = pageSize;        this.contents = contents;    }    public List getContents() {        return contents;    }    public void setContents(List contents) {        this.contents = contents;    }    public int getTotalPages() {        return totalSize / pageSize + 1;    }    public int getPageSize() {        return pageSize;    }    public void setPageSize(int pageSize) {        this.pageSize = pageSize;    }    public int getCurPage() {        return curPage;    }    public void setCurPage(int curPage) {        this.curPage = curPage;    }    public int getTotalSize() {        return totalSize;    }    public void setTotalSize(int totalSize) {        this.totalSize = totalSize;    }} 
後端實現

解析URL,得到第幾頁的資料,填充到model data中。

@Controllerpublic class ExampleController {    //例如http://localhost:8088/javaweb/example/page_3.html,    @RequestMapping(value = { /example/page{pageNo}.html, /example/page_{pageNo}.html })    public String page(@PathVariable(pageNo) int pageNo, Map map) {        if (pageNo <= 0)            pageNo = 1;        FrontUtil.frontPageData(map, pageNo, /javaweb/example/page_);        return example/page;    }} 
Freemarker自訂指令
public class ContentPageDirective implements TemplateDirectiveModel {    // private Version version = new Version(2.3.23);    private DefaultObjectWrapperBuilder defaultObjectWrapperBuilder = new DefaultObjectWrapperBuilder(            new Version(2.3.23));    @Inject    private ContentDao contentDao;    @Override    public void execute(Environment env, @SuppressWarnings(rawtypes) Map params, TemplateModel[] loopVars,            TemplateDirectiveBody body) throws TemplateException, IOException {        // TODO Auto-generated method stub        try {            int pageNo = FrontUtil.getPageNo(env);            Page page = contentDao.getPage(pageNo);            env.setVariable(page, defaultObjectWrapperBuilder.build().wrap(page));            if (body != null) {                body.render(env.getOut());            }        } catch (ClientException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}
前端實現
<@cms_content_page>
  • <#list page.contents as content>
    • ${content.title}
    總共${page.totalSize}條記錄 ${page.curPage}/${page.totalPages}頁 <#assign href=/javaweb/example/page_> 首頁 <#if (page.curPage > 1) > 上一頁 <#else> 上一頁 <#if page.curPage < page.totalPages> 下一頁 <#else> 下一頁 尾頁
Hibernate關鍵實現
public class PageableBaseDao extends AbstractBaseDao implements Pageable {    @Override    public Page getPage(int curPage) throws ClientException {        // TODO Auto-generated method stub        if (curPage < 1) {            throw new ClientException(page number should large than 0);        }        int totalSize = getCount();        int pageSize = PageConstant.SIZE_PER_PAGE;        Criteria criteria = getSession().createCriteria(entityClass);        criteria.setFirstResult((curPage - 1) * pageSize);        criteria.setMaxResults(pageSize);        List contents = criteria.list();        return new Page(curPage, totalSize, pageSize, contents);    }} 
 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.