Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Implement pagination using beans??

Status
Not open for further replies.

RicardoPereira

Programmer
Jun 3, 2003
255
PT
Hi,

Anyone have an example of how to implement pagination using beans?
I have two class files, one to define the database access and all the querys and the other to define the fields that i want to get. My question is how do i control the number of records to display and implement next and back buttons.
All the samples i saw have the connection string in the same code as the code that control the number os records per page.

With an example is better to understand.

Thanks
Ricardo
 
Hi,

Get the records from the DB and store it in a ArrayList and add the ArrayList to session object and to the next and previous options send the offset value of the ArrayList with URI to the Next and Previous pages and iterate the ArrayList with as number of objects that need to be displayed in the page.

Hope this will help you, if not let me know.. so that I can send you an example of it.

Cheers,
Venu
 
Hi,

Here is the Example, of paging. It is just a single JSP file. If required move the logic to java file. Copy the below code and execute the JSP file to make sure every thing works fine. If any questions let me know..

<%@ page import=&quot;java.util.ArrayList,
java.util.Iterator,
java.util.List&quot;%>
<%
ArrayList arrayList = null;
boolean check = true;
// session.removeAttribute(&quot;arrayList&quot;);
/* if you have the arrayList in the session Object avoid the DB Call
and get the ArrayList from the session Object
*/
if(null == session.getAttribute(&quot;arrayList&quot;))
{
/** need to get the ArrayList from the DB */
arrayList = new ArrayList();
arrayList.add(&quot;First&quot;);
for(int i=0; i<10; i++)
{
arrayList.add(i+&quot;&quot;);
}
arrayList.add(&quot;Last&quot;);
session.setAttribute(&quot;arrayList&quot;, arrayList);
}else{
arrayList = (ArrayList)session.getAttribute(&quot;arrayList&quot;);
}

int arrayListSize = arrayList.size();
// Number of Records that need to displayed per page
// make the increment value 5 and check it will display only 5 records per page
int increment = 2;
int fromIndex = 0;
int toIndex = increment;
String uri = request.getRequestURI();
String previous= &quot;Previous&quot;;
String next = &quot;Next&quot;;
List displayList = null;
if( null != request.getParameter(&quot;next&quot;))
{
fromIndex = Integer.parseInt(request.getParameter(&quot;next&quot;));
toIndex = increment + fromIndex;
if( toIndex+1 > arrayListSize)
{
toIndex = arrayListSize;
check = false;
}
if( fromIndex > arrayListSize)
fromIndex = 0;
}

if( null != request.getParameter(&quot;prev&quot;))
{
toIndex = Integer.parseInt(request.getParameter(&quot;prev&quot;));
fromIndex = toIndex - increment;
}
if(arrayListSize > 0)
displayList = arrayList.subList(fromIndex, toIndex);
if(fromIndex != 0)
previous = &quot;<a href=&quot;+ uri +&quot;?prev=&quot;+ fromIndex +&quot;> Previous </a>&quot;;
if(toIndex != 0 && check)
next = &quot;<a href=&quot;+ uri +&quot;?next=&quot;+ toIndex +&quot;> Next </a>&quot;;

out.println(arrayList);
out.println(&quot;<br>&quot;);
out.println(displayList);
out.println(&quot;<br>&quot;);
out.println(next);
out.println(&quot;<br>&quot;);
out.println(previous);
%>

Cheers,
Venu
 
Yes, with this i got the next and previous links but the records showed are very strange, look:

[extranet.TblClientes@ed5d9d, extranet.TblClientes@19d12cc, extranet.TblClientes@13d1402, extranet.TblClientes@eca36e, First, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, Last]
[extranet.TblClientes@ed5d9d, extranet.TblClientes@19d12cc]
Next
Previous
--------------------

With the simple bean i have this code:

<%

ArrayList LC=form.getLC();
for( int x=0;x<LC.size();x++)
{
TC qryClt=(TC) LC.get(x);
out.println(&quot;qryClt.getSitio() +
&quot;,&quot; + qryClt.getCliente() +
&quot;'>&quot; + qryClt.getSitio() + &quot; | &quot; + qryClt.getCliente() + &quot;);
}
%>


How do i adapt whis code to the code you give me?

Thanks
Ricardo
 
Hi,

I have marked the changes with RED Color. If not copy the below code it should work. But donot forget to import the bean into this jsp file.

<%@ page import=&quot;java.util.ArrayList,
java.util.Iterator,
java.util.List&quot;%>
<%
ArrayList arrayList = null;
boolean check = true;
// session.removeAttribute(&quot;arrayList&quot;);
/* if you have the arrayList in the session Object avoid the DB Call
and get the ArrayList from the session Object
*/
if(null == session.getAttribute(&quot;arrayList&quot;))
{
/** need to get the ArrayList from the DB */
arrayList = form.getLC();
session.setAttribute(&quot;arrayList&quot;, arrayList);
}else{
arrayList = (ArrayList)session.getAttribute(&quot;arrayList&quot;);
}

int arrayListSize = arrayList.size();
// Number of Records that need to displayed per page
// make the increment value 5 and check it will display only 5 records per page
int increment = 2;
int fromIndex = 0;
int toIndex = increment;
String uri = request.getRequestURI();
String previous= &quot;Previous&quot;;
String next = &quot;Next&quot;;
List displayList = null;
if( null != request.getParameter(&quot;next&quot;))
{
fromIndex = Integer.parseInt(request.getParameter(&quot;next&quot;));
toIndex = increment + fromIndex;
if( toIndex+1 > arrayListSize)
{
toIndex = arrayListSize;
check = false;
}
if( fromIndex > arrayListSize)
fromIndex = 0;
}

if( null != request.getParameter(&quot;prev&quot;))
{
toIndex = Integer.parseInt(request.getParameter(&quot;prev&quot;));
fromIndex = toIndex - increment;
}
if(arrayListSize > 0)
displayList = arrayList.subList(fromIndex, toIndex);
if(fromIndex != 0)
previous = &quot;<a href=&quot;+ uri +&quot;?prev=&quot;+ fromIndex +&quot;> Previous </a>&quot;;
if(toIndex != 0 && check)
next = &quot;<a href=&quot;+ uri +&quot;?next=&quot;+ toIndex +&quot;> Next </a>&quot;;

out.println(arrayList);
out.println(&quot;<br>&quot;);
//out.println(displayList);

Iterator iterator = displayList.iterator();
while(iterator.hasNext())
{
TC qryClt=(TC) iterator.next();
out.println(qryClt.getSitio() + &quot;,&quot; + qryClt.getCliente() + &quot;'>&quot; + qryClt.getSitio() + &quot; | &quot; + qryClt.getCliente() + &quot;&quot;);

}

out.println(&quot;<br>&quot;);
out.println(next);
out.println(&quot;<br>&quot;);
out.println(previous);
%>

Cheers
Venu

 
If i put int increment = 5 and i had only 4 records i got an error message that says:
org.apache.jasper.JasperException: toIndex = 5

What should be done to return all the 4 records even if they are lower than the maximun defined per page?

Thanks
Ricardo
 
Hi,

Change the code in red
if(arrayListSize > 0)
displayList = arrayList.subList(fromIndex, toIndex);


To

if(arrayListSize > 0)
{
if(increment > arrayListSize){
toIndex = arrayListSize;
displayList = arrayList.subList(fromIndex, toIndex);
toIndex = 0;
}else{
displayList = arrayList.subList(fromIndex, toIndex);
}
}

Cheers,
Venu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top