Introduction
Welcome to another Structs 2 control tags article where we will focus on the Append Tag. We will discuss the concept of the Append tag with an example.
Structs is an open-source Java framework used for developing JAVA EE web applications. Many developers use it as it allows them to build maintainable, flexible, and extensible web applications faster.
The Append Tag
The Append tab is a generic tag that appends two or more iterator values in a single iterator. This tag helps us to club two or more lists/hashmaps into a single list/hashmap.
Order of appending:
The Append tag takes the iterators you want to append as parameters and then iterates through the first iterator completely, and then moves on to the next iterator.
Example
List1 = [“Hey1”, “Hey2”, “Hey3”]
List2 = [“Ninja1”, “Ninja2”, “Ninja3”]
After appending the two lists - List1 and List2, using the Append tag, we will get the below list:
result = [“Hey1”, “Hey2”, “Hey3”, “Ninja1”, “Ninja2”, “Ninja3”]
You can see that the elements of the first list are added first, and only after iterating through all the elements of the first list did we move to the second list.
The syntax of the Append tag is:
<s:append var="var_name">
<s:param value="%{iter1}" />
<s:param value="%{iter2}" />
</s:append>
To perform the append operation using the Append tag in structs, we have to do three things:
- Create Action Class
- Create Views
- Create Configuration Files
We will walk you through each step. It will help you understand the concept of the Append tag better.
Let’s begin! 😃
Create Action Class
We will take two ArrayLists and 2 Hashmap properties in this code example. We will append the 2 ArrayLists, 2 HashMap properties, and 1 ArrayList and 1 HashMap property together.
Firstly, we will create an action class that contains the 2 ArrayLists and 2 HashMap properties we want to work with.
ActionNinja.java
package com.codingninjas.struts2;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.opensymphony.xwork2.ActionSupport;
public class ActionNinja extends ActionSupport{
private List<String> List1 = new ArrayList<String>();
private List<String> List2 = new ArrayList<String>();
private Map<String,String> Map1 = new HashMap<String,String>();
private Map<String,String> Map2 = new HashMap<String,String>();
public String execute() {
/* Adding elements to List1*/
List1.add("List1 : Item1");
List1.add("List1 : Item2");
List1.add("List1 : Item3");
/* Adding elements to List2*/
List2.add("List2 : Item1");
List2.add("List2 : Item2");
List2.add("List2 : Item3");
/* Adding elements to HashMap1 */
Map1.put("Map1 - Key1", "Map1 - Value1");
Map1.put("Map1 - Key2", "Map1 - Value2");
Map1.put("Map1 - Key3", "Map1 - Value3");
/* Adding elements to HashMap2 */
Map2.put("Map2 - Key1", "Map2 - Value1");
Map2.put("Map2 - Key2", "Map2 - Value2");
Map2.put("Map2 - Key3", "Map2 - Value3");
return SUCCESS;
}
/* getters and setters methods */
}
Create View
The next thing is to create the view files. This is where we will be using the Append tag. Here we will mention the three Append operations and also print the results.
ViewFile.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head> </head>
<body>
<h1>Example </h1>
1. Combining the two ArrayLists
<s:append var="ListIterator">
<s:param value="%{List1}" />
<s:param value="%{List2}" />
</s:append>
<ol>
<s:iterator value="%{#ListIterator}">
<li><s:property /></li>
</s:iterator>
</ol>
2. Combining 2 HashMap properties
<s:append var="MapIterator">
<s:param value="%{Map1}" />
<s:param value="%{Map2}" />
<s:param value="%{Map3}" />
</s:append>
<ol>
<s:iterator value="%{#MapIterator}">
<li><s:property /></li>
</s:iterator>
</ol>
3. Combining an ArrayList and a HashMap
<s:append var="MixedIterator">
<s:param value="%{List1}" />
<s:param value="%{Map1}" />
</s:append>
<ol>
<s:iterator value="%{#MixedIterator}">
<li><s:property /></li>
</s:iterator>
</ol>
</body>
</html>
Create configuration file
Now we will have to configure the files and set up everything together. The extension of configuration files is .xml. We have created the below-shown structs.xml file:
structs.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="ActionNinja"
class="com.codingninjas.struts2.ActionNinja" >
<result name="success">pages/ViewFile.jsp</result>
</action>
</package>
</struts>
Output
http://localhost:8080/ActionTagExample/ActionNinja.action
You can also checkout Java List Iterator here.