Pagination on the Internet to see a few structs pagination, feeling is not very perfect, so according to their own experience, wrote a relatively efficient and concise method of paging. Because of my limited level, if we have any better ideas, welcome to enlighten us.
I. Development environment
My development environment is: JBuilder x + Weblogic 8.1 + Oracle 9i + Windows 2003, if your friends ' development environment is not the same.
second, the development of ideas
Since it's about struts, it's natural to be out of MVC, as is the paging display.
1, the establishment of the database and the corresponding table, the table of this example is tcertificate.
2, set up the appropriate model components, corresponding to the database you want to query the table. This section is implemented by the DAO data Access layer, if some friends are unfamiliar with DAO, you can check the relevant information. This example is implemented by Certificatedao.java.
3, to establish the required model components of pagination, by JavaBean to act as, and Certificatedao implementation. Many of the methods introduced on the Web have the phenomenon of the coupling of data and paging components, which is the main difference between this method and other paging methods.
4, the establishment of controller components, this part by the action in struts to achieve. It is primarily responsible for instantiating the Certificatedao, taking only the data records to be displayed, depositing the ArrayList object and then returning, and putting it in the request. The paging section is constructed separately according to the paging condition, avoiding the situation that is mixed with DAO. Some of the other paging methods introduced on the Web are basically reading out all the query data at once and then constructing the paging-related component. In this way, if the amount of data is large, it is easy to create bottlenecks. In this example, because it is not a one-time read all the data of the query, but only read out a page to display the data records, which saves a lot of unnecessary data transmission, improve efficiency. This example is Certificateaction.java.
5, the establishment of the view component, this part by the JSP to act as, in order not to appear Java code, we use struts to provide a tag library, mainly responsible for removing the object from request just put in, by repeatedly calling Certificateaction and action parameters, and implement pagination display. This example is listcertificate.jsp.
6, establish and configure Struts-config.xml.
Third, the instance code
After you have identified the development ideas above, the code can be implemented in a single way.
1, build the database and the corresponding table.
2, the data logic layer of the relevant code.
1), the common DAO Class: Commondao.java
This is a generic DAO class that a lot of DAO are going to inherit, and I've summed it up in terms of practice, and this shows only the code associated with this example in order to reduce space.
Java code:
Code:
--------------------------------------------------------------------------------
Package com.xindeco.business;
Import java.io.*;
Import java.sql.*;
Import java.util.*;
Import javax.sql.*;
Import java.lang.IllegalAccessException;
Import java.lang.reflect.InvocationTargetException;
Import Org.apache.commons.beanutils.BeanUtils;
public class DAO
{
protected DataSource ds;
/**
* Note: Get the total number of records of the current query
*/
public int getRows ()
{
return this.count;
}
public void Rshandler (ResultSet rs, int offset, int limit)
{
Try
{
Count = 0;
Rs.absolute (-1);
Count = Rs.getrow ();
if (offset <= 0)
{
Rs.beforefirst ();
}
Else
{
Rs.absolute (offset);
}
}
catch (Exception e)
{
E.printstacktrace ();
}
}
Public DAO (DataSource DS) {
This.ds = ds;
}
public void Setdatasource (DataSource ds) {
This.ds = ds;
}
This class is mainly through the subclass of advanced result set, get the total number of records of the query, and the database connection for simple management.
findcertificatelist (int offset,int limit) is all the data to be displayed and put into the ArrayList. Have seen some examples on the internet, the data records into the ArrayList of the action process directly in the loop body to complete, if the word Gedo, will cause a way too pet big, and not beautiful. Here, the data records into the ArrayList action process by the Rs2vo method completes, is more neat. In addition, if (RS!= null && rs.next ()) in conjunction with while ((countrows++ < limit) && Rs.next ()) is considered for the robustness of the program, a little analysis is not difficult to conclude the knot On.
3, the establishment of controller components: Certificateaction.java
Certificateaction.java mainly takes the data out of the DAO, and puts it into a ArrayList, and then through the configuration file the JSP page of the software view.
5, the Establishment view listcertificate.jsp file.
Import java.util.*;
public class Pager {
private static int max_page_index = 10; How many pages does the footer display
private static String HEADER = "Result page";
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.