Struts 2 Append Tag Example


On this page, you will learn how to concatenate the multiple lists into a single list. To do this type of operation Struts 2 provide the append tag to perform the operation.

Similar Post: Struts 2 Merge Tag Example

In my example, I have appended 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: Struts 2 append tag always concatenate the value next to the first.

Action Class

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

AppendTagAction.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 AppendTagAction 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");
		list2.add("C++");
		list2.add("PHP");
		list3.add(10);
		list3.add(8);
		list3.add(5);

		// 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 append 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 Append Tag Example</h1>
	<h3>ArrayList Append Example</h3>
	<s:append var="myArrayList">
		<s:param value="%{list1}"></s:param>
		<s:param value="%{list2}"></s:param>
		<s:param value="%{list3}"></s:param>
	</s:append>
	<ol>
		<s:iterator value="%{#myArrayList}">
			<li><s:property /></li>
		</s:iterator>
	</ol>

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

	<h3>ArrayList and HashMap Append Example</h3>
	<s:append 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:append>
	<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.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="details" class="org.websparrow.AppendTagAction">
			<result name="success">/index.jsp</result>
		</action>
	</package>
</struts>
Output:

To see the changes, deploy the application into 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/Struts2AppendTag/details

Struts 2 Append Tag Example

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

References

  1. Control Tags

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.