Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Writing Out A 2D Array With JSTL

 
Luke Shannon
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following:



I want to end up with a nice looking table with the letter across the top and numbers in the rows below.

But I am not sure of the correct syntax to do this with JSTL. I don't think the forLoop is good for this. Do I need to create a custom tag?

Thanks,

Luke
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65340
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure you can do this with JSTL forEach tags. It'd actually be easier if you had the headers in a separate collection rather than as the first row of the matrix, but that's not too much of an issue.

I'll code up something to get you started and post it in a little bit.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65340
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming JSP 2.0 and that the Object[][] array has been placed in a scoped variable named matrix, something like the following should get you started. You can fill in more HTML attributes and add styles as necessary, but this should build the basic table.



Disclaimer: this was typed into a text area, so hasn't been tested or anything... but it should give you the idea.

Be sure that you understand why and how this works. Ask questions until you do, please.
 
Luke Shannon
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. I didn't think you could do something like matrix[0], this all makes perfect sense.

The one thing that is weird is for the header information I am getting what looks like the addresses of the objects in memory:

org.apache.commons.el.ImplicitObjects$7@134ab9b

This is weird because it works for the rows below. Does this need to be cast somehow?

Thanks,

Luke
 
Luke Shannon
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems that ImplicitObjects$7 has a getValue method. As far as I know I can only call a getter method using JSTL by using ${reference.attributeName}.
ImplicitObjects$7
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65340
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting what looks like the addresses of the objects in memory:


That is the default behavior for a class that doesn't have a toString() override.

What is the type of object that is in that first row?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65340
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't think you could do something like matrix[0],


There's something about that that I find rather "hacky" which is why, if I were structuring this, I would opt to have the headers passed separately from the data rather than embedded within it, but given your struture, gets the job done.
 
Luke Shannon
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my little example I am putting in a "A". I am guessing that evaluates to a string, but so wouldn't "1" (first value of the second row)?
 
Luke Shannon
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right. I think I will seperate headers and data. Should simplify things.

Luke
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65340
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, something weird is going on with the headers... not sure what yet. It becomes moot if you move them out to their own list or array, but I wonder about why they didn't render correctly as they are indeed Strings.
 
Luke Shannon
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm. Something is up with the header code. Here is how I am obtaining it in its own collection now:



Here is how I am outputting it in the JSP:



PreviousData is a reference to a data object, simple setters and getters for the header and data arraylists.

Here header text is still coming out like this:

org.apache.commons.el.ImplicitObjects$7@132e910

I can see from System.out statements the correct header names are being put into the Array. A little confused as too memory references are coming out. Why can't it tell there is a String there?

Luke
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65340
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DOH!

header is a reserved word of the EL which referes to the request headers!

Change 'header' to 'caption' and all should be well.

Doh! Doh! Doh!
 
Luke Shannon
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was it :-)

Thanks for all your help.

Luke
 
Luke Shannon
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, another question on this. Let me know if the topic is swaying away from the scope of this board, I almost started a new thread for it in intermediate java. The solution may be in the JSTL so I stayed here for now.

My problem is I end up with all my data is one long row. Can something be done in the JSTL to write it out so it knows when to start a new row? I started to implement this and realized it was complicated.

I could also create the whole html table in java...but this would be a an ugly hack.

How are other people doing this? I feel like I am over looking the obvious.

Writing out the table:


Getting the data:
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65340
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compare your table HTML and mine. Pay careful attention to the structure of the loops. Where is your nested loop?
[ September 23, 2005: Message edited by: Bear Bibeault ]
 
Luke Shannon
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, got it. Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic