The MultiForEach Tag

The "multiForEach" tag is a looping tag, like the forEach tag, but it can operate on multiple Collections simultaneously. This tag must contain a body. The tag's block of cells is copied once for each collection element found in the largest of the collections. If a Collection is exhausted before another Collection, then "pastEndAction rules" apply. If only one Collection is specified, then the behavior is equivalent to a forEach tag.

Attributes

  • The "multiForEach" tag supports all base tag attributes.
  • The "multiForEach" tag supports all looping tag attributes.
  • collections: Collections is a semicolon-separated list of Expressions that specify all Collections to be used.
  • vars: String Required. This is a semicolon-separated list of looping variable names to be exposed in the beans map, one for each Collection in the "collections" attribute.
  • limit: int Optional. This specifies a limit to the number of Collection items displayed. If the collection size is less than the limit, then the block of cells is copied for the extra non-existent items, but the cells are left blank. For example, if the Collection has 7 items, and the limit is 10, then 10 rows are produced, one for each of the 7 items, and 3 additional blank rows.
  • indexVar: String Optional. This specifies an integer "counter" variable name to be exposed in the beans map. This variable starts at index zero.

Example

There are 2 Managers and there are 3 Departments. Notice the blank cells below the end of the display for the managers.

Manager First Name Manager Last Name Manager Salary Department ID Department Name Department Floor
<jt:multiForEach collections="${managers};${departments}" vars="mgr;dept">${mgr.firstName} ${mgr.lastName} ${mgr.salary} ${dept.id} ${dept.name} ${dept.floor}</jt:multiForEach>

...gets transformed into...

Manager First Name Manager Last Name Manager Salary Department ID Department Name Department Floor
Robert Stack $1000.00 101 Application Development 5
Bugs Bunny $1500.00 102 Human Resources 4
      103 Quality Assurance 5

PastEndAction Example

Here's an example that uses the "pastEndAction" attribute. Notice how the "remove" value removes all formatting, including the merged region, past the end of the managers collection. If the "pastEndAction" attribute was not specified, or it was specified as the default value "clear", then the cells would be blank, but the merged region (plus any other borders and formatting) would remain.

Manager Name Manager Salary Department ID Department Name Department Floor
<jt:multiForEach collections="${managers};${departments}" vars="mgr;dept" pastEndAction="remove">${mgr.lastName}, ${mgr.firstName} ${mgr.salary} ${dept.id} ${dept.name} ${dept.floor}</jt:multiForEach>
           

...gets transformed into...

Manager Name Manager Salary Department ID Department Name Department Floor
Stack, Robert $1000.00 101 Application Development 5
Bunny, Bugs $1500.00 102 Human Resources 4
      103 Quality Assurance 5
           

Another example uses the "replaceExpr" value and the "replaceValue" attribute to replace those expressions past the end of the managers collection. If the "replaceValue" attribute wasn't specified, then the expressions would be replaced by an empty string instead of a "-". The value of the attribute "replaceValue" can itself be an expression, e.g. replaceValue="${replVal}".

Manager Name Manager Salary Department ID Department Name Department Floor
<jt:multiForEach collections="${managers};${departments}" vars="mgr;dept" pastEndAction="replaceExpr" replaceValue="-" >${mgr.lastName}, ${mgr.firstName} ${mgr.salary} ${dept.id} ${dept.name} ${dept.floor}</jt:multiForEach>
           

...gets transformed into...

Manager Name Manager Salary Department ID Department Name Department Floor
Stack, Robert $1000.00 101 Application Development 5
Bunny, Bugs $1500.00 102 Human Resources 4
-, - - 103 Quality Assurance 5