Hibernate + Freemarker paging control implementation

Source: Internet
Author: User

Hibernate + Freemarker paging control implementation
Upload to front-end 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; }} 
      
     
    
   
  
Backend implementation

Parse the URL to get the page data and fill it in the model data.

@ Controllerpublic class ExampleController {// For example, 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 custom commands
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();        }    }}
  
Front-end implementation
<@cms_content_page>
  • <#list page.contents as content>
    • ${content.title}
    Total $ {page. totalSize} record $ {page. curPage}/$ {page. totalPages page <# assign href =/javaweb/example/page _> homepage <# if (page. curPage> 1)> previous page <# else> Previous Page <# If page. curPage <page. totalPages> next page <# else> next page Last page
Key Hibernate implementations
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); }} 
      
     
    
   
  
 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.