• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem filling Select from Database - c:forEach

 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it working. Thanks for the help Bear.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
reply
    Bookmark Topic Watch Topic
  • New Topic