• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Why can not refer to iteration variable in c:forEach with a script?

 
Ranch Hand
Posts: 101
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
var is a static attribute to expose the current element (local to the body)
How to acces to var attribute through script?


forEach code 1 Error

NOR
forEach code 2 Error
 
Creator of Enthuware JWS+ V6
Posts: 3346
303
Android Eclipse IDE Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joey,

First of all: never mix scriptlets and EL expressions. Actually don't use scriptlets at all.

How to acces to var attribute through script?


You can't. The var attribute is an instance variable for the Tag handler code of the <c:forEach> tag.

Watch out: the other way around should also not be done (but is possible)
EL cannot access variables declared in a scriptlet, unless that variable is put in the pageContext as an attribute, this won't work:

Regards,
Frits
 
Joey Sanchez
Ranch Hand
Posts: 101
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Frits.

I've tried your phones example and it compiles but it doesn't show anything. Could it be because is null?.
(I changed ${phones} for <c:out value="${phone}" /> to print the phones).

And I tried again, after an attribute was set in pageContext, the snippet printed mobile fixed

The question is theoretical because I found it in Enthuware tests.

It finally fix the problem I had. It were the jstl libraries, differents versions in same project give me this error after every change. I got java.lang.NoSuchFieldError: deferredExpression because of this.
But now it works well like this. It finds the attribute in all scopes.



I am not sure if it is the same, because we are working with integers, but in "OCJEWCD Study Companion" book, there is an example of an alternative to EL and it also works:



Alternative with JSP scripting


Best regards,
Joey
 
Frits Walraven
Creator of Enthuware JWS+ V6
Posts: 3346
303
Android Eclipse IDE Chrome
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Joey Sanchez wrote:Thanks a lot Frits.

I've tried your phones example and it compiles but it doesn't show anything. Could it be because is null?.
(I changed ${phones} for <c:out value="${phone}" /> to print the phones).


Yes that is because it is null (EL is null friendly)

But now it works well like this. It finds the attribute in all scopes.


Oh great, well done and good that you proved me wrong here! The var variable was of course possible to reach (I was :sleep: )

The JSTL documentation documentation says it clearly "Name of the exported scoped variable for the current item of the iteration. This scoped variable has nested visibility." and nested means from the start tag <c:forEach> until the end tag </c:forEach>.

Regards,
Frits
 
Joey Sanchez
Ranch Hand
Posts: 101
Netbeans IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your always easy to understand answers!

They are a huge help for us.

 
Now I am super curious what sports would be like if we allowed drugs and tiny ads.
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic