Struts 2 Append tag example

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

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

  1. First entry of the first iterator.
  2. Second entry of the first iterator.
  3. First entry of the second 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.

AppendTagAction


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 AppendTagAction 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 append 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.

appendIterator.jsp


<%@ taglib prefix="s" uri="/struts-tags" %>
 <html>
<head>
</head>
 
<body>
<h1>Struts 2 Append tag example</h1>

1. Combine 3 ArrayList into a single iterator.
<s:append var="customListIterator">
     <s:param value="%{list1}" />
     <s:param value="%{list2}" />
     <s:param value="%{list3}" />
</s:append>
<ol>
<s:iterator value="%{#customListIterator}">
     <li><s:property /></li>
</s:iterator>
</ol>

2. Combine 3 HashMap into a single iterator.
<s:append var="customMapIterator">
     <s:param value="%{map1}" />
     <s:param value="%{map2}" />
     <s:param value="%{map3}" />
</s:append>
<ol>
<s:iterator value="%{#customMapIterator}">
     <li><s:property /></li>
</s:iterator>
</ol>

3. Combine ArrayList and HashMap into a single iterator.
<s:append var="customMixedIterator">
     <s:param value="%{list1}" />
     <s:param value="%{map1}" />
</s:append>
<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="appendTagAction" 
			class="com.mkyong.common.action.AppendTagAction" >
			<result name="success">pages/appendIterator.jsp</result>
		</action>
		
	</package>
		
</struts>

4. Demo

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

Output


Struts 2 Append tag example

1. Combine 3 ArrayList into a single iterator.

  1. List1 - 1
  2. List1 - 2
  3. List1 - 3
  4. List2 - 1
  5. List2 - 2
  6. List2 - 3
  7. List3 - 1
  8. List3 - 2
  9. List3 - 3

2. Combine 3 HashMap into a single iterator.

  1. map1-key3=map1-value3
  2. map1-key1=map1-value1
  3. map1-key2=map1-value2
  4. map2-key2=map2-value2
  5. map2-key3=map2-value3
  6. map2-key1=map2-value1
  7. map3-key3=map3-value3
  8. map3-key1=map3-value1
  9. map3-key2=map3-value2

3. Combine ArrayList and HashMap into a single iterator.

  1. List1 - 1
  2. List1 - 2
  3. List1 - 3
  4. map1-key3=map1-value3
  5. map1-key1=map1-value1
  6. map1-key2=map1-value2

Reference

  1. Struts 2 Append tag documentation

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
2 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
3 Comment authors
ChenmkyongSivaranjan Recent comment authors
newest oldest most voted
Chen
Guest
Chen

What if i only want the value of the map?

Thanks!

Sivaranjan
Guest
Sivaranjan

Excellent article. Would like to see more info on the same though.Also, you might be interested in 1000+ free java ebooks from my website.