• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Iterating through local List using JSTL

 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At what point do you create a scoped variable named list? (Answer: you don't.)
 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just change this part of your code
 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And take a look at the For loop (JSTL).
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the scope List list?
Where it will look for list?
 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
list is an local variable i am just creating it before iterating

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yup i won't use scriptlets anymore.. but it worked with request scope... i was trying an example...
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



 
Shashank Rudra
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Shashank Rudra
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic