File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Problem filling Select from Database - c:forEach Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Problem filling Select from Database - c:forEach" Watch "Problem filling Select from Database - c:forEach" New topic
Author

Problem filling Select from Database - c:forEach

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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.


GenRocket - Experts at Building Test Data
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60800
    
  65



Since dept is a scoped variable, its reference must be made in an EL context.
[ July 16, 2004: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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

Joined: Jan 10, 2002
Posts: 60800
    
  65

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

Joined: Jul 11, 2001
Posts: 15299
    
    6

Got it working. Thanks for the help Bear.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
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.


42
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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

Joined: Jan 10, 2002
Posts: 60800
    
  65

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

Joined: Jul 11, 2001
Posts: 15299
    
    6

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

Joined: Jan 10, 2002
Posts: 60800
    
  65

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

Joined: Jul 11, 2001
Posts: 15299
    
    6

Well, this time it worked just fine. Strange. oh well, as long as it works. Maybe I had a typo I missed last time..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem filling Select from Database - c:forEach
 
Similar Threads
Populating a drop down list from database
Iterating over Map using JSTL
Populating each row of a Column with combo boxes containing values from database
can the drop down menu remember the last selection?
printing specific value from the list