Java Reference
In-Depth Information
The value of the Current item of the Iteration text field is the name we wish to use
to refer to the current iteration item inside the body of the tag. In our example, we
chose to name the current item customer .
Although we typically wish to iterate through the whole collection or array, the
JSTL <c:forEach> tag allows us to specify the index of the element in the array or
Collection to begin iterating from, where the first element has an index of 0 . To do
this, we need to check the Fixed Number of Iterations checkbox, and specify the index
of the element we wish to start iterating from as the value of the Begin text field.
Similarly, if we wish to stop iterating at a specific element in the array or collection,
we can specify the index of the element we wish to end iterating by entering its index
as the value of the End text field.
If we don't wish to process every item in the array or collection we are iterating
through, and instead we wish to process every other item, or every three items, and
so on, we can specify this by entering a value for the Step text field. For example, if
we wished to process every other item, we would enter a value of 2 for this field.
After filling out the fields of the Insert JSTL Field window as shown in the above
screenshot, and clicking on OK , the following markup is added to our page:
<c:forEach var="customer" items="{sessionScope.customerList}">
</c:forEach>
We then modify the page, by adding a scriptlet to create the ArrayList we are
iterating through and adding it as a session attribute, plus adding some markup
inside the body of the <c:forEach> tag, as well as before and after the tag.
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.util.ArrayList" %>
<%@page import="com.ensode.nbbook.CustomerBean" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%
ArrayList<CustomerBean> customerList = new
ArrayList<CustomerBean>();
customerList.add(new CustomerBean("David", "Heffelfinger"));
customerList.add(new CustomerBean("Jeff", "Wu"));
customerList.add(new CustomerBean("Jacqueline", "Smith"));
session.setAttribute("customerList", customerList);
%>
<html>
<head>
 
Search WWH ::




Custom Search