Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

JSTL - <c:forEach> Syntax explanation

 
Greenhorn
Posts: 5
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good Day!
Could someone please explain me why we can't access 'var" as the following?

<c:forEach begin ="1" end ="5" var="counter">
<%= counter%>
</c:forEach>

why is var not accessible just like a normal scripting element?

I know we can access the"counter" as the following.
<%=((Integer)(pageContext.findAttribute("counter"))).intValue()%>

Thanks & Regards
 
Sheriff
Posts: 67620
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lekamge Perera wrote:Could someone please explain me why we can't access 'var" as the following?


Because you are trying to mix the JSTL with scriptlets. No can do. The JSTL and EL are meant to replace scriptlets, not work in concert with them.

The correct syntax would be: ${counter}
 
Lekamge Perera
Greenhorn
Posts: 5
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bear for your reply. I'm not convinced not enough though. Really appreciate if you could answer my following questions.
1. Has <c:forEach> implemented as a "Classic Tag" ?

2. If so, Shouldn't it allow using <%= counter%> in my example ?

3. We know that we can use scripting in the body as I've shown in the second part of the question. But we can't use "<%= counter%>". Is it because JSTL tags don't provide Variable Synchronization?

4. Do JSP standard actions provide Variable Synchronization. Don't they?

Thanks and Regards

 
Bear Bibeault
Sheriff
Posts: 67620
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lekamge Perera wrote:I'm not convinced not enough though.


You can choose to take the advice of the senior members, or you can ignore it. The choice is yours.

1. Has <c:forEach> implemented as a "Classic Tag" ?


Immaterial. The implementation is not specified by the Specification.

2. If so, Shouldn't it allow using <%= counter%> in my example ?


No.

3. We know that we can use scripting in the body as I've shown in the second part of the question. But we can't use "<%= counter%>". Is it because JSTL tags don't provide Variable Synchronization?


Scriptlets and JSTL do not mix well. They were not designed to work together, and never will. Using scriptlets in 2011 is simply irresponsible at this point. They have been discredited for almost ten years now. Time to move on.

4. Do JSP standard actions provide Variable Synchronization. Don't they?


I don't know what you mean by that, but it doesn't really matter. Scriptlets should no longer be used except in legacy code where they already exist. No new JSP code should be written using scriptlets.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic