Struts 2 Merge Tag Example


Struts 2 merge tag is used to merge more than one lists or maps into a single list or map. You may also describe as it will merge few iterators into a single iterator.

Similar Post: Struts 2 Append Tag Example

In my example, I have merged the three ArrayList into a single list, two HashMap into a single map and two ArrayList with two HashMap. And by using iterator we will iterate all the values.

Note: Merge tag display the entries randomly.

  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.

Action Class

Create an action class with three ArrayList and two HashMap. Add the values in all.

MergeTagAction.java
package org.websparrow;

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 static final long serialVersionUID = -2422285858809271233L;
	
	// Generate getters and setters...
	private List<String> list1 = new ArrayList<>();
	private List<String> list2 = new ArrayList<>();
	private List<Integer> list3 = new ArrayList<>();

	private Map<String, String> map1 = new HashMap<>();
	private Map<Integer, String> map2 = new HashMap<>();

	@Override
	public String execute() throws Exception {

		// ArrayList Data
		list1.add("Java- Computer");
		list1.add("C++ - Computer");
		list2.add("Physics - Science");
		list2.add("Chemistry - Science");
		list3.add(10);
		list3.add(8);

		// HashMap Data
		map1.put("New Delhi", "India");
		map1.put("Washington DC", "USA");
		map1.put("Moscow", "Russia");
		map2.put(1, "One");
		map2.put(2, "Two");
		map2.put(3, "Three");

		return SUCCESS;
	}
}

JSP Page

On the JSP page use the Struts2 merge tag to combine 3 ArrayList, 2 HashMap and 2 ArrayList + 2 HashMap into a single iterator.

index.jsp
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head></head>
<body>
	<h1>Struts 2 Merge Tag Example</h1>
	<h3>ArrayList Merge</h3>
	<s:merge var="myArrayList">
		<s:param value="%{list1}"></s:param>
		<s:param value="%{list2}"></s:param>
		<s:param value="%{list3}"></s:param>
	</s:merge>
	<ol>
		<s:iterator value="%{#myArrayList}">
			<li><s:property /></li>
		</s:iterator>
	</ol>

	<h3>HashMap Merge</h3>
	<s:merge var="myHashMap">
		<s:param value="%{map1}"></s:param>
		<s:param value="%{map2}"></s:param>
	</s:merge>
	<ol>
		<s:iterator value="%{#myHashMap}">
			<li><s:property /></li>
		</s:iterator>
	</ol>

	<h3>ArrayList + HashMap Merge</h3>
	<s:merge var="myArrayListHashMap">
		<s:param value="%{map1}"></s:param>
		<s:param value="%{map2}"></s:param>
		<s:param value="%{list1}"></s:param>
		<s:param value="%{list2}"></s:param>
	</s:merge>
	<ol>
		<s:iterator value="%{#myArrayListHashMap}">
			<li><s:property /></li>
		</s:iterator>
	</ol>
</body>
</html>

struts.xml

Map the action class in struts.xml.

struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
	<constant name="struts.devMode" value="true" />
	<package name="default" namespace="/" extends="struts-default">
		<action name="details" class="org.websparrow.MergeTagAction">
			<result name="success">/index.jsp</result>
		</action>
	</package>
</struts>
Output:

To see the changes, deploy the application inside the Tomcat » webapps folder and hit the below URL on your browser. You will find the output as shown in the figure.

URL: localhost:8090/Struts2MergeTag/details

Struts 2 Merge Tag Example

Download Source Code: struts2-merge-tag-example.zip


Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.