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

JSTL and collections

Vidar x Langberget
Greenhorn

Joined: May 12, 2003
Posts: 10
Hi!
I have a web application where I use collections for caches and JSTL for the views. Let's say I have a list of book objects stored in a LinkedHashMap. I also have a list of author objects stored in another LinkedHashMap. The book class have a variable called author, and is an int. This is the unique id of the author in the database.
I want to loop through all my books and print the name of the books and the author name. Is it possible with JSTL? Or do I have to get the author map in the JSP and write a scriptlet inside the c:foreach jstl loop and fetch the correct author? Like this:
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
sure you can loop through a selection using JSTL.
For example, I'm using the following code in an app I'm working on:

where the collection is a List placed in the request or session in the servlet that forwards to the JSP.
My List contains all Objects of a class I defined myself, as will yours.
[ January 19, 2004: Message edited by: Jeroen Wenting ]

42
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
Hmm. I think the original poster understands iterating through lists, but is having problems with some of the more complex aspects of the EL.
Try the following:

I haven't had time to set up the data to test this, but I think it should work. let us know how you get on.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Vidar x Langberget
Greenhorn

Joined: May 12, 2003
Posts: 10
Frank: I haven't used that syntax before, but after looking at the spec it seems like it should work. But it doesn't. I get this error message:


Line 46 looks like this:

Any suggestion on what is wrong?
Thanks!
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Change the nested quotation marks to be distinct.

should work, the engine thought that after [" the EL expression ended which it didn't.
Vidar x Langberget
Greenhorn

Joined: May 12, 2003
Posts: 10
Joroen: Thanks, that fixed the error, but no author shows up. I've checked that the authors map is available from the page, and that the books have an existing author listed. I tried to change book.author to book.value.author, but it didn't help either.
Any other suggestions?
Thanks!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

Jeroen and Vidar, if you check the 'Disable smilies in this post.' checkbox at the bottom of the reply form, your tag examples will retain their intended formatting.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Vidar x Langberget
Greenhorn

Joined: May 12, 2003
Posts: 10
Anyone?
Vidar x Langberget
Greenhorn

Joined: May 12, 2003
Posts: 10
Some additional information can be found here:
Java.sun.com forum thread
I've not got it to work yet, and I'm running out of ideas..
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
Aha. Looks like a type clash - you are giving it an int, which is being automatically converted to an Integer, but the key is a String.
Two possibilities:
1. use an Integer as the key:

2. convert the integer to a String before looking it up:

Do either of those work?
Vidar x Langberget
Greenhorn

Joined: May 12, 2003
Posts: 10
Frank: It works!! Thanks a lot for your help! The second method didn't work though.. I read that EL did automatic type conversion, so I thought it understood that if given a string and it needed an integer, it would convert the string.
You can see the result in a couple of days at www.templeoftech.com.
Thanks again!
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
when I checked disable smilies it parsed an unsightly "c: o ut" instead of the expected "cut" the first time around.
Maybe it works now...
 
Don't get me started about those stupid light bulbs.
 
subject: JSTL and collections