Forums Register Login

JSTL c:forEach tag iteration variable

+Pie Number of slices to send: Send
Hi all,

I was trying out the following code.




here is the test.jsp:



and the code inside the servlet



Now I am getting a compiler error at line 06 of test.jsp i.e
<%=dept.getName()%>

why cant the scripting code access the iteration variable "dept"
whereas EL can access the iteration variable in the body of the tag?

Thanks

+Pie Number of slices to send: Send
Scripting variables and scoped variables are two separate things. They were not designed to work together, and will not easily work together.

Use one or the other; mixing them is just problematic and will make a big mess.
+Pie Number of slices to send: Send
Thanks for the reply...

do you mean that scripting code should not be used at all inside the body of jstl tag?
did i miss some basic point here?
+Pie Number of slices to send: Send
Ignoring the certification exam (which I know nothing about), scripting shouldn't be used anywhere in a JSP page. It's over 8 years out of date and discredited.

That said, I repeat: scripting has no direct access to scoped variables. You should use JSTL and EL exclusively. If you are going to use poor-practice scriptlets (not recommended), then use those exclusively. Mixing scriptlets with JSTL/EL is just a big old mess.
+Pie Number of slices to send: Send
 

do you mean that scripting code should not be used at all inside the body of jstl tag?
did i miss some basic point here?


Not using scripting code at all is a good point, but that's not what Bear was telling you here. The variable "dept" used by the c:forEach tag is a scoped variable. It is accessible via EL ${dept}. The "dept" variable you are using in the scriptlet <%=dept.getName()%> is not a scoped variable, but a scripting variable.

Check how your JSP is being translated into a servlet, and you'll understand why they are not the same, and why a compilation error will happen.
+Pie Number of slices to send: Send

checked the generated servlet code..got that clear

Thanks guys...
The human mind is a dangerous plaything. This tiny ad is pretty safe:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2458 times.
Similar Threads
pagination exception in spring
value from servlet to dropdown in jsp
EL Mystery
HashSet unique values
Bean java.lang.NullPointerException
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 17:16:31.