Struts logic:Iterate Tag with example code. Iterating ArrayList in JSP using Struts

Iterate tag is used to repeat the nested body content ( <logic:iterate…> body content </logic:iterate> ) once for each element of the collection. The collection can be an

1. Array of Java objects or primitives (eg. int, long , etc..)

2. Implementation of java.util.Collection (ArrayList, Vector , etc..)

3.Implementation of java.util.Enumeration

4. Implementation of java.util.Iterator

5. Implementation of java.util.Map ( including HashMap, Hashtable, and TreeMap)

Some of the attributes used in the logic:iterate tag

1. collection : Runtime expression (Scriptlet) that evaluates to a collection
2. id : Specifies the name of a page scope JSP bean that will have a reference to the current element of the collection on each iteration
3. name : Specifies the name of the object that contains the collection to be iterated . If property attribute is specified one of the fields of the object will have its getter method called to return the collection to be iterated
4. property : Specifies the property of the object specified by the name attribute , whose getter method will be called to return the collection to be iterated

You can use iterate tag in three basic ways

1. The Ist way uses collection attribute


<logic:iterate id="cityname" collection="<%=city_List%>" >
City Name : <%=cityname%> </br>
</logic:iterate>

Before using the above, we have to create a city_List object in the JSP. Sample code is below.


<jsp:useBean id="empBean" class="test.techlabs.actions.EmpDAO" scope="request" />
<%
ArrayList city_List= new ArrayList();
city_List=empBean.loadCityList();
%>

where loadCityList() is a method defined in EmpDAO class which loads the city names  in a array list and returns a arraylist.

2. In the IInd way , name attribute is used to specify the name of a collection object which can be in any scope .

You can use scope attribute to specify the scope of the collection object.


<logic:iterate id="cityname" name="city_List" >
City Name : <bean:write name="cityname"/> </br>
</logic:iterate>

For using the above way , you have to write the following code in Action


ArrayList cityList=new ArrayList();
EmpDAO empDao = new EmpDAO();
cityList = empDao.loadCityList();
request.setAttribute("city_List", cityList);

-> where loadCityList() is a method defined in EmpDAO class

3. In the third way , name and property attributes are used to specify the name of a object and its field whose getter method will be called to return a collection to be iterated. (i.e field is an arraylist property defined in the form whose getter & setter method is also defined )


<logic:iterate id="designation" name="empForm" property="desig_List">
Desig. Code : <bean:write name="designation" property="value"/> ,
Designation : <bean:write name="designation" property="label"/> </br>
</logic:iterate>

Same code written in the Action are as follows


EmpDAO empDao = new EmpDAO();
desigList = empDao.loadDesigList();
empForm.setDesig_List(desigList);

->where loadDesigList() is a method defined in EmpDAO class which loads the designation & designation code in a arraylist and returns an array list contains  objects.

in EmpForm:


private List desig_List=new ArrayList();
public List getDesig_List()

{

return desig_List;

}
public void setDesig_List(List list)

{

desig_List = list;

}

Note : The following code will produce the same output which is produced by the 3 rd way


<logic:iterate id="designation" name="desig_List" >
Desig. Code : <bean:write name="designation" property="value"/> ,
Designation : <bean:write name="designation" property="label"/> </br>
</logic:iterate>

Here no need to declare the form variable and getter setter method in empForm

Sample code to be written in action


ArrayList desigList=new ArrayList();
EmpDAO empDao = new EmpDAO();
desigList = empDao.loadDesigList();
request.setAttribute("desig_List", desigList);

Same code written in EmpDAO class to load cityList & Designation List

to Load City List:


....
.....
strQuery =" select city_name from city ";
pstmt = conn.prepareStatement(strQuery);
rs = pstmt.executeQuery();
while (rs.next()) { cityList.add(rs.getString("city_name")); }
...
...
return cityList;

To load Designation List:


import org.apache.struts.util.LabelValueBean;
.....
.....
strQuery =" select desig_code, designation from Desig ";
pstmt = conn.prepareStatement(strQuery);
rs = pstmt.executeQuery();
while (rs.next()) {
desigList.add( new LabelValueBean( rs.getString("Designation"), rs.getString("Desig_code"))); }
.....
......
return desigList;

 

You may also like

Leave a Reply