JSTL Core c:forEach Tag with select example

The JavaServer Pages Standard Tag Library (JSTL) encapsulates core functionality common to many JSP applications. Instead of mixing tags from numerous vendors in your JSP applications, JSTL allows you to employ a single, standard set of tags. This standardization allows you to deploy your applications on any JSP container supporting JSTL and makes it more likely that the implementation of the tags is optimized.

Below is the syntax to include JSTL Core library in your JSP for JSTL Core c:forEach Tag with select example:

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

JSTL Core c:forEach Tag with select example: The basic iteration tag, accepting many different collection types and supporting subsetting and other functionality.The syntax and attributes are as follows with example and ran on server to show you output:

<%@page import="java.util.ArrayList"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>JSTL Core c:forEach Tag with select example</title>
</head>
<body>
<% 
ArrayList<String> arrayList=new ArrayList<String>();
arrayList.add("First");
arrayList.add("Second");
arrayList.add("Third");
arrayList.add("Fourth");
arrayList.add("Fifth");
request.setAttribute("arrayList",arrayList);

%>
<h2> c:foreach select value logic 1: 

<select id="names" name="names">
   <c:forEach items="<%=request.getAttribute(\"arrayList\") %>" var="names">
        <option value="<c:out value="${names}"/>"><c:out value="${names}"/></option>
    </c:forEach>
</select>

<br><br>c:foreach select value logic 2: 

<select id="names" name="names">
   <c:forEach items="${arrayList }" var="names">
        <option value="<c:out value="${names}"/>"><c:out value="${names}"/></option>
    </c:forEach>
</select></h2>

</body>
</html>

 

JSTL Core c:forEach Tag with select example Output:

JSTL Core c:forEach Tag with select example

Leave a Reply

Your email address will not be published. Required fields are marked *