aspose file tools*
The moose likes JSP and the fly likes How to build a table with nested foreach loops Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How to build a table with nested foreach loops" Watch "How to build a table with nested foreach loops" New topic
Author

How to build a table with nested foreach loops

J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 1001
    
  13

I've struggled with this all day but just can't hit the right solution. I need a jsp page that will display query results in a table. But I won't know the column names until runtime so I can't do the typical loop like this:

So what I want to do is pass another collection (List, Array, whatever will work) of the column names so I can nest two foreach loops something like this:

This example doesn't work of course, line 4 is where it all falls apart with "property 'column' not found", but I've tried several syntactical variations of EL and just can't hit the mark. This comes the closest:

..but the resulting td's contain strings like "com.gy.dan.jkr.obj.PDP00003Bean@16ae6b9.bbdate8" where bbdate8 is the column name.

Will someone please point me in the right direction so I don't dream about this problem all weekend?


"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." -- Ted Nelson
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61420
    
  67

You omitted the most important piece of information: what's the format of the data?

If you have a choice, best case is to simply make it an Object[][] -- then you don't need to worry about column names.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 1001
    
  13

Sorry, the rows are an ArrayList of beans, and the column names are an ArrayList of Strings. I tried the column names as a simple array but with no better results.

I've never tried managing query results as a 2D array, but I'll give it a shot. It's typically my habit to have a DAO create a bean for each record and add them to either a List or a Map, depending on the requirements. I'm not sure how to convert a resultset to a 2D object but I'll see what I can do. It never hurts to know more than one way to skin a cat, right?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61420
    
  67

If you want to use beans, with column names as the properties, the accessor syntax would be: ${bean[columnName]} where bean is the bean reference and columnName is a variable containing the a name of the column/property.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18656
    
    8

Jk Robbins wrote:Sorry, the rows are an ArrayList of beans, and the column names are an ArrayList of Strings.

And is there a relationship between those beans and those column names? A small example would be helpful. If you want a guess I might suggest

but chances are that isn't quite right.

Edit: Now that I see Bear has posted a similar guess, chances are better that it's right.
J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 1001
    
  13

The beans don't contain the column names, just the record data, but maybe that's what I need to change. Here's the DAO code.

For testing the column names are hard-coded. This will be changed to get column count and column name from the result set meta data. This lives in the DAO.

The servlet just gathers these two lists and sets them as request attributes.

The beans are like this:



So it sounds like an ArrayList of the column names needs to added as a bean property? That means every bean will have the same property with the same value(list of column names). That's why I tried to generate just one separate list instead of storing it in every bean, but I can't work out the EL to append the column name variable to the row variable. I'll have to think about this from a different angle. Looks like I'll be dreaming in code this weekend after all....
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61420
    
  67

It looks as if the bean properties are the column names. But even if they weren't, this is easy.

Have a List<String> or (string array) of the property names in the order you want then in the rows.

Your outer loop will iterate over the list of rows creating the <tr> elements. The inner loop will iterate over the property name list and generate the <td> elements, using the syntax I described above for the <td>'s data.

Piece of cake.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to build a table with nested foreach loops