*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Why can not refer to iteration variable in c:forEach with a script? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Why can not refer to iteration variable in c:forEach with a script?" Watch "Why can not refer to iteration variable in c:forEach with a script?" New topic
Author

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

Joey Sanchez
Ranch Hand

Joined: Jun 23, 2011
Posts: 86

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
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1632
    
  23

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

Joined: Jun 23, 2011
Posts: 86

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
Bartender

Joined: Apr 07, 2010
Posts: 1632
    
  23

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

Joined: Jun 23, 2011
Posts: 86

Thank you for your always easy to understand answers!

They are a huge help for us.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why can not refer to iteration variable in c:forEach with a script?
 
Similar Threads
According to TLD or attribute directive in tag file, attribute items does not accept any expressions
Getting error while deploying Paging folder in tomcat 4.1
Using EL to retrieve bean values
JSTL arraylist of strings
access custom bean in EL