Avatar of pothios
pothios asked on

Pager Tag Lib

hello, i used page tag lib for displaying paged records. however, i do not understand how the paging works cos even time i click on a page there will be an error.  

the link generated by the paging tab lib is:
http://localhost:8088/customer?pager.offset=2

how do i handle it?
Java EEJSP

Avatar of undefined
Last Comment
pothios

8/22/2022 - Mon
summerian

What paging  library are you using, and how? Post your JSP code.
ASKER
pothios

hello heres the code im using
Vector result = result.getCustomerList();
<pg:pager items="<%= result.size() %>"
          index="center"
          maxPageItems="3"
          maxIndexPages="10"
          isOffset="<%= true %>"
          export="offset,currentPageNumber=pageNumber"
          scope="request">
  <%-- save pager offset during form changes --%>
    <input type="hidden" name="pager.offset" value="<%= offset %>">
  <TABLE BORDER="0">
    <TR>
      <th>Customer name</th>
      <TH>Details</TH>
      <TH>Contact</TH>
      <TH></TH>
    </TR> 
    
<% 
for (Iterator it = result.iterator();it.hasNext();) {
Customer cus= (Customer)it.next();
%>
    <pg:item>
      <TR>
        <td><img src="<%= cus.getName() %>" ></td>
        <td><%= cus.getDetails() %></td>
        <td><%= cus.getContact() %></td>
        <td><a href=viewCus?id="<%= cus.getId() %>"/></td>
      </TR>
      <TR>
    </TR>
    </pg:item>
<%
    }
%>
<tr>
<td>
<pg:index>
    <jsp:include page="/WEB-INF/jsp/altavista.jsp" flush="true"/>
  </pg:index><strong></strong>  </td>
  </tr>
</TABLE>
  
</pg:pager>

Open in new window

summerian

You should post altavista.jsp JSP as well. I'm guessing that you use pager tag library from jsptags.com (you did not post it either).

But my guess is, that you should supply "url" attribute to the pager tag - because generated links do not have context path included. Try this (assuming "customer" is a valid servlet mapping or some other valid application link):

<pg:pager items="<%= result.size() %>"
          ......
url="<%=((HttpServletRequest)request).getContextPath()+"/customer"%>"
          scope="request">

Open in new window

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ASKER
pothios

how to i use the offset to determine the next sql i should use to retrieve the next records?
summerian

I think that pager library takes care of cropping the whole record list to display current page. Paging libraries usually operate on whole lists, they are given page index, record list, and page size, so they could calculate page links, number of pages and so on. So try caching retrieved database record list (for example in session), and let pager library do the paging.

If you want to make paging sqls yourself, I think there's no point in using pager library - basically you will have to do everything yourself (calculate record offset from page offset, number of pages, etc). That's a bit of work.
ASKER
pothios

hmm im still confuse. currently, i have a page where i enter the search parameter that sends a request to a servlet and redirect user to the result jsp. by adding the following, im calling the customer servlet again which doesnt do anything.

url="<%=((HttpServletRequest)request).getContextPath()+"/customer"%>"
          scope="request">


//altavista.jsp
 
<%@ page session="false" %>
<%@ taglib uri="http://jsptags.com/tags/navigation/pager" prefix="pg" %>
<jsp:useBean id="currentPageNumber" type="java.lang.Integer" scope="request"/>
<font face=Helvetica size=-1>Result Pages:
<pg:prev export="pageUrl">&nbsp;<a href="<%= pageUrl %>">[&lt;&lt; Prev]</a></pg:prev>
<pg:pages><%
  if (pageNumber.intValue() < 10) {
    %>&nbsp;<%
  }
  if (pageNumber == currentPageNumber) {
    %><b><%= pageNumber %></b><%
  } else {
    %><a href="<%= pageUrl %>"><%= pageNumber %></a><%
  }
%>
</pg:pages>
<pg:next export="pageUrl">&nbsp;<a href="<%= pageUrl %>">[Next &gt;&gt;]</a></pg:next>
<br></font>

Open in new window

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
summerian

Well.. I don't know nearly any details of your program, that's why I'm guessing. However, I can advise you to do something like this:

1. Make servlet retrieving data from database as you do now, but also save the result list in session, and redirect it to JSP displaying the results (for example results.jsp).
2. In results.jsp use pager library tags and result list as you did, but retrieve them from session now (based on what you wrote, the first page after search displayed correctly).
3. Supply the pager library url with results.jsp url:
url="<%=((HttpServletRequest)request).getContextPath()+"/results.jsp"%>"

In this solution you will retrieve data from database only once (for example when user clicks search button). Navigation to different pages will use going directly to results.jsp with appropriate page number set as parameter (pager library takes care of generating links and then displaying correct page), and use session-cached list of results.
ASKER
pothios

hmm. i have set it as a session and redirect it to the same page but when i click the next page, it shows the same results as the 1st page :(


<% 
Collection results= (Collection)session.getAttribute("results");
%>
</p>
 
<pg:pager items="<%= results.size() %>"
          index="center"
          maxPageItems="3"
          maxIndexPages="10"
          url="<%=((HttpServletRequest)request).getContextPath()+"/search.jsp"%>"
          isOffset="<%= true %>"
          export="offset,currentPageNumber=pageNumber"
          scope="request">
  <%-- save pager offset during form changes --%>
    <input type="hidden" name="pager.offset" value="<%= offset %>">
  <TABLE>
    <TR>
      <th></th>
      <TH>Name</TH>
      <TH>description></TH>
    </TR>
    
<% 
if(results.iterator().hasNext()) {
for (Iterator it = results.iterator();it.hasNext();) {
Customer cus= (Customer )it.next();
%>
    <pg:item>
      <TR>
        <td><%= cus.getName() %>></td>
        <td><%= cus.getDescription() %>"></td>
        <td><%= cus.getTel() %></td>
      </TR>
      <TR><td</td>
    </TR>
    </pg:item>
<%
    }
} else {
%>
	<pg:item>
      <TR>
       <td><br/>
        No results</td>
      </TR>
      <TR>
    </TR>
    </pg:item>
<%
}
%>
<tr>
<td>
<pg:index>
    <jsp:include page="/WEB-INF/jsp/altavista.jsp" flush="true"/>
  </pg:index><strong></strong>  </td>
  </tr>
</TABLE>
</pg:pager>

Open in new window

summerian

What do the links look like? Do they have offset parameter?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER
pothios

ASKER CERTIFIED SOLUTION
summerian

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
pothios

hmm i nid some examples for displaytag. i will open a new question for it, thx