Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction 
2.
The Append Tag
2.1.
Example
2.2.
Create Action Class
2.3.
Create View
2.4.
Create configuration file
2.5.
Output
3.
Frequently Asked Questions
3.1.
What is structs in JAVA?
3.2.
What is an ArrayList in JAVA?
3.3.
What is the Append tag?
3.4.
What is an ORM tool?
3.5.
How does the Append tag work?
4.
Conclusion
Last Updated: Mar 27, 2024
Medium

The Append Tag

Author Teesha Goyal
0 upvote

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. 

Introduction

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. 

Example - Append Tag


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

Output Append Tag

You can also checkout Java List Iterator here.

Must Read Difference between ArrayList and LinkedList

Frequently Asked Questions

What is structs in JAVA?

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. 

What is an ArrayList in JAVA?

An ArrayList in Java is a dynamic array. It is found in java.util package. The major difference between an ArrayList and the built-in array in java is that the size of an ArrayList can be modified.  

What is the Append tag?

The Append tab is a generic tag used to append 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. 

What is an ORM tool?

An Object-relational mapping(ORM) tool is a tool to simplify the creation, manipulation, and storage of objects more accessible. It provides a way to map the objects written in any programming language(python, java) to the SQL database. 

How does the Append tag work?

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 and so on. 

Conclusion

This article will discuss the Append tag in structs in JAVA. We will also discuss the concept of the Append tag using an example.

I hope you would have gained a better understanding of these topics now!

Are you planning to ace the interviews of reputed product-based companies like AmazonGoogleMicrosoft, and more? 

Attempt our Online Mock Test Series on Coding Ninjas Studio now!

Thank You

Happy Coding!

Live masterclass