aspose file tools*
The moose likes Struts and the fly likes Using Struts to iterate over queries with aggregates Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Using Struts to iterate over queries with aggregates" Watch "Using Struts to iterate over queries with aggregates" New topic
Author

Using Struts to iterate over queries with aggregates

Eugene Williams
Greenhorn

Joined: Jan 31, 2010
Posts: 20
i'm working on an application where i need to process a hibernate query similar to the following:

Using this query, I hope to obtain a list of books with their various properties (title, author, number of pages, etc.).

my struts code inside of the view processes the list as follows:

What I want to add to this display is a final count of all the books returned by the query using:

How would I go about programming my struts code so that the count can be displayed? I realize that I can add a line such as:

But this value isn't accurate if I'm parsing out my pages with the following code before returning the list:


Thanks for any help...
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
The <s:iterator> tag includes a status attribute. You can use this to get the count of rows provided by the iterator (among other things). For example:
Eugene Williams
Greenhorn

Joined: Jan 31, 2010
Posts: 20
Thanks for the reply Mark, and for the suggestion. I apologize for not being more descriptive in my post. I am aware of this method for determining the number of items which were printed during the iteration process, however, I need to display the total number of books in the database rather than just the number of books within the display. Here is how the process works:

1. A user clicks a button to view a list of books
2. The application returns the first 25 books in the DB
3. The user clicks "Next" to review the next 25, and so on

What I need is a way to display the total number of books in the database on every page (i.e. "Viewing 1-25 of 472 books"). More specifically, I need to know how to handle a Hibernate query which requests data that is not native to the Object being queried; books have titles, authors, year published, and page numbers. Sometimes I want to count the total number of books matching a query:

or display the total number of pages in books covering a specific topic:

I'm not sure how to handle these aggregations in my Struts view during iteration.

Thanks.
Eugene Williams
Greenhorn

Joined: Jan 31, 2010
Posts: 20
So, after working on this today I was able to figure out how it should be done. I post this so that anyone who may be searching will know what steps to take.

The first thing I had to do was modify my query to include a handy subquery. Here's the new one:

The second part required changing the Struts view:

This way, not matter how many books I have displayed in my list (using paging methods), I can still display the total number of books matching the query. The key to figuring this out was understanding how the query was being processed and the list generated. My query results in multiple rows containing two columns (the first is the "Book" object, the second is the book count). Supplying an index to the OGNL expression gives me access to the appropriate column within the list.
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
That count won't show the total number of records returned by your query. It will return the total number of records in the table.
Eugene Williams
Greenhorn

Joined: Jan 31, 2010
Posts: 20
Thanks for the reply. But, this is exactly what I needed. I know what is necessary to display the number of records in the current list, as mentioned in an earlier post. The issue I wanted to resolve was that where I wanted to display a count such as:

"Displaying Books 26 through 50 of 289"

The "289" number is what I was trying to retrieve (check out yesterday's - March 16th's - post at 9:07 A.M. for a better understanding of what I mean)
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
I should probably just let this go, as I don't want to argue with you if you're sure what you want

However, if you have 5000 books in your database, and your user searches for all Java books, of which there are 450. When you display these 450 books 25 at a time, you really want to see:

while on page 1: Displaying books 1 through 25 of 5000
while on page 2: Displaying books 26 through 50 of 5000
...
while on page 18: Displaying books 425 through 450 of 5000

?
Eugene Williams
Greenhorn

Joined: Jan 31, 2010
Posts: 20
Thanks for pointing out the error in my query. Here's an updated version

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using Struts to iterate over queries with aggregates