| Refresh | Home EGTry.com

use list of object in forEach loop construction


jstl

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@page import="com.egtry.SimpleObject" %>
<%@page import="java.util.List,java.util.ArrayList" %>

<h3>use List of Object</h3>
<%
List<SimpleObject> list1=new ArrayList<SimpleObject>();
 list1.add(new SimpleObject("Mon"));
 list1.add(new SimpleObject("Tue"));
 list1.add(new SimpleObject("Wed"));
 pageContext.setAttribute("weekday", list1, PageContext.PAGE_SCOPE);
%>


<c:forEach var="day" items="${pageScope.weekday}" varStatus="seq">
${seq.count}: ${day.name}  <br/>
</c:forEach>








Output:

use List of Object

1: Mon
2: Tue
3: Wed

SimpleObject.java

package com.egtry;

public class SimpleObject {
	private String name;
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public SimpleObject(String name) {
		this.name=name;
	}
}