logic:iterate tag in struts
Hello Friends,
Please give me the explanation for the following
I have CompEmployees class and Employee class.
CompEmployees.java as follows
class CompEmoloyees {
ArrayList totalEmps;
public ArrayList getTotalEmps() {
return totalEmps;
}
public ArrayList setTotalEmps(ArrayList totalEmps) {
this.otalEmps = totalEmps;
}
}
Employee.java
class Employee {
int empNo;
String empName;
setters and getters for empNo,empName;
}
My program
Here i have 100 employees of ArrayList named elist;
CompEmployees compEmployees = new CompEmployees();
compEmployees.setTotalEmps(elist);
session.setAttribute("total",compEmployees);
Please tell me how can we print the 100 employees in the browser by using logic iterate tag in the Jsp
# 1
I assume that the ArrayList eList is a list of Employee objects.
First of all you need to put the list in some scope. Either request or session.
Your code in the Action class will be
request.setAttribute("total",eList);
==========================================================
In the JSP you will need to write the following:
<logic:present name="total" >
<logic:iterate id="idEmp" name="total" >
<tr align="center">
<td>
<bean:write name="idEmp" property="empNo"/>
</td>
<td>
<bean:write name="idEmp" property="empName"/>
</td>
</tr>
</logic:iterate>
</logic:present>
===========================================================
# 2
Hi,
Thanks for ur reply.
if my CompEmployees class have
class CompEmoloyees {
ArrayList totalEmps;
ArrayList FemaleEmps;
public ArrayList getTotalEmps() {
return totalEmps;
}
public ArrayList setTotalEmps(ArrayList totalEmps) {
this.otalEmps = totalEmps;
public ArrayList getFemaleEmps() {
return femaleEmps;
}
public ArrayList setFemaleEmps(ArrayList totalEmps) {
this.femaleEmps = femaleEmps;
}
}
Here i have 100 employees of ArrayList named elist for total employees
i have 50 employees of ArrayList named elist named flist for female employees
CompEmployees compEmployees = new CompEmployees();
compEmployees.setTotalEmps(elist);
compEmployees.setFemaleEmps(flist);
session.setAttribute("total",compEmployees);
I want to print the Total employees only not for female employees.
Can u explain How it evaluvates................
Thanks
Mallik
# 3
Now now.......
How about having some java programmimg done?
your female list is the subset of the total list.
In the MVC framework we have the business logic written in the java class. You need to seperate the female list from the total list and then put it into some scope and then print it on the JSP.
One more thing: you have written the line below in your code.
session.setAttribute("total",compEmployees);
May I ask why have you done it? If it is related to printing of the list then it is of no use.
# 4
>May I ask why have you done it?
>If it is related to printing of the list then it is of no use.
But it IS of use. The objects compEmployees is in scope.
It has the list we want to print out.
With logic:iterate:
<table>
<tr>
<th>Number</th>
<th>Employee</th>
</tr>
<logic:iterate name="compEmployees" property="totalEmps" id="emp">
<tr>
<td>
<bean:write name="emp" property="empNo"/>
</td>
<td>
<bean:write name="emp" property="empName"/>
</td>
</tr>
</logic:iterate>
</table>
or alternatively with JSTL and c:forEach
<table>
<tr>
<th>Number</th>
<th>Employee</th>
</tr>
<c:forEach items="${compEmployees.totalEmps}" var="emp">
<tr>
<td>
<c:out value="${emp.empNo}"/>
</td>
<td>
<c:out value="${emp.empName}"/>
</td>
</tr>
</c:forEach>
</table>
Cheers,
evnafets