• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSTL and collections

 
Vidar x Langberget
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vidar x Langberget
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64623
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vidar x Langberget
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone?
 
Vidar x Langberget
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic