Struts 2 merge tag is used to merge few Iterators (created by List or Map) into a single Iterator. In this tutorials, you will use Struts 2 merge tag to do the following tasks :

  1. Merge three ArrayList into a single Iterator.
  2. Merge three HashMap into a single Iterator.
  3. Merge ArrayList and HashMap into a single Iterator.
Assume 2 iterators, each has two entries, after merge with merge tag into a single iterator, the order of the entries will look like following :

  1. First entry of the first iterator.
  2. First entry of the second iterator.
  3. Second entry of the first iterator.
  4. Second entry of the second iterator.

This is only apply to List iterator; Map iterator, the order will be random.

1. Action

An Action class with 3 ArrayList and 3 HashMap properties.

MergeTagAction

package com.mkyong.common.action;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import com.opensymphony.xwork2.ActionSupport;
 
public class MergeTagAction extends ActionSupport{
 
	private List<String> list1 = new ArrayList<String>();
	private List<String> list2 = new ArrayList<String>();
	private List<String> list3 = new ArrayList<String>();
 
	private Map<String,String> map1 = new HashMap<String,String>();
	private Map<String,String> map2 = new HashMap<String,String>();
	private Map<String,String> map3 = new HashMap<String,String>();
 
	public String execute() {
 
		list1.add("List1 - 1");
		list1.add("List1 - 2");
		list1.add("List1 - 3");
 
		list2.add("List2 - 1");
		list2.add("List2 - 2");
		list2.add("List2 - 3");
 
		list3.add("List3 - 1");
		list3.add("List3 - 2");
		list3.add("List3 - 3");
 
		map1.put("map1-key1", "map1-value1");
		map1.put("map1-key2", "map1-value2");
		map1.put("map1-key3", "map1-value3");
 
		map2.put("map2-key1", "map2-value1");
		map2.put("map2-key2", "map2-value2");
		map2.put("map2-key3", "map2-value3");
 
		map3.put("map3-key1", "map3-value1");
		map3.put("map3-key2", "map3-value2");
		map3.put("map3-key3", "map3-value3");
 
		return SUCCESS;
	}
 
	//getter methods...
}

2. Append tag example

A JSP page to show the use of merge tag to combine the 3 ArrayList / 3 HashMap / 1 ArrayList + 1 HashMap into a single iterator, and loop over its value and print it out.

merge.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
 <html>
<head>
</head>
 
<body>
<h1>Struts 2 Merge tag example</h1>
 
1. Merge 3 ArrayList into a single iterator.
<s:merge var="customListIterator">
     <s:param value="%{list1}" />
     <s:param value="%{list2}" />
     <s:param value="%{list3}" />
</s:merge>
<ol>
<s:iterator value="%{#customListIterator}">
     <li><s:property /></li>
</s:iterator>
</ol>
 
2. Merge 3 HashMap into a single iterator.
<s:merge var="customMapIterator">
     <s:param value="%{map1}" />
     <s:param value="%{map2}" />
     <s:param value="%{map3}" />
</s:merge>
<ol>
<s:iterator value="%{#customMapIterator}">
     <li><s:property /></li>
</s:iterator>
</ol>
 
3. Merge ArrayList and HashMap into a single iterator.
<s:merge var="customMixedIterator">
     <s:param value="%{list1}" />
     <s:param value="%{map1}" />
</s:merge>
<ol>
<s:iterator value="%{#customMixedIterator}">
     <li><s:property /></li>
</s:iterator>
</ol>
 
</body>
</html>

3. struts.xml

Link it ~

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
 
<struts>
 
 	<constant name="struts.devMode" value="true" />
 
	<package name="default" namespace="/" extends="struts-default">
 
		<action name="mergeTagAction" 
			class="com.mkyong.common.action.MergeTagAction" >
			<result name="success">pages/merge.jsp</result>
		</action>
 
	</package>
 
</struts>

4. Demo

http://localhost:8080/Struts2Example/mergeTagAction.action

Output

Struts 2 Merge tag example
 
1. Merge 3 ArrayList into a single iterator.
 
  1. List1 - 1
  2. List2 - 1
  3. List3 - 1
  4. List1 - 2
  5. List2 - 2
  6. List3 - 2
  7. List1 - 3
  8. List2 - 3
  9. List3 - 3
 
2. Merge 3 HashMap into a single iterator.
 
  1. map1-key3=map1-value3
  2. map2-key2=map2-value2
  3. map3-key3=map3-value3
  4. map1-key1=map1-value1
  5. map2-key3=map2-value3
  6. map3-key1=map3-value1
  7. map1-key2=map1-value2
  8. map2-key1=map2-value1
  9. map3-key2=map3-value2
 
3. Merge ArrayList and HashMap into a single iterator.
 
  1. List1 - 1
  2. map1-key3=map1-value3
  3. List1 - 2
  4. map1-key1=map1-value1
  5. List1 - 3
  6. map1-key2=map1-value2

Reference

  1. Struts 2 Merge tag documentation
Note : You can find more similar articles at - Struts 2.x Tutorials