JSTL c:forEach Core Tag

The JSTL <c:forEach> Core Tag is used when a block of statements is executed again and again. It works same as for loop in java.

Syntax:

<c:forEach var="counterVar" begin="startValue" end="endValue">
    //Block of statements
</c:forEach>

c:forEach tag attributes:

AttributeDescriptionRequired
itemsInformation to loop over.No
beginElement to start with (0 = first item, 1 = second item, …).No
endElement to end with (0 = first item, 1 = second item, …).No
stepProcess every step items.No
varName of the variable to expose the current item.No
varStatusName of the variable to expose the loop status.No

Example:

test.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
 
<html>
	<head>
		<title>c:forEach JSTL core tag example</title>
	</head>
	<body>
		Table of 2. <br/>
		<c:forEach var="i" begin="1" end="10">
   			<c:out value="${i*2}"/><p>
		</c:forEach>
	</body>
</html>

web.xml

<web-app>
 
  <welcome-file-list>
          <welcome-file>test.jsp</welcome-file>
  </welcome-file-list>	
 
</web-app>

Output:

jsp example 38
 
Download this example.
 
Next Topic: JSTL c:forTokens Core Tag with example.
Previous Topic: JSTL c:import Core Tag with example.

 

Content Protection by DMCA.com
Please Share