Friday, 15 November 2013

s:iterator within another s:iterator Struts2Tag

Struts 2 Iterator will iterate over a value. An iterable value can be any of: java.util.Collection, java.util.Iterator.
This example show you how to iterate a list inside another List.

s:iterator Parameters (struts2-core-2.0.11.jar):-

1. id :- id Deprecated Use 'var' instead.
2. status :- instanceof IteratorStatus will be pushed into stack upon each iteration.
3. value :- iteratable source to iterate over, else an the object itself will be put into a newly created List

Sample code :-
package com.javastoreroom.action;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

public class NestedList extends ActionSupport {

  * @author Arun
 private static final long serialVersionUID = 3731797076821680628L;

 private List teachers =  new ArrayList(); 
  private List students = new ArrayList();
        //code here to fetch result 
        //create getter/setter private List students ; inside a Teacher Class.

        //finally set student object and add to students list.
        // After that add students list to Teacher bean object like(Teacher teacher) 

  * students = */codeToSet and return student list*/;
  * teacher.setStudents(students);
  * teachers.add(teacher);

       //getter setter 

teachers list return to view.jsp contains corresponding students.


executing project you can see Student related to teacher (first teacher related to 10 , 2nd relate to 15 all process have gone in this way).
This is just a sample to show how to do it in .