SSH pager-taglib分页的实现

这个是我个人对分页的解决方案,其实就是在数据库底层做了分页查询
这套解决方案,是按照MVC的模式来做的,基于STRUTS框架。分为三个部分,表现层用的是JSP,其中用了JSTL和pager taglib做分页;控制层就是用的很传统的Action;底层就用的是Spring+Hibernate。下面分三个部分慢慢说:
说到分页:
1 首先新建一个PageModel类

Java代码

package com.dengmin.oa.utils;

import java.util.List;   
  
public class PageModel {   
  
    /**
      * 总记录数
      */  
    private int total;   
    /**
      * 当前页的记录集
      */  
    private List datas;   
  
    public List getDatas() {   
        return datas;   
     }   
  
    public void setDatas(List datas) {   
        this.datas = datas;   
     }   
  
    public int getTotal() {   
        return total;   
     }   
  
    public void setTotal(int total) {   
        this.total = total;   
     }   
}  

package com.dengmin.oa.utils;

import java.util.List;

public class PageModel {

/**
  * 总记录数
  */
private int total;
/**
  * 当前页的记录集
  */
private List datas;

public List getDatas() {
  return datas;
}

public void setDatas(List datas) {
  this.datas = datas;
}

public int getTotal() {
  return total;
}

public void setTotal(int total) {
  this.total = total;
}
}

2 底层是基于Spring+Hibernate来做的。在DAO这一层,为其增加两个方法,底层的分页查询用的是Hibernate提供的分页查询。
Java代码
public interface UserDao {   
  
    public void addUser(User user);   
  
    /**
      *
      * @param offset   从第几条记录开始查询
      * @param pagesize   每页显示多少条记录
      * @return
      */  
    public PageModel findAllUser(int offset, int pagesize);   
}  

public interface UserDao {

public void addUser(User user);

/**
  *
  * @param offset  从第几条记录开始查询
  * @param pagesize  每页显示多少条记录
  * @return
  */
public PageModel findAllUser(int offset, int pagesize);
}

3 UserDao的实现类
Java代码
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {   
  
    public void addUser(User user) {   
        this.getHibernateTemplate().save(user);   
     }   
       
    public PageModel findAllUser(int offset, int pagesize) {   
           
        //得到总记录数   
         String queryCountHql = "select count(*) from User";   
           
         Query query = getSession().createQuery(queryCountHql);   
        int total = ((Long)query.uniqueResult()).intValue();   
           
         List datas = getSession().createQuery("from User")   
                     .setFirstResult(offset)   
                     .setMaxResults(pagesize)   
                     .list();   
        //得到结果集   
         PageModel pm = new PageModel();   
         pm.setTotal(total);   
         pm.setDatas(datas);   
           
        return pm;   
     }   
  
}  

public class UserDaoImpl extends HibernateDaoSupport implements UserDao {

public void addUser(User user) {
  this.getHibernateTemplate().save(user);
}

public PageModel findAllUser(int offset, int pagesize) {
 
  //得到总记录数
  String queryCountHql = "select count(*) from User";
 
  Query query = getSession().createQuery(queryCountHql);
  int total = ((Long)query.uniqueResult()).intValue();
 
  List datas = getSession().createQuery("from User")
     .setFirstResult(offset)
     .setMaxResults(pagesize)
     .list();
  //得到结果集
  PageModel pm = new PageModel();
  pm.setTotal(total);
  pm.setDatas(datas);
 
  return pm;
}

}


ok!底层的实现工作就做好了

在控制层中UserAction从DispatchAction继承重写它的unspecified方法
通过spring注入UserDao 代码如下:
Java代码
public class UserAction extends DispatchAction {   
  
    private UserDao userDao;   
    @Override  
    protected ActionForward unspecified(ActionMapping mapping, ActionForm form,   
             HttpServletRequest request, HttpServletResponse response) throws Exception {   
           
        int offset = 0;   
           
        try {   
             offset = Integer.parseInt(request.getParameter("pager.offset"));   
         } catch (Exception e) {   
         }   
        //这里我按照每页显示10条   
         PageModel pm = userDao.findAllUser(offset, 10);   
         request.setAttribute("pm", pm);   
           
        return mapping.findForward("success");   
     }   
       
    public void setUserDao(UserDao userDao) {   
        this.userDao = userDao;   
     }   
       
}  

public class UserAction extends DispatchAction {

private UserDao userDao;
@Override
protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) throws Exception {
 
  int offset = 0;
 
  try {
   offset = Integer.parseInt(request.getParameter("pager.offset"));
  } catch (Exception e) {
  }
  //这里我按照每页显示10条
  PageModel pm = userDao.findAllUser(offset, 10);
  request.setAttribute("pm", pm);
 
  return mapping.findForward("success");
}

public void setUserDao(UserDao userDao) {
  this.userDao = userDao;
}

}


spring中的配置如下
Java代码
<!-- 配置sessionFactory -->   
     <bean id="sessionFactory"  
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">   
         <property name="configLocation">   
             <value>classpath:hibernate.cfg.xml</value>   
         </property>   
     </bean>   
     <!-- 配置事务管理器 -->   
     <bean id="transactionManager"  
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">   
         <property name="sessionFactory">   
             <ref bean="sessionFactory" />   
         </property>   
     </bean>   
  
     <!-- 配置事务的传播特性 -->   
     <tx:advice id="txAdvice" transaction-manager="transactionManager">   
         <tx:attributes>   
             <tx:method name="*" propagation="REQUIRED" />   
         </tx:attributes>   
     </tx:advice>   
  
  
<bean id="userDao" class="com.dengmin.pager.dao.UserDaoImpl">   
         <property name="sessionFactory" ref="sessionFactory" />   
     </bean>   
  
     <bean name="/userManager"  
        class="com.dengmin.pager.actions.UserAction" scope="prototype">   
         <property name="userDao" ref="userDao" />   
     </bean>  






--------------------------

最后是jsp页面做数据显示
这里采用了jstl和pager-taglib的标签库
首先将其引入:
Java代码
<%@ taglib prefix="c"   uri="http://java.sun.com/jsp/jstl/core" %>   
<%@ taglib prefix="pg" uri="http://jsptags.com/tags/navigation/pager" %>  

<%@ taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="pg" uri="http://jsptags.com/tags/navigation/pager" %>


Html代码
  <body>  
    <h1>User Manager</h1>  
    <hr/>  
       
    <table width="778" border="0" cellPadding="0" cellSpacing="1" bgcolor="#6386d6">  
    <tr bgcolor="#EFF3F7">  
        <TD align="center">ID</TD>  
        <TD align="center">名称</TD>  
        <TD align="center">密码</TD>  
        <TD align="center">地址</TD>  
        <TD align="center">创建时间</TD>  
        <TD align="center">相关操作</TD>  
           
    </tr>  
    <c:if test="${!empty pm.datas}">  
        <c:forEach items="${pm.datas}" var="user">  
            <tr bgcolor="#EFF3F7">  
                <td align="center">${user.id }</td>  
                <td align="center">${user.username }</td>  
                <td align="center">${user.password}</td>  
                <td align="center">${user.address}</td>  
                <td align="center">${user.createTime }</td>  
                <td align="center">  
                     修改   
                     &nbsp;   
                     删除</td>  
            </tr>  
        </c:forEach>  
        </c:if>  
         <c:if test="${empty pm.datas}">  
        <tr>  
            <td colspan="5" align="center" bgcolor="#EFF3F7">  
             没有找到相应的记录   
            </td>  
        </tr>  
        </c:if>  
    </table>  
    <pg:pager url="userManager.do" items="${pm.total}" export="currentPageNumber=pageNumber">  
    <pg:first>  
        <a href="${pageUrl}">首页</a>  
    </pg:first>  
    <pg:prev>  
        <a href="${pageUrl }">上一页</a>  
    </pg:prev>  
    <pg:pages>  
        <c:choose>  
            <c:when test="${currentPageNumber eq pageNumber}">  
                <font color="red">${pageNumber }</font>  
            </c:when>  
            <c:otherwise>  
                <a href="${pageUrl }">${pageNumber }</a>  
            </c:otherwise>  
        </c:choose>  
    </pg:pages>  
    <pg:next>  
        <a href="${pageUrl }">下一页</a>  
    </pg:next>  
    <pg:last>  
        <a href="${pageUrl }">尾页</a>  
    </pg:last>  
</pg:pager>  
  </body>  

你可能感兴趣的:(spring,Hibernate,jsp,ssh,配置管理)