You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by "Givler, Eric" <eg...@state.pa.us> on 2006/05/03 15:06:38 UTC

PaginatedList example

I opened the JPetstore demo and it makes use of the PaginatedList, but does so through another class, called BeanAction, and also uses Struts logic and bean:define tags for working with the list.

Is there an example out there that uses JSTL?  I have something working right now, but I'm concerned about my page now having scriptlet code in it.  

Specifically, action prior to the page calls the sqlmap.queryForPaginatedList, then puts the list in the session, along with a list count.  The count is really only used to determine if the page needs to show a table, or a message that no records were found.  The top of the page uses this scriptlet code though:


<%
   PaginatedList list = (PaginatedList) pageContext.getAttribute("sfsaved");
   pageContext.setAttribute("pageSize", new Integer( list.getPageSize() ) );
   pageContext.setAttribute("pageIndex", new Integer( list.getPageIndex() ) );
   pageContext.setAttribute("previousPageAvailable",
      new Boolean( list.isPreviousPageAvailable() ) );
   pageContext.setAttribute("nextPageAvailable", 
      new Boolean( list.isNextPageAvailable() ) );
%>

The code is so I can display rec# as a column:

<c:out value="${mystat.count + pageScope.pageIndex * pageScope.pageSize }" />

And, so my [previous page] and [next page] buttons can be dynamic:

               <c:choose>
                 <c:when test="${pageScope.previousPageAvailable}">
                    <input type="submit" name="event" value="<bean:message key="button.Water_Reports.previousSet" />"  />
                 </c:when>
                 <c:otherwise>
                    <input type="submit" name="event" value="<bean:message key="button.Water_Reports.previousSet" />" disabled="disabled" />
                 </c:otherwise>
              </c:choose>

So, is this the "right way" of doing things, or am I going about it all wrong?

I appreciate any feedback on this one.  I just started using iBatis a few days ago, and definitely want to adhere to the best practices.

Eric


Re: PaginatedList example

Posted by Ted Schrader <ts...@gmail.com>.
Hi Eric,

I'm not sure about a "right way" to manage the PaginatedList once we
get it out of iBATIS; I suppose it depends on the application using
it.  Thus, I think some may consider this thread to be slightly off
topic, but I agree that it would be nice to have some examples up on
the wiki.

For my Struts web app, I'm keeping the PaginatedList in session scope
(the page limit is 25, so I don't think this will cause problems). 
I'm assuming you're doing the same, since the only other alternative I
can think of is to somehow execute the exact same query again, get a
brand new PaginatedList, then manipulate the page accordingly before
sending it the the View layer.

In my JSPs, I'm doing the same as you but with Struts tags: checking
the isXXXXPageAvailable() methods to provide "previous" and "next"
links.

However, I have an idea to remove or cut down on the size of your
scriplet.  Would this work with the c tags?

<c:out value="${mystat.count + pageScope.sfsaved.pageIndex *
pageScope.sfsaved.pageSize }" />

<c:choose>
    <c:when test="${pageScope.sfsaved.previousPageAvailable}">
        <input type="submit" name="event" value="<bean:message
key="button.Water_Reports.previousSet" />"  />
    </c:when>
    <c:otherwise>
        <input type="submit" name="event" value="<bean:message
key="button.Water_Reports.previousSet" />" disabled="disabled" />
    </c:otherwise>
</c:choose>

The difference is drilling down through the "sfsaved" variable instead
of explicitly setting its properties into PageContext.

At risk of straying further off topic, how about this to take the
clean-up a bit further with your Previous button:

<% String prevDisabled = "disabled=\"disabled\""; %>
<c:choose>
    <c:when test="${pageScope.sfsaved.previousPageAvailable}">
        <% prevDisabled = ""; %>
    </c:when>
</c:choose>

<input type="submit" name="event" <%=prevDisabled%>
          value="<bean:message key="button.Water_Reports.previousSet" />"
/>

This makes it obvious that your submit button will always be
displayed; it's just a matter of whether it's disabled or not.  The
"downside", if it's necessary to label it as such, is that it's not
completely tag-based.  But, your button will show in the "design" mode
of JSP editors.

Ted