• Post Reply Bookmark Topic Watch Topic
  • New Topic

JSTL - <c:forEach> Syntax explanation

 
Lekamge Perera
Greenhorn
Posts: 5
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • 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
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!