Java Reference
In-Depth Information
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h2>Hello World!</h2>
<table border="1" cellpadding="1" cellspacing="0">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tbody>
<c:forEach var="customer"
items="${sessionScope.customerList}">
<tr>
<td>${customer.firstName}</td>
<td>${customer.lastName}</td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
</html>
Notice at the top of the JSP, the
ArrayList
we attach to the session instances of a
class called
CustomerBean
. This class is a simple JavaBean with two properties,
firstName
and
lastName
. In the body of the
<c:forEach>
tag, we access the getter
methods for these properties by simply entering the name of the property after the
name we gave for the current item in the list (customer). For example, to invoke
the
getFirstName()
getter method in
CustomerBean
, we simply need to type
${customer.firstName}
inside the body of the
<c:forEach>
tag. This notation
allows us to easily invoke getter methods in JavaBeans from JSTL expressions. The
notation can be used with any expression that resolves to a JavaBean, it is not limited
to the body of a
<c:forEach>
tag.