File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Joey Sanchez
Ranch Hand
Posts: 88
Eclipse IDE Java Linux
  • 0
  • 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
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2167
57
Android Chrome Eclipse IDE
  • 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: 88
Eclipse IDE Java Linux
  • 0
  • 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
Saloon Keeper
Pie
Posts: 2167
57
Android Chrome Eclipse IDE
  • 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: 88
Eclipse IDE Java Linux
  • 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.

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic