Page 1 of 1

About Question enthuware.jwpv6.2.1038 :

Posted: Fri Dec 21, 2012 11:47 am
by ETS User
There is a question as following:
Which of the following statements are correct regarding the forEach tag of JSTL?
One of the option which is given as true is:
The EL code within the body of the tag may refer to the iteration variable but scripting code cannot.
The explanation is given as below in answer:

Code: Select all

For example, <%=dept.getName()%> is invalid here:

<c:forEach var="dept" items="${departments}">
  <c:out value="${dept.name}"/><br><%=dept.getName()%>
</c:forEach>
I tried following in JSP:
<% Employee emp = new Employee();
emp.setEmail("abc@gmail.com");
java.util.List<Employee> empList = new java.util.ArrayList();
empList.add(emp);
request.setAttribute("empList", empList); %>

<c:forEach var="emp" items="${empList}" varStatus="status">
Emp email: <%=emp.getEmail()%> <br>
</c:forEach>
I got the output as
Emp email: abc@gmail.com
.

Please correct if am wrong.

Thanks,
Sanjay

Re: About Question enthuware.jwpv6.2.1038 :

Posted: Fri Dec 21, 2012 11:53 am
by admin
ETS User wrote:There is a question as following:
Which of the following statements are correct regarding the forEach tag of JSTL?
One of the option which is given as true is:
The EL code within the body of the tag may refer to the iteration variable but scripting code cannot.
The explanation is given as below in answer:

Code: Select all

For example, <%=dept.getName()%> is invalid here:

<c:forEach var="dept" items="${departments}">
  <c:out value="${dept.name}"/><br><%=dept.getName()%>
</c:forEach>
I tried following in JSP:
<% Employee emp = new Employee();
emp.setEmail("abc@gmail.com");
java.util.List<Employee> empList = new java.util.ArrayList();
empList.add(emp);
request.setAttribute("empList", empList); %>

<c:forEach var="emp" items="${empList}" varStatus="status">
Emp email: <%=emp.getEmail()%> <br>
</c:forEach>
I got the output as
Emp email: abc@gmail.com
.

Please correct if am wrong.

Thanks,
Sanjay
You are not really using the loop variable inside the tag. Try changing the variable name at the first line to something different that the one used in forEach tag.

HTH,
Paul.

Re: About Question enthuware.jwpv6.2.1038 :

Posted: Wed Aug 17, 2016 2:34 pm
by johnlong
Hi

Just to clarify.

Code: Select all

<c:forEach items="${map}" var="varr" >
	<%= pageContext.findAttribute("varr") %>
</c:forEach>
Do we access here with scripting code?

Re: About Question enthuware.jwpv6.2.1038 :

Posted: Wed Aug 17, 2016 9:01 pm
by admin
No, you are accessing pageContext variable here.

Re: About Question enthuware.jwpv6.2.1038 :

Posted: Thu Aug 18, 2016 12:26 am
by johnlong
You mean that we cannot access SCRIPTING variable?

Re: About Question enthuware.jwpv6.2.1038 :

Posted: Thu Aug 18, 2016 8:53 am
by admin
The scripting code i.e. code inside <% and %> cannot access variable dept.

Re: About Question enthuware.jwpv6.2.1038 :

Posted: Thu Aug 18, 2016 4:33 pm
by johnlong
Thanks you Paul for your explanations. Passed exam today.