1.

Explain Jstl Foreach Tag With An Example?

Answer»

JSTL forEach TAG: This tag provides a mechanism for iteration within a JSP page. JSTL forEach tag works similarly to enhanced for loop of Java Technology. You can use this tag to iterate over an existing collection of items. For Example :

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="C" %>

<HTML>
<head>
<title>Tag Example</title>
</head>
<body>
<c:forEach var="message" items="${errorMsgs}" >
<LI>${message}</li>
</c:forEach>
</body>
</html>

Here the attribute items has its value as an EL expression which is a collection of ERROR messages. Each item in the iteration will be stored in a variable called message which will be available in the body of the forEach tag.

JSTL forEach tag: This tag provides a mechanism for iteration within a JSP page. JSTL forEach tag works similarly to enhanced for loop of Java Technology. You can use this tag to iterate over an existing collection of items. For Example :

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<html>
<head>
<title>Tag Example</title>
</head>
<body>
<c:forEach var="message" items="${errorMsgs}" >
<li>${message}</li>
</c:forEach>
</body>
</html>

Here the attribute items has its value as an EL expression which is a collection of error messages. Each item in the iteration will be stored in a variable called message which will be available in the body of the forEach tag.



Discussion

No Comment Found