13 December 2008

How to iterate over a collection using struts logic taglib

Among struts Tag libararies there's a taglib called logic, it aims to do logical operation especially values and strings comparison using its tags such as Equa, NotEqaul, present, notPresent, greaterThan and more.

Among theses tags, there is a very handy tag that used to iterate over collection, it remembers me with the JSTL forEach tag, but it provides more funcationality.

We can use this tag to display the model data as it appears in the next view in very easy-to-use way :

lets illustrate it, assume we have an abject called EmployeeBO that wrap and Employee object of four attributes : ssn, firstname, lastname and age.

The EmployeeBO object contains a method called getAllEmployees that returns an array list of employees objects.The following code is the code of the action class, it inserts the result of "getAllEmployees" in the session to get it back again in the view from the session: