aspose file tools*
The moose likes JSP and the fly likes Iterating through local List using JSTL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Iterating through local List using JSTL " Watch "Iterating through local List using JSTL " New topic
Author

Iterating through local List using JSTL

Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 759
Hi i have once jstl in which i have created java.util.List and adding three element.

And now when i am trying to iterate through the list using jstl forEach loop, i am not able to retrive any element.

Bellow is the complete code .jsp file, Help me out solving this issue.


Regards,


Jigar Naik


Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61598
    
  67

At what point do you create a scoped variable named list? (Answer: you don't.)


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 759
line no 17 is not required anymore i just added it for testing whether the EL is working fine or not






Line 25 in items i m getting the list which is of type java.util.List items="${list }">

and assisgnig it to variable listItems for iteration var="listItems"



saumil baxi
Ranch Hand

Joined: Apr 18, 2008
Posts: 58
Just change this part of your code
Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 759
yeah i have changed it

new code



and the bellow is the output in browser




Welcome to JSTL

--------------------------------------------------------------------------------
List Size : 3
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
And take a look at the For loop (JSTL).
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
What is the scope List list?
Where it will look for list?
Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 759
list is an local variable i am just creating it before iterating

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61598
    
  67

Why would you expect the JSTL/EL, which are meant to replace scriptlets to know anything about scripting variables?

It doesn't.

You need to create a scoped variable for the EL to be able to access the data.

I assumed that the scriptlet code was in this JSP for testing purposes only. You wouldn't really put scriptlets in a JSP, would you?
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Jigar, you don't see the desired output because list is out of scope for the loop.
Try putting List list into request and see what happens?

And take in account Bear's advice. Don't use Scriplets.
Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 759
yup i won't use scriptlets anymore.. but it worked with request scope... i was trying an example...
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Jigar Naik wrote:yup i won't use scriptlets anymore.. but it worked with request scope... i was trying an example...

Shashank Rudra
Ranch Hand

Joined: Mar 26, 2009
Posts: 131
Naik
yup i won't use scriptlets anymore.. but it worked with request scope... i was trying an example..

Here you have used the scope to access/iterate the List - would you please post the code you used.

I am trying to set it using (I dun know how to do it using JSTL tags )and then trying to access it on the same page as well as some other forwarded page(on submit of a form)But that is not helping. And I am getting this error -the mysterious and cryptic error. Thanks in advance.


Programmer Analyst || J2EE web development/design
Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 759



Shashank Rudra
Ranch Hand

Joined: Mar 26, 2009
Posts: 131
Thanks Naik for the quick response. But still I am helpless. here are the jsps that I am trying to test this on

The first JSPHere I am setting the request parameter and then submitting to the testList.jsp where I am trying to access the List entries.
testList.jspBut this is getting me this error Please tell me where I am going haywire. TIA
Deepali Anand
Greenhorn

Joined: May 12, 2009
Posts: 12
Giffy Geraldo wrote:Thanks Naik for the quick response. But still I am helpless. here are the jsps that I am trying to test this on

The first JSPHere I am setting the request parameter and then submitting to the testList.jsp where I am trying to access the List entries.
testList.jsp


Hello,

You have used ${params} ...
It should be ${param}

Hope, this solves your problem.

-Deepali

SCJP 1.5, SCWCD 5
Shashank Rudra
Ranch Hand

Joined: Mar 26, 2009
Posts: 131
Thanks Deepali. That worked. Actually I reposted it with slightly different iteration as other thread
http://www.coderanch.com/t/450213/JSP/java/using-JSTL-EL-get-set
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Iterating through local List using JSTL