Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem filling Select from Database - c:forEach

 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a servlet a fill an ArrayList with a bunch of departments from a database. So now I need to put those items in a Select List. I know I need to use the c:forEach of JSTL but for some reason I am not getting it right. I don't get errors, I just get an Empty list. I put the ArrayList in the Request and then I forward that request to a JSP page. The name of the ArrayList in the Request is dept

This is what I have between my <select></select>


So since I am appearntly doing something wrong here, what is it?

Thanks.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64990
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Since dept is a scoped variable, its reference must be made in an EL context.
[ July 16, 2004: Message edited by: Bear Bibeault ]
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:


Since dept is a scoped variable, its reference must be made in an EL context.

[ July 16, 2004: Message edited by: Bear Bibeault ]


I thought about that. But wasn't sure how to approach it. It seems overkill to create some Bean to hold a single value and then create an ArrayList full of these beans just to get the department name.

Can I just access it in the context of the Request? Something like...



I'm not sure that is the correct syntax, I'll need to look up again how to explicitly call the Request from EL...
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64990
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it seems overkill to create some Bean to hold a single value and then create an ArrayList full of these beans


For a single value, there's no need for a bean. Why not just put a String array up?

To directly reference an attribute in request scope:

 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it working. Thanks for the help Bear.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
using "${requestScope.whatever}" is actually superfluous unless there's a potential naming conflict with another attribute called whatever elsewhere in the pageContext (for example in the session).
You can use just "${whatever}" usually which is shorter.
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeroen Wenting:
using "${requestScope.whatever}" is actually superfluous unless there's a potential naming conflict with another attribute called whatever elsewhere in the pageContext (for example in the session).
You can use just "${whatever}" usually which is shorter.


Using ${whatever} didn't work in the JSTL code. It only worked after I used ${requestScope.whatever}.

However, elsewhere on the page, when I access variables not using JSTL tags, ${whatever} works just fine.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64990
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It only worked after I used ${requestScope.whatever}.

Hmm, that's a bit odd since, when you don't specify a particular scope, the variable is searched for in page, request, session and application scopes respectively.
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:

Hmm, that's a bit odd since, when you don't specify a particular scope, the variable is searched for in page, request, session and application scopes respectively.


That's what I thought too, but...I'll change my code when I get to work this morning and run it again. I'll let you know what happened.

On that note, though...Would it not be better to specify the scope for performance reasons? That way, it doesn't have to search through, Context, Page, Request, Session...
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64990
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The performance overhead of searching through the contexts is pretty marginal so I wouldn't be too concerned. I'd choose to use the scope specifiers based upon whether it made the code more readable or not, or to resolve ambiguities as Jeroen pointed out.
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, this time it worked just fine. Strange. oh well, as long as it works. Maybe I had a typo I missed last time..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic