Hibernate+Freemarker分页控件实现

页面导航:首页 > 软件编程 > Java编程 > Hibernate+Freemarker分页控件实现

Hibernate+Freemarker分页控件实现

来源: 作者: 时间:2016-01-21 09:39 【

传到前端的Pagepublic class Page implements Serializable { private static final long serialVersionUID = 1L; private List contents; private int pageSize;

传到前端的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中。

@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 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);
    }
} 

 

 

Tags:

文章评论

最 近 更 新
热 点 排 行
Js与CSS工具
代码转换工具

<