aspose file tools*
The moose likes JSP and the fly likes JSTL Pagination for Map inside Map. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "JSTL Pagination for Map inside Map." Watch "JSTL Pagination for Map inside Map." New topic
Author

JSTL Pagination for Map inside Map.

kiran badi
Greenhorn

Joined: Oct 26, 2009
Posts: 26
I need to Implement simple pagination displaying 100 records per page and pagination data is sorted by date.Though I have sorted the information correctly,however I am unable to think about pagination logic since resultset is inside map which is again holding Map inside it,

Below is my JSTL Code along with definition for TitleList/Title which I am iterating after adding resultset data in it,



I have also asked this question in SO, and the link for the same is here.All information as how I am generating datastructure and other details are in below link.

http://stackoverflow.com/questions/11876495/jstl-pagination-for-map-inside-map

Appreciate if someone can give me some thoughts on this as if this can be done the I am thinking of doing it.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61198
    
  66

What is up with this:

<%=request.getContextPath()%>



You use the EL everywhere else -- why the scriptlet?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
kiran badi
Greenhorn

Joined: Oct 26, 2009
Posts: 26
Hmm I can remove that scriplets,no issues,but my core problem is pagination and it seems like I am totally getting lost on this.I just dont see the way to get this done the way I am looking for.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61198
    
  66

It's in the JspFaq: ${pageContext.request.contextPath}. Glad to see that you are using the context path -- a lot of people do not and it makes things very fragile.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61198
    
  66

If you want help with your question, you'll need to post the code here. Few people are going to go to SO just to see your code.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Maybe I'm missing something, but "pagination" is far from a complete description of your problem.

HTML doesn't really lend itself to breaking a document into pages, since it's designed to be displayed in a browser, for which the data model is just a long stream of data. Pages don't make sense in this environment. And so I can't imagine what you expect your paginated output to look like.

So... what exactly do you want to produce? That's always the first question to ask. Once you have a description of your desired output, only then can you ask questions about how to produce it.
kiran badi
Greenhorn

Joined: Oct 26, 2009
Posts: 26
Fair enough Bear,

Here is Title list map whose output looks like below


and this is piece of servlet which populates the Titlelist map contains another map which is dynamic in size,all data is read from the db,

kiran badi
Greenhorn

Joined: Oct 26, 2009
Posts: 26
Paul,

Pagination is what I am looking for, I can put the data in the list and then display it page wise till the last record, but this is not the functionality my client is looking for.He wants titles to be displayed datewise and then keep displaying till the last date with each page showing 100 records.

The data is sorted datewise in the descending order with current date being the first and followed by titles for that date.Titles for dates is dynamic and could be of varying size.

If you need to look at real time, take a look the way craglists does the pagination.Thats the simplest live example I can think of.

- Kiran
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

You keep repeating the word "pagination" as though only an idiot could not understand what you meant.

I expect Craigslist is blocked from where I'm posting. When you come up with a description of what you want, let me know.
kiran badi
Greenhorn

Joined: Oct 26, 2009
Posts: 26
Ok Paul, let me try again what I need,

I need to display data based on date with latest date on top.

Something like


now if Date 1 has more than 100 titles, then it should display 100 records and move next 100 to the next page and like wise with each page showing only 100 records in the descending order.

If Date 1 has less than 100 records then the page should display Date 2 titles in the similar manner and total record count should be 100 per page.

In case if you get a chance, please see this link http://newjersey.craigslist.org/apa/

here apartments are displayed datewise and per page is 100 records.I know you dont have access to cl,but do check whenever you can.Other than this explaination I dont think I can explain this further.

Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
Your data structure unfortunately doesn't lend itself to being paginated efficiently that easily.

eg to show records 500-600 you would need to load all the records from 1 to 600, because you don't have any indication of how many are for each date group without retrieving them first.

I would suggest writing a wrapper around this data structure which provides you a "window" of records to render, and have the logic for which records to include in the "window" in that class.
That makes your JSP then just a simple call to display the current window of records.


General suggestions for your current code:
Use a prepared statement rather than building up your sql via string concatenation. It avoids potential sql injection attacks.

Consider modifying your top level query like this:

It gives you your date breakdown plus also an indication of how many records are on each date.
With that information, you can make your retrieval logic smarter. eg for retrieving records 500-600 you can skip over complete dates without querying their details.
kiran badi
Greenhorn

Joined: Oct 26, 2009
Posts: 26
>>>eg to show records 500-600 you would need to load all the records from 1 to 600, because you don't have any indication of how many are for each date group without retrieving them first.


I was thinking of giving next button rather than giving page numbers to paginate.

>>>I would suggest writing a wrapper around this data structure which provides you a "window" of records to render, and have the logic for which records to include in the "window" in that class.
That makes your JSP then just a simple call to display the current window of records.

This is where my real problem is, for values which are dynamic , I did a seperate query outside map, and then tried iterating the inner map to this value,but unfortunately, I lose the relation between columns.

something like sqla="select count(*) from table where flag="active";

results of this query are in inner map,

using this sqla value, I tried to iterate the inner map,but this is incorrect way of doing it.Let me think of some other way of getting this.But yes datastructures seems to be not meeting the requirement though in servlet out put it does what I need.

>>General suggestions for your current code:
Use a prepared statement rather than building up your sql via string concatenation. It avoids potential sql injection attacks.

Yup I have few sections in my code where I am building sql strings.I will make these changes during review phase for the code which I have already written.Given the number of classess I have , this considerable effort me.but surely I will make these changes.



I tried this code and for each record it seems to showing count as 1,so I believe this code do not fit my requirement,but I understand your point.730 is 2 years of data,and for inner query I am using limit clause.So I think this should limit the number of records I retreive.

and yes now I have started getting feeling that existing data structure is incorrect and I need to use the resultset in the list and then move that list to the view for display and then have Next Button with 100 records per page.atleast this should make my life easy.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSTL Pagination for Map inside Map.