【J2EE】jsp分页问题

来源:互联网 发布:endnote 在线数据库 编辑:程序博客网 时间:2024/05/19 06:49

我们的javaweb项目中在分页问题上似乎没有像在php和asp上那么容易,今天就来一探究竟。

0.分页工具类

在写分页的工具类之前,我们需要搞清楚分页中应该有哪些属性和属性之间的关系。
package util;import java.util.List;import org.springframework.stereotype.Component;/** * 分页工具类 * */@Componentpublic class Pager<T> {private int pageIndex = 0;//索引private int pageSize = 10;//页面大小private int pageNum = 1;//页码private int count;//总记录数private int pageCount;//总页数private List<T> list;//返回分页的集合private T entity;//pager对应的实体,可以用来做查询搜索时的分页/** * 获取当前页面索引 = (当前页号 - 1)*页面大小 */public int getPageIndex() {return (pageNum -1) * pageSize;}/** * 获取总页数 =(总记录数 -1)/页面大小 + 1 */public int getPageCount() {return (count - 1) / pageSize + 1;}public int getPageNum() {return pageNum;}public void setPageNum(int pageNum) {this.pageNum = pageNum;}public int getCount() {return count;}public void setCount(int count) {this.count = count;}public int getPageSize() {return pageSize;}public void setPageSize(int pageSize) {this.pageSize = pageSize;}public List<T> getList() {return list;}public void setList(List<T> list) {this.list = list;}public T getEntity() {return entity;}public void setEntity(T entity) {this.entity = entity;}}
工具类写好之后我们来看分页工具类怎么来使用

1.数据库访问层

@Overridepublic Pager<Shops> queryByPager(final Pager<Shops> pager) {List list = getHibernateTemplate().find("SELECT COUNT(s) FROM Shops s");int count = Integer.parseInt(list.get(0) + "");//给总记录数赋值pager.setCount(count);//查询数据库List<Shops> l = getHibernateTemplate().executeFind(new HibernateCallback<List<Shops>>() {@Overridepublic List<Shops> doInHibernate(Session session)throws HibernateException, SQLException {Query query = session.createQuery("FROM Shops");//起点即是pager的当前索引,默认值是0,即从表头开始query.setFirstResult(pager.getPageIndex());//将页面大小(默认为10)设置需要的结果集大小query.setMaxResults(pager.getPageSize());List<Shops> shopsList = query.list();return shopsList;}});//给分页后的集合赋值pager.setList(l);return pager;}

2.业务逻辑层代码(传球)

@Overridepublic Pager<Shops> queryByPager(Pager<Shops> pager) {return shopsDAO.queryByPager(pager);}

这里业务逻辑层啥也没干,传球到表现层。

3.表现层

action中 shop_list返回结果shops-list.jsp
public String list(){pager = shopsService.queryByPager(pager);return "shops_list";}
shops-list.jsp
    <table class="table table-hover text-center">      <tr>        <th width="100" style="text-align:left; padding-left:20px;">ID</th>        <th>店铺名称</th>        <th>店铺类别</th>        <th>店铺地址</th>        <th>联系人</th>        <th>联系电话</th>        <th width="10%">创建时间</th>        <th width="310">操作</th>      </tr>      <volist name="list" id="vo">      <s:iterator value="#request.pager.list" id="shops" >        <tr>          <td style="text-align:left; padding-left:20px;"><input type="checkbox" name="id[]" value="" />           ${shops.shopId }</td>          <td width="10%">${shops.shopName}</td>          <td>${shops.categories.categoryTitle} </td>          <td><font color="#00CC99">${shops.shopAddress }</font></td>          <td>${shops.shopContactname }</td>          <td>${shops.shopContacttel }</td>          <td>${shops.shopCreatetime }</td>          <td><div class="button-group"> <a class="button border-main" href="add.html"><span class="icon-edit"></span> 修改</a> <a class="button border-red" href="javascript:void(0)" onclick="return del(1,1,1)"><span class="icon-trash-o"></span> 删除</a> </div></td>        </tr>        </s:iterator>      <tr>        <td style="text-align:left; padding:19px 0;padding-left:20px;"><input type="checkbox" id="checkall"/>          全选 </td>        <td colspan="7" style="text-align:left;padding-left:20px;"><a href="javascript:void(0)" class="button border-red icon-trash-o" style="padding:5px 15px;" onclick="DelSelect()"> 删除</a>                 </tr>      <tr>        <td colspan="8">        <div class="pagelist">        ${pager.count }条,共${pager.pageCount }页            <c:if test="${pager.pageNum > 1 }">        <a href="shops_list.html?pager.pageNum=1">首页</a>        <a href="shops_list.html?pager.pageNum=${pager.pageNum-1 }">上一页</a>        </c:if>        <c:if test="${pager.pageNum == 1 }">        <span class="current">${pager.pageNum  }</span>        <a href="shops_list.html?pager.pageNum=${pager.pageNum +1}">${pager.pageNum + 1}</a>        <a href="shops_list.html?pager.pageNum=${pager.pageNum +2}">${pager.pageNum + 2}</a>        <a href="shops_list.html?pager.pageNum=${pager.pageNum +3}">${pager.pageNum + 3}</a>        <a href="shops_list.html?pager.pageNum=${pager.pageNum +4}">${pager.pageNum + 4}</a>        </c:if>        <c:if test="${pager.pageNum == 2 }">        <a href="shops_list.html?pager.pageNum=${pager.pageNum -1}">${pager.pageNum -1}</a>        <span class="current">${pager.pageNum  }</span>        <a href="shops_list.html?pager.pageNum=${pager.pageNum +1}">${pager.pageNum + 1}</a>        <a href="shops_list.html?pager.pageNum=${pager.pageNum +2}">${pager.pageNum + 2}</a>        <a href="shops_list.html?pager.pageNum=${pager.pageNum +3}">${pager.pageNum + 3}</a>        </c:if>        <c:if test="${pager.pageNum > 2 &&  pager.pageNum < pager.pageCount -1}">        <a href="shops_list.html?pager.pageNum=${pager.pageNum -2}">${pager.pageNum-2}</a>        <a href="shops_list.html?pager.pageNum=${pager.pageNum -1}">${pager.pageNum -1}</a>        <span class="current">${pager.pageNum  }</span>        <a href="shops_list.html?pager.pageNum=${pager.pageNum +1}">${pager.pageNum + 1}</a>        <a href="shops_list.html?pager.pageNum=${pager.pageNum +2}">${pager.pageNum + 2}</a>        </c:if>        <c:if test="${pager.pageNum == pager.pageCount -1 }">        <a href="shops_list.html?pager.pageNum=${pager.pageNum -3}">${pager.pageNum -3}</a>        <a href="shops_list.html?pager.pageNum=${pager.pageNum -2}">${pager.pageNum-2}</a>        <a href="shops_list.html?pager.pageNum=${pager.pageNum -1}">${pager.pageNum -1}</a>        <span class="current">${pager.pageNum  }</span>        <a href="shops_list.html?pager.pageNum=${pager.pageNum +1}">${pager.pageNum + 1}</a>        </c:if>        <c:if test="${pager.pageNum == pager.pageCount }">        <a href="shops_list.html?pager.pageNum=${pager.pageNum -4}">${pager.pageNum-4}</a>        <a href="shops_list.html?pager.pageNum=${pager.pageNum -3}">${pager.pageNum -3}</a>        <a href="shops_list.html?pager.pageNum=${pager.pageNum -2}">${pager.pageNum-2}</a>        <a href="shops_list.html?pager.pageNum=${pager.pageNum -1}">${pager.pageNum -1}</a>        <span class="current">${pager.pageNum  }</span>        </c:if>                <c:if test="${pager.pageNum < pager.pageCount}">        <a href="shops_list.html?pager.pageNum=${pager.pageCount }">尾页</a>        <a href="shops_list.html?pager.pageNum=${pager.pageNum+1 }">下一页</a>        </c:if>                 </div></td>      </tr>    </table>

4.效果展示





0 0
原创粉丝点击