struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Dariusz Wojtas" <dwoj...@gmail.com>
Subject Re: Display a list of items in multiples HTML files but within one JSP and action
Date Thu, 21 Dec 2006 18:13:29 GMT
No answer so far, then maybe I will help.
The described solution was found long time ago somewhere in the net, I
have also used it in some projects with success.

I use the displaytag library, which does excellent job in displaying
paged tabular data.
It has some annoyances and sometimes I think it is a semi-abandoned
project, but most of the fixes I needed were found in the project's
JIRA issues (patches).

Important.
The solution below works with displaytag using EXTERNALLY sorted pages.


When displaying paged data you need at least 2 things
1) info about the request
    I use a bean for it

    class PageRequest implements Serializable {
	private int pageNumber;  // the number of the requested page
	private int pageSize;      // requested page size
	private SortOrder sortOrder;   // UP-DOWN
	private String sortColumn;     // the column to use for sorting
	private boolean countResults;  // if I want it to count the number of
all matches

        ....
    }

   I have some utility that parses the request and returns such bean.
   I designed it to be compatible with displaytag and the way it
passes that info in URLs.
   Something like
      parseRequestBean(HttpServletRequest req, String tableName,
boolean countResults)

2) info about the result page
    class PageResponse<E> implements Serializable {
        private ArrayList<E> list;
        private int pageNumber;
        private long totalElmementCount;
        private int pageSize;

        ...
    }

I also have some utility methods (both Hibernate and JDBC) that accept:
  + the PageRequest
  + know which table to query
  + optional params
and return a PageResponse of beans.
My pagingmethods in DAOs look like this:

String baseQuery = "from MyHibernateBean myHibernateBean ....";
PageResult page = HibernatePager.page(MyHibernateBean.class,
        session,            // hibernate session
        baseQuery,
        "select count(*) " + baseQuery,
        params,
        paramTypes,    // hibernate specific stuff
        pRequest);

Then I have another utility method that stores my result page in the
request - in a way compatible with displaytag.
And ... that's all.

Maybe it sounds complicated but really works, and in fact requires
very little code when using it later. The whole concept is build with:
  2 beans(PageRequest, PageResponse)
  2-3 utility classes

It's usage in the action class is also very simple:

PageRequest pRequest = DisplayTagUtils.getRequest(getRequest(),
TABLE_NAME, PAGE_SIZE);
PageResponse<MyBean> page = getDbService().listMyBeans(param1, ..., pRequest);
DisplayTagUtils.storePage(getRequest(), TABLE_NAME, page);

That's all. I have tons of such actions/tables.
It only loads the beans that need to be displayed, with sorting done in DB.
This one works perfectly for me (after fixing some displaytag annoyances).


Will anyone else share other ideas?

Dariusz Wojtas


On 12/21/06, Fabio Miranda Hamburger <fabmirha@ns.isi.ulatina.ac.cr> wrote:
>
> Hello,
>
> Let's assume I have this big list of item, 250 records are going to be
> display.
>
> I have search.jsp that asks in a FORM the search criteria, then, based on
> the parameters, search.do is execute, and, let's say the first 25 items
> are shown, then, the next 25 (from 25 to 50) are shown and so on.
>
> Questions (all of them are related):
>
> 1. How can I coordinate this per basis display of items between the
> searchResults.jsp (search.do success to that file) and the action ?
>
> 2. In search.do action, how can I know the offset to display?
>
> 3. How searchResults.jsp keeps the offset between consecutive displays and
> how does it resend it to the action ?
>
> A simple code in JSP, html and STRUTS will be very kind.
>
> Best regards,
>
> ---
> Fabio Andrés Miranda
> Ingeniería en sistemas informáticos
> Universidad Latina - Costa Rica
> http://ns.isi.ulatina.ac.cr/~fabmirha
Mime
View raw message