自我的SSH2实现数据库和界面的分页



自我运行的::::::
pageBean 类

package com.test;

import java.io.Serializable;
import java.util.List;

public  class PageBean    {
  
        
         private List list;  // 要返回的某一页的记录列表    

         private  int allRow;  // 总记录数    
         private  int totalPage;  // 总页数    
         private  int currentPage;  // 当前页    
         private  int pageSize;  // 每页记录数    

            
         private  boolean isFirstPage;  // 是否为第一页    
         private  boolean isLastPage;  // 是否为最后一页    
         private  boolean hasPreviousPage;  // 是否有前一页    
         private  boolean hasNextPage;  // 是否有下一页    

         public List getList() {    
                 return list;    
        }    

         public  void setList(List list) {    
                 this.list = list;    
        }    

         public  int getAllRow() {    
                 return allRow;    
        }    

         public  void setAllRow( int allRow) {    
                 this.allRow = allRow;    
        }    

         public  int getTotalPage() {    
                 return totalPage;    
        }    

         public  void setTotalPage( int totalPage) {    
                 this.totalPage = totalPage;    
        }    

         public  int getCurrentPage() {    
                 return currentPage;    
        }    

         public  void setCurrentPage( int currentPage) {    
                 this.currentPage = currentPage;    
        }    

         public  int getPageSize() {    
                 return pageSize;    
        }    

         public  void setPageSize( int pageSize) {    
                 this.pageSize = pageSize;    
        }    

         /** */ 
         /**    
         * 初始化分页信息    
         */ 
         public  void init() {    
                 this.isFirstPage = isFirstPage();    
                 this.isLastPage = isLastPage();    
                 this.hasPreviousPage = isHasPreviousPage();    
                 this.hasNextPage = isHasNextPage();    
        }    

         /** */ 
         /**    
         * 以下判断页的信息,只需getter方法(is方法)即可    
         *     
         * @return    
         */ 

         public  boolean isFirstPage() {            
                 return (currentPage == 1); // 如是当前页是第1页            
        }         
         public  boolean isLastPage() {            
return currentPage == totalPage;  //如果当前页是最后一页            
}         
         public  boolean isHasPreviousPage() {            
return currentPage != 1;  //只要当前页不是第1页            
}         
         public  boolean isHasNextPage() {            
return currentPage != totalPage;  //只要当前页不是最后1页            
}         
         /** */ 
         /**    
         * 计算总页数,静态方法,供外部直接通过类名调用    
         *     
         * @param pageSize    
         *                        每页记录数    
         * @param allRow    
         *                        总记录数    
         * @return 总页数    
         */ 
         public  static  int countTotalPage( final  int pageSize,  final  int allRow) {    
                 int totalPage = allRow % pageSize == 0 ? allRow / pageSize : allRow    
                                / pageSize + 1;    
                 return totalPage;    
        }    

         /** */ 
         /**    
         * 计算当前页开始记录    
         *     
         * @param pageSize    
         *                        每页记录数    
         * @param currentPage    
         *                        当前第几页    
         * @return 当前页开始记录号    
         */ 
         public  static  int countOffset( final  int pageSize,  final  int currentPage) {    
                 final  int offset = pageSize * (currentPage - 1);    
                 return offset;    
        }    

         /** */ 
         /**    
         * 计算当前页,若为0或者请求的URL中没有"?page=",则用1代替    
         *     
         * @param page    
         *                        传入的参数(可能为空,即0,则返回1)    
         * @return 当前页    
         */ 
         public  static  int countCurrentPage( int page) {    
                 final  int curPage = (page == 0 ? 1 : page);    
                 return curPage;    
        }    

}

Action相应的方法
public String showAll() throws Exception {
      String sql =  "from User user ";
       this.pagebean =loginServce.findAllByPage(pageSize, page,sql);
      ActionContext context=ActionContext.getContext();
      Map map=context.getSession();
      map.put( "pagebean", pagebean);
       return  "all";
}
  
//分页Dao类的分页方法

   public  List findAllByPage(String hql,  int  offset,  int  length) {
    Session session=sessionFactory.openSession();
    Query query=session.createQuery(hql);
  
    query.setFirstResult(offset);
    query.setMaxResults(length);
    List list=query.list();
    
   /*  for(int i=0;i<list.size();i++){
      LeaveMessage leaveMessage=(LeaveMessage)list.get(i);
      
      System.out.println("levaeMessage.leaveTime="+leaveMessage.getLeaveTime());
    }*/
   //  session.close();
     return  list;  
  }

  
   public  PageBean findAllByPage( int  pageSize,  int  page,String hql) {
                 // String hql="from Break breakInfo order by id desc"; 
            
      int  allRow= this .getAllRowCount(hql);
     
  
      int  totalPage=PageBean.countTotalPage(pageSize, allRow);    
      int  offset=PageBean.countOffset(pageSize, page); 
      if (offset<0)
     {
        offset=0;
     }
     
      final  int  length=pageSize;
      final  int  currentPage=PageBean.countCurrentPage(page);    
     List    list= this .findAllByPage(hql, offset, length);
     
     PageBean pageBean= new  PageBean();
     pageBean.setPageSize(pageSize);
     pageBean.setAllRow(allRow);
     pageBean.setCurrentPage(currentPage);
     pageBean.setTotalPage(totalPage);
     pageBean.setList(list);
     pageBean.init();  
      return  pageBean;
  }
  
   public  int  getAllRowCount(String hql) {
    Session session=sessionFactory.getCurrentSession();
    Query query=session.createQuery(hql);
    List list=query.list();
    session.close();
     return  list.size();
  }


相应的分页jsp页面

< %@ page  language ="java"  import ="java.util.*"  pageEncoding ="UTF-8"% >
< %@ taglib  prefix ="c"  uri ="http://java.sun.com/jsp/jstl/core" % >
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
< html >
     < head >
         < base href="<%=basePath% >">
        
         < title >My JSP 'index.jsp' starting page </title>
   < meta  http-equiv ="pragma"  content ="no-cache" >
   < meta  http-equiv ="cache-control"  content ="no-cache" >
   < meta  http-equiv ="expires"  content ="0" >        
   < meta  http-equiv ="keywords"  content ="keyword1,keyword2,keyword3" >
   < meta  http-equiv ="description"  content ="This is my page" >
  <!--
   < link  rel ="stylesheet"  type ="text/css"  href ="styles.css" >
  -->
     </head>
    
     < body >
< table  width ="619"  height ="294"  border ="1"  cellpadding ="0.05"  cellspacing ="0.05"  align ="center" >
         < tr  align ="center" >
             < td >密码 </td>
             < td >用户 </td>
      < td >操作 </td>
         </tr>
         < c:forEach  items ="${pagebean.list}"  var ="list"  >
         < tr >
             < td >${list.password}  </td>
             < td >${list.userName}  </td>
         < td > < a  href ="loginManagerControl!delet.action?id=${list.id}"  onclick ="return confirm('你确定要删除吗?')" >删除 </a>
         < a  href ="loginManagerControl!updateUI.action?id=${list.id}"  >修改 </a> 
                 < a  href ="loginManagerControl!datil.action?id=${list.id}"  >详细 </a> 
          </td>
         </tr>
      </c:forEach>
     
    
     </table>
      < center >
    共${ pagebean.allRow} 条记录
                共${pagebean.totalPage} 页
                当前第${pagebean.currentPage }页 < br />
                 < c:choose >
                 < c:when  test ="${pagebean.currentPage == 1}" >
                
             
                        第一页 上一页
                         </c:when>
          < c:otherwise >
                         < a  href ="loginManagerControl!showAll.action?page=1" >第一页 </a>
                         < a  href ="loginManagerControl!showAll.action?page=${pagebean.currentPage-1}" >上一页 </a>
              </c:otherwise>
                 </c:choose>
                
                 < c:choose >
                 < c:when  test ="${pagebean.currentPage != pagebean.totalPage}" >
         
                         < a  href ="loginManagerControl!showAll.action?page=${pagebean.currentPage+1}" >下一页 </a>
                         < a  href ="loginManagerControl!showAll.action?page=${pagebean.totalPage}" >最后一页 </a>
                 </c:when>
                 < c:otherwise >
                
                        下一页 最后一页
                 </c:otherwise>
                 </c:choose>
   </center>  
     </body>
</html>


你可能感兴趣的:(数据库,的)