In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to achieve Struts+Hibernate pagination". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to achieve Struts+Hibernate pagination".
1. There are two structures for paging in Struts:
1. In Action, all the records are queried by DAO, then added to the session or request object, passed to the client, and paged by JSP. This method is very convenient when the amount of data is small, and does not affect the speed.
two。 In Action, only one page of records are queried through DAO at a time, and then passed to the JSP page. This structure is good for programs with a large amount of data, but for a small amount of data, it will increase the request to the server and increase the load on the server.
2. Hibernate query
Since the fixed-point quantitative query method for the database is directly provided in Hibernate, I use the second method.
For example, 100 records are taken out from Article 10, 000.
Query Q = session.createQuery ("from Cat as c"); q.setFirstResult (10000); q.setMaxResults (100); List l = q.list ()
III. Concrete realization
1.Pager class
Package com.jpcf.db.helper;import java.math.*;public class Pager {private int totalRows; / / Total number of rows private int pageSize = 10; / / number of rows displayed per page private int currentPage; / / current page number private int totalPages; / / total number of pages private int startRow; / / the starting line of the current page in the database public Pager () {} public Pager (int _ totalRows) {totalRows = _ totalRows;totalPages=totalRows/pageSize;int mod=totalRows%pageSize;if (mod > 0) {totalPages++;} currentPage = 1 } public int getStartRow () {return startRow;} public int getTotalPages () {return totalPages;} public int getCurrentPage () {return currentPage;} public int getPageSize () {return pageSize;} public void setTotalRows (int totalRows) {this.totalRows = totalRows;} public void setStartRow (int startRow) {this.startRow = startRow;} public void setTotalPages (int totalPages) {this.totalPages = totalPages;} public void setCurrentPage (int currentPage) {this.currentPage = currentPage;} public void setPageSize (int pageSize) {this.pageSize = pageSize } public int getTotalRows () {return totalRows;} public void first () {currentPage = 1X startRow = 0;} public void previous () {if (currentPage = = 1) {return;} currentPage--;startRow = (currentPage- 1) * pageSize;} public void next () {if (currentPage)
< totalPages) {currentPage++;}startRow = (currentPage - 1) * pageSize;}public void last() {currentPage = totalPages;startRow = (currentPage - 1) * pageSize;}public void refresh(int _currentPage) {currentPage = _currentPage;if (currentPage >TotalPages) {last ();}
The Pager class is used to calculate the starting row and current page number of the first page, the previous page, the next page, and the last page in the database.
2.PagerHelp class
Package com.jpcf.db.helper;import javax.servlet.http.*;public class PagerHelper {public static Pager getPager (HttpServletRequest httpServletRequest,int totalRows) {
/ / define the pager object, which is used to pass to the page
Pager pager = new Pager (totalRows)
/ / get the current page number from the Request object
String currentPage = httpServletRequest.getParameter ("currentPage")
/ / if the current page number is empty, query the page as *.
/ / if it is not empty, refresh the pager object and enter the current page number and other information
If (currentPage! = null) {pager.refresh (Integer.parseInt (currentPage));}
/ / get the currently executed method, front page, previous page, back page, and last page.
String pagerMethod = httpServletRequest.getParameter ("pageMethod"); if (pagerMethod! = null) {if (pagerMethod.equals ("first")) {pager.first ();} else if (pagerMethod.equals ("previous")) {pager.previous ();} else if (pagerMethod.equals ("next")) {pager.next ();} else if (pagerMethod.equals ("last")) {pager.last ();}} return pager;}}
I needless to say that I know what to do with the class PageHelper.
3.DAO class
Package com.jpcf.db.dao;import com.jpcf.db.model.*;import com.jpcf.db.helper.HibernateUtil;import net.sf.hibernate.*;import java.util.*;import com.jpcf.db.controller.*;public class VehiclePropertyDAO {public Collection findWithPage (int pageSize, int startRow) throwsHibernateException {Collection vehicleList = null;Transaction tx = null;try {Session session = HibernateUtil.currentSession (); tx = session.beginTransaction (); Query Q = session.createQuery ("from VehicleProperty vp"); q.setFirstResult (startRow); q.setMaxResults (pageSize) VehicleList = q.list (); tx.commit ();} catch (HibernateException he) {if (tx! = null) {tx.rollback ();} throw he;} finally {HibernateUtil.closeSession ();} return vehicleList;} public int getRows (String query) throwsHibernateException {int totalRows = 0 X transaction tx = null;try {Session session = HibernateUtil.currentSession (); tx = session.beginTransaction (); totalRows = (Integer) session.iterate (query). Next (). IntValue (); tx.commit () } catch (HibernateException he) {if (tx! = null) {tx.rollback ();} throw he;} finally {HibernateUtil.closeSession ();} return totalRows;}}
I will post the code needed for these paging in the DAO class.
"from VehicleProperty vp" can also be passed in with a parameter. If you are interested, change it yourself.
4.Action
Here is the code used in Action:
Public ActionForward queryWithPage (ActionMapping actionMapping,ActionForm actionForm,HttpServletRequest httpServletRequest,HttpServletResponse httpServletresponse) {
Collection clInfos = the collection of records that null;// is used to output to the page
Total number of rows recorded by int totalRows;//
VehiclePropertyDAO vehicleDAO = new VehiclePropertyDAO ()
/ / get the total number of rows in the current table
Try {totalRows = vehicleDAO.getRows ("select count (*) from VehicleProperty");} catch (Exception ex) {servlet.log (ex.toString ()); return actionMapping.findForward (Constants.FAILURE);}
/ / get the pager object for output to the page through the PagerHelper class
Pager pager=PagerHelper.getPager (httpServletRequest,totalRows)
/ / fetch pageSize line records starting from startRow
Try {clInfos = vehicleDAO.findWithPage (pager.getPageSize (), pager.getStartRow ());} catch (Exception ex) {servlet.log (ex.toString ()); return actionMapping.findForward (Constants.FAILURE);}
/ / Save the output recordset and pager object to the request object
HttpServletRequest.setAttribute (CLINFOS, clInfos); httpServletRequest.setAttribute (PAGER, pager)
Return actionMapping.findForward (Constants.SUCCESS)
}
The query statement select count (*) from VehicleProperty can also be changed into any condition you need (select count (*) from VehicleProperty where.)
5.JSP page usage
Here is the application in JSP:
Total page = "/ bussiness/clInfo/queryWithPage.do?method=queryWithPage&pageMethod=first previous page next last page" paramName= "PAGER" paramProperty= "currentPage" paramId= "currentPage" > Home page
Explain the line: "/ bussiness/clInfo/queryWithPage.do?method=queryWithPage&pageMethod=first
Method=queryWithPage is because my Action inherits from DispatchAction and requires a method parameter
PageMethod=first is used to determine which operation to perform in the PageHelper class
Thank you for your reading, the above is the content of "how to achieve Struts+Hibernate pagination", after the study of this article, I believe you have a deeper understanding of how to achieve Struts+Hibernate pagination, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.