About Question enthuware.jwpv6.2.1038 :

Moderator: admin

Post Reply
ETS User

About Question enthuware.jwpv6.2.1038 :

Post 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

Online
admin
Site Admin
Posts: 10034
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.jwpv6.2.1038 :

Post 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.
If you like our products and services, please help us by posting your review here.

johnlong
Posts: 197
Joined: Mon Jun 20, 2016 5:06 pm
Contact:

Re: About Question enthuware.jwpv6.2.1038 :

Post 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?

Online
admin
Site Admin
Posts: 10034
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.jwpv6.2.1038 :

Post by admin »

No, you are accessing pageContext variable here.
If you like our products and services, please help us by posting your review here.

johnlong
Posts: 197
Joined: Mon Jun 20, 2016 5:06 pm
Contact:

Re: About Question enthuware.jwpv6.2.1038 :

Post by johnlong »

You mean that we cannot access SCRIPTING variable?

Online
admin
Site Admin
Posts: 10034
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.jwpv6.2.1038 :

Post by admin »

The scripting code i.e. code inside <% and %> cannot access variable dept.
If you like our products and services, please help us by posting your review here.

johnlong
Posts: 197
Joined: Mon Jun 20, 2016 5:06 pm
Contact:

Re: About Question enthuware.jwpv6.2.1038 :

Post by johnlong »

Thanks you Paul for your explanations. Passed exam today.

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests