Hibernate+Freemarker分页控件实现

传到前端的Page

public class Page<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    private List<T> contents;
    private int pageSize;
    private int totalSize;
    private int curPage;
    public Page(int curPage, int totalSize, int pageSize, List<T> contents) {
        this.curPage = curPage;
        this.totalSize = totalSize;
        this.pageSize = pageSize;
        this.contents = contents;
    }
    public List<T> getContents() {
        return contents;
    }
    public void setContents(List<T> 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中。

@Controller
public 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<String, Object> 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<Content> 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();
        }
    }
}

前端实现

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>分页</title> </head> <body> <@cms_content_page> <ul><#list page.contents as content> <li>${content.title}</li> </#list> </ul> 总共${page.totalSize}条记录 ${page.curPage}/${page.totalPages}页 <#assign href="/javaweb/example/page_"> <a href="${href + "1" + ".html"}">首页</a> <#if (page.curPage > 1) > <a href="${href + (page.curPage - 1) + ".html"}">上一页</a> <#else> <a href="${href + page.curPage + ".html"}">上一页</a> </#if> <#if page.curPage < page.totalPages> <a href="${href + (page.curPage + 1) + ".html"}">下一页</a> <#else> <a href="${href + page.curPage + ".html"}">下一页</a> </#if> <a href="${href + page.totalPages + ".html"}">尾页</a> </@cms_content_page> </body> </html>

Hibernate关键实现

public class PageableBaseDao<T> extends AbstractBaseDao<T> implements Pageable<T> {
    @Override
    public Page<T> 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<T>(curPage, totalSize, pageSize, contents);
    }
} 

截图

访问http://localhost:8088/javaweb/example/page_3.html

Hibernate+Freemarker分页控件实现_第1张图片

你可能感兴趣的:(freemarker,Hibernate,分页)