码迷,mamicode.com
首页 > 其他好文 > 详细

SSH简单分页Demo

时间:2015-09-15 00:01:46      阅读:470      评论:0      收藏:0      [点我收藏+]

标签:

Dao实现层代码:

 1     @Override
 2     public BasicInfo getBasicInfo(BasicInfo basciInfo) {
 3         // TODO Auto-generated method stub
 4         if (basciInfo.getId() == null)
 5             return null;
 6         return (BasicInfo) getHibernateTemplate().get(BasicInfo.class, basciInfo.getId());
 7     }
 8 
 9     @Override
10     public List<BasicInfo> findAll(String loginUserId, int firstResult, int maxResult, String type) throws Exception {
11         // TODO Auto-generated method stub
12         Query createQuery = getSession().createQuery("from BasicInfo where requester=? and state=?");
13         createQuery.setParameter(0, loginUserId);
14         createQuery.setParameter(1, type);
15         createQuery.setFirstResult(firstResult);//从第几个开始
16         createQuery.setMaxResults(maxResult);//最多记录数
17         return createQuery.list();
18     }

 

 

Action控制层:

 

 1 public class BaseAction extends ActionSupport implements Protected {
 2 
 3 
 4     public int firstResult = 1;// 起始行数
 5     public int maxResult = 10;// 每次查询10条
 6 
 7     public int total = 0;// 总条数
 8     public int currentPage = 1;// 当前页
 9     public int totalPage = 0;// 总页数
10 
11     public void paging() {
12         // 调用方法前设置总记录数
13         if (total < 0) {
14             return;
15         }
16         // 设置总页数
17         this.setTotalPage(total % maxResult > 0 ? total / maxResult + 1 : total / maxResult);
18         if (currentPage <= 1) {
19             currentPage = 1;
20         } else if (currentPage > this.getTotalPage()) {
21             currentPage = this.getTotalPage();
22         }
23         this.setFirstResult(currentPage - 1 > -1 ? (currentPage - 1) * maxResult : currentPage * maxResult);
24     }
25 
26 ... 此处省略N个字
 1 public class Todo extends BaseAction {
 2 
 3     public String execute() throws Exception {
 4         String loginUserId = getUserLogin().getUserLoginId();
 5         setTotal(basicInfoService.findToDo(loginUserId));
 6         paging();
 7         basicInfos = basicInfoService.pageFindToDo(loginUserId, firstResult, maxResult);
 8         return SUCCESS;
 9     }
10 
11 }

 

 

FreeMarker视图层:

 

 1             <#if totalPage gt 1>
 2             <div class="paging">
 3                 <a href="${base}/workspace/todo.action?currentPage=1">首页</a>
 4                 <a href="${base}/workspace/todo.action?currentPage=${currentPage?if_exists-1}">上一页</a>
 5                 <#if (totalPage?if_exists>0)>
 6                 <#list 1..totalPage as idx>
 7                 <a <#if (currentPage?if_exists==idx_index+1)>class="checked"</#if> href="${base}/workspace/todo.action?currentPage=${idx_index+1}">${idx_index+1}</a>
 8                 </#list>
 9                 </#if>
10                 <a href="${base}/workspace/todo.action?currentPage=${currentPage?if_exists+1}">下一页</a>
11                 <a href="${base}/workspace/todo.action?currentPage=${totalPage}">末页</a>
12             </div>
13             </#if>

 技术分享

这种逻辑想通了,其实很简单!

SSH简单分页Demo

标签:

原文地址:http://www.cnblogs.com/Nicholas-Cheng/p/4808907.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!