JAVA-SSH2 implementation of database and interface paging _java

Source: Internet
Author: User

Pagination should be the work that we often do when we develop Web applications, and it is important to be able to achieve a more concise implementation of the database and view-tier pagination.

In the database layer, the hibernate is used for paging the database, the data queried from the database is encapsulated as JavaBean, and the paging can be implemented conveniently in the view layer.

Create Pagebean

Package Com.fishing.common.bean; 
Import java.util.List;  @SuppressWarnings ("Unchecked") public class Pagebean {private list;//List of records for a page to return private int allrow; Total record number private int totalpage; Total pages private int currentpage; Current page private int pageSize; Number of records per page private Boolean isfirstpage; is the first page private Boolean islastpage; is the last page private Boolean haspreviouspage; Whether there is a previous page private Boolean hasnextpage; 
  Whether there is a next page public List GetList () {return list; 
  public void setlist (list list) {this.list = list; 
  public int Getallrow () {return allrow; 
  The public void Setallrow (int allrow) {this.allrow = Allrow; 
  public int gettotalpage () {return totalpage; 
  The 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; 
  The public void setpagesize (int pageSize) {this.pagesize = pageSize; 
    /**//** * Initialize paging information/public void init () {this.isfirstpage = Isfirstpage (); 
    This.islastpage = Islastpage (); 
    This.haspreviouspage = Ishaspreviouspage (); 
  This.hasnextpage = Ishasnextpage ();   
    /**//** * Below the information of the Judgment page, just the Getter method (is method) can * * @return/public boolean isfirstpage () { return (currentpage = = 1); If the current page is 1th page} public boolean islastpage () {return currentpage = = Totalpage; /If the current page is the last page} public boolean ishaspreviouspage () {return currentpage!= 1;////As long as the current page is not 1th page} public bool Ean Ishasnextpage () {return currentpage!= totalpage;////As long as the current page is not the last 1 pages}/**//** * Calculate the total number of pages, static method, for external direct through class Name call * * @param pageSize * Number of records per page * @param allrow * Total records * @return Total pages/PublIC static int counttotalpage (final int pageSize, final int allrow) {int totalpage = allrow% pageSize = 0? allrow 
    /pagesize:allrow/pagesize + 1; 
  return totalpage; /**//** * Calculates current page start record * * @param pageSize * Number of records per page * @param currentpage * Current ordinal Page * @return Current page start record number */public static int countoffset (final int pageSize, final int currentpage) {final I 
    NT offset = pageSize * (currentPage-1); 
  return offset;  /**//** * Calculates the current page, if 0 or does not have "? page=" in the requested URL, use 1 instead of * * @param page * Incoming parameter (may be empty, i.e. 0, return 1) * 
    @return Current Page */public static int countcurrentpage (int page) {final int curpage = (page = = 0? 1:page); 
  return curpage; 
 } 
}

Add a method to the DAO's abstract interface Basedao:

Public List queryforpage (final String hql, final int offset, 
      final int length); 
Implement the method in the implementation class Jianshedwdaoimpl of DAO public
list queryforpage (final String hql, final int offset, 
      final int length) { C5/>list List = Gethibernatetemplate (). Executefind (New Hibernatecallback () {public 
 
      Object doinhibernate (session session) 
          throws Hibernateexception, SQLException { 
        query query = session.createquery (HQL); 
        Query.setfirstresult (offset); 
        Query.setmaxresults (length); 
        List List = Query.list (); 
        return list; 
 
      } 
    }); 
 
    return list; 
  } 

Add a method to the Service abstraction Layer interface Jianshedwservice:

Public Pagebean queryforpage (int pagesize,int currentpage);  

Implement the method in the service implementation class:

Public Pagebean queryforpage (int pageSize, int page) { 
    final String hql = ' from Jianshedwbean ';//query statement 
    int Allrow = This.baseDao.getAllRowCount (HQL); Total number of records 
    int totalpage = Pagebean.counttotalpage (pageSize, allrow);/Total pages 
    final int offset = Pagebean.countoffset (pageSize, page); Current page start record 
    final int length = pageSize//per page record number 
    final int currentpage = pagebean.countcurrentpage (page); 
    list<jianshedwbean> list = This.baseDao.queryForPage (hql, offset, length); "one page" record 
 
    //Save paging information to bean 
    pagebean Pagebean = new Pagebean (); 
    Pagebean.setpagesize (pageSize); 
    Pagebean.setcurrentpage (currentpage); 
    Pagebean.setallrow (Allrow); 
    Pagebean.settotalpage (totalpage); 
    Pagebean.setlist (list); 
    Pagebean.init (); 
    return pagebean; 
 
  } 

Creating a paging model in the view-layer action

Package COM.FISHING.ACTION.LCQ; 
Import Com.fishing.common.bean.JianSheDWBean; 
Import Com.fishing.common.bean.PageBean; 
Import Com.fishing.service.lcq.JianSheDWService; 
 
Import Com.opensymphony.xwork2.ActionSupport; 
 
  @SuppressWarnings ("Serial") public class Getinfojsdwlistaction extends Actionsupport {private int page;//page Private Pagebean Pagebean; 
  Beans that contain distribution information private Jianshedwbean Jianshedwbean; 
  Private Pagebean page; 
 
  Private Jianshedwservice Jianshedwservice; 
  public int GetPage () {return page; 
  public void setpage (int page) {this.page = page; 
  Public Pagebean Getpagebean () {return pagebean; 
  public void Setpagebean (Pagebean pagebean) {This.pagebean = Pagebean; 
  Public Jianshedwbean Getjianshedwbean () {return jianshedwbean; 
  public void Setjianshedwbean (Jianshedwbean jianshedwbean) {This.jianshedwbean = Jianshedwbean; } public Jianshedwservice GetjiansheDwservice () {return jianshedwservice;  
  public void Setjianshedwservice (Jianshedwservice jianshedwservice) {this.jianshedwservice = Jianshedwservice; @Override public String Execute () throws Exception {//pagination Pagebean, parameter pagesize represents the number of records per page, page is   
   Front page This.pagebean = jianshedwservice.queryforpage (page); 
  return SUCCESS; 
 } 
}

Write paging in JSP

<tr class= "Odd" > <td> &l t;/td> <td> <s:if test= "%{pagebean.currentpage = 1}" &   
                           Gt 
                              Home prev </s:if> <s:else> 
                                <a href= "jianguan/getjsdwinfos.action?page=1" > Home </a> <a href= "Jianguan/getjsdwinfos.action?page=<s:property value="%{pagebean.currentpage-1} "/>"/> Previous page ;/a> </s:else> </td> & 
 
                              lt;td> <s:if test= "%{pagebean.currentpage!= pagebean.totalpage}" > <a href= "Jianguan/getjsdwinfos.action?page=<s:property VALue= "%{pagebean.currentpage+1}"/> "> next page </a> <a 
                              href= "Jianguan/getjsdwinfos.action?page=<s:property value=" Pagebean.totalpage "/>" > Last  
                           </a> </s:if> <s:else> 
 
                          Next Last </s:else> </td> 
                              <td> <div align= "center" > page times 
                              <s:property value= "Pagebean.currentpage"/>/ 
                              <s:property value= "Pagebean.totalpage"/> Altogether  
                      <s:property value= "Pagebean.allrow"/> Records </div>      <div align= "center" ></div> </td> </tr>

The above is just the implementation of the code, does not describe the configuration file configuration, the reader according to the situation configuration.

I hope this article is helpful to you, SSH2 implementation of the database and interface of the paging content is introduced here. I hope you will continue to pay attention to our website! Want to learn Java can continue to focus on this site.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.