Struts2 - iterator tag

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abehl
    New Member
    • Sep 2008
    • 9

    Struts2 - iterator tag

    Hi I had a question about struts2 iterator tag.

    Suppose I had an array of the form

    Code:
    private int [][]transactions = new int[11][this.size()];
    and had getters and setters for this.

    Now using iterator tag I need to display this

    Code:
    Hour     Mac1 Xact     Mac2 Xact .... MacX Xact  
    00:00        5             3     ....     2
    01:00        6             5              1
    "hourList" is a list of strings under the Hour header of the form 00:00, 01:00 ....12:00

    and the data for the rest is in the 2D array.

    Can some one please help me with this.
    Last edited by Nepomuk; Sep 10 '08, 09:45 AM. Reason: Table wasn't being shown properly
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Originally posted by abehl
    ...Can some one please help me with this.
    Of course we can help. You'll want to use a for loop for the lines and inside that, a nested for loop for the entries. For example, if you had a simple 2d array [code=java]int[][] array = new int[4][6];[/code] you walk through all of it's data like this: [code=java]for(int i=0; i<array.length( ); i++) {
    for(int j=0; j<array[0].length(); j++) {
    // Do something with array[i][j]
    }
    }[/code] Now, try to adjust that for your needs.

    Greetings,
    Nepomuk

    Comment

    • abehl
      New Member
      • Sep 2008
      • 9

      #3
      But I need to use struts2 iterator tag.

      [code]
      <s:iterator ....>
      ---
      ---
      </s:iterator>
      [,/code]

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        Originally posted by abehl
        But I need to use struts2 iterator tag.

        Code:
        <s:iterator ....>
        ---
        ---
        </s:iterator>
        Ah, sorry. I missed that part.

        OK, in that case check the Using iterator Tag Howto on struts2.org. Then try solving the problem and if you get stuck, post your code here and we'll help you.

        Greetings,
        Nepomuk

        Comment

        • abehl
          New Member
          • Sep 2008
          • 9

          #5
          Ok am really struck here

          Code:
          	private void dataReady(){
          		for (int itr = 0; itr < this.HOURS; itr++) {
          			tempList = new ArrayList();
          			// Add all the till related data
          			for(int tillItr = 0; tillItr < this.terminalList.size(); tillItr++){				
          				tempList.add(this.transactions[itr][tillItr]);
          				tempList.add(this.unitsSold[itr][tillItr]);				
          			}
          			// add all overall data
          			tempList.add(this.peakBlocks[itr]);
          			tempList.add(this.incCtr[itr]);
          			tempList.add(this.percentages[itr]);
          			/*
          			 * Now to place this list in the list which will
          			 * render it on the screen via JSP 
          			 */
          			this.dataList.add(tempList);
          		}
          	}
          I use this code to get an arrayList of primitive types.
          (Each element added to the list is a int or a long)

          Ok first in the loop I make a list with all primitive types called "tempList" then I place this entire list in another list.

          Now I need to use the struts2 iterator to iterate over this list.

          Can anyone please help me with this.

          Comment

          Working...