Table of contents
1.
Introduction 
2.
The Merge Tag
2.1.
Example
2.2.
Create Action Classes
2.3.
Create Views
2.4.
Create Configuration Files
3.
Frequently Asked Questions
3.1.
What is structs in JAVA?
3.2.
What is a set in java?
3.3.
What is a merge tag?
3.4.
What is an ORM tool?
3.5.
How does the merge tag work?
4.
Conclusion
Last Updated: Mar 27, 2024
Medium

The Merge Tag

Author Teesha Goyal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 

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. 

This article will discuss the merge tag present in structs 2. We will discuss the concept of merge tag by taking an example. 

Structs 2 logo

The Merge Tag

The merge tab is a generic tag used to merge two or more lists and form a single list. It considers the first elements of each list one at a time until it reaches the final list and then again begins with the second element of the first list until the last. This goes on till the iterator covers all the elements.

Example

List1 = [“hey1”, “hey2”, “hey3”]

List2 = [“Ninja1”, “Ninja2”, “Ninja3”] 

After merging the two lists - List1 and List2, using the merge tag, we will get the below list:

result = [“hey1”, “Ninja1”, “hey2”, “Ninja2”, “hey3”, “Ninja3”] 

Example of merge tag

To perform the merge operation using the merge tag in structs, we have to do three things:

  • Create Action Classes
  • Create Views 
  • Create Configuration Files

 

We will walk you through each step. It will help you understand the concept of the merge tag better. 

Let’s begin! 😃

Create Action Classes

Let’s first create an action class Ninja.java. This action file will have a class with two lists- List1 and List2. Both the lists have 3 elements, and we will merge the two lists and view the outcome. Following is your Ninja.java file: 

package com.codingninjas.struts2;

import java.util.ArrayList;
import java.util.List;
import org.apache.struts2.util.SubsetIteratorFilter.Decider;

public class Ninjas {
   /* Initializing Two Lists */
   private List<String> List1 = new ArrayList<String>();
   private List<String> List2 = new ArrayList<String>();
 
   public String execute() {
      /* Adding elements to List1 */  
      List1.add("hey1");
      List1.add("hey2");
      List1.add("hey3");
         
      /* Adding elements to List2 */
      List2.add("Ninja1");
      List2.add("Ninja2");
      List1.add("Ninja3");
 
      return SUCCESS;
   }

   public List<String> getList1() {
      return List1; }

   public void setList1(List<String> List1) {
      this.List1 = List1; }

   public List<String> getList2() {
      return List2; }

   public void setList2(List<String> List2) {
      this.List2 = List2; }
}
You can also try this code with Online Java Compiler
Run Code


Create Views

A view will contain our merge tag. It takes the lists we want to merge as arguments and to track the iterator, we also have to provide an id. We have created a view by the name CodingNijas.jsp, as shown below:

<%@ taglib prefix="s" uri="/struts-tags" %>

<html>
   <head>
      <title>The Merge Tag - Coding Ninjas Studio</title>
   </head>
   <body>
      <s:merge id="myid">
      <s:param value="%{List1}" />
      <s:param value="%{List2}" />
      </s:merge>
      <s:iterator value="%{#myid}">
      <s:property />
      </s:iterator>
   </body>
</html>


Create Configuration Files

The last thing is to make your configuration files ready. The extension of configuration files is .xml. We have created the below-shown structs.xml file:

<?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.enable.DynamicMethodInvocation" value="false" />
   <constant name="struts.devMode" value="false" />
   <constant name="struts.custom.i18n.resources" value="myapp" />

   <package name="default" extends="struts-default" namespace="/">
      <action name="mergetag" class="Ninjas">
         <result name="success">/CodingNijas.jsp</result>
      </action>
   </package>
</struts>


Now that everything is done, you can see that your web.xml file looks like this: 
 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
   <display-name>Struts2ControlTags</display-name>
   <filter>
      <filter-name>struts2</filter-name>
      <filter-class>
         org.apache.struts2.dispatcher.FilterDispatcher
      </filter-class>
   </filter>
   <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
   </filter-mapping>
   <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
   </welcome-file-list>
</web-app>


Now we are done with the writing part, all we are left to do is to check the outcome. For that, you have to Right click on the project name. From the options, go to Export > WAR. This will create a WAR(Web application ARchive) file, which will be deployed to Tomcat's web apps directory. You can use the URL to access the output page.

Following is what the output would look like:

Output

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 a set in java?

A set in java is a data structure used to store unique elements. Duplications are not allowed in a set collection. To be able to add an element to a set, it must implement both the equals() and hashCode() methods. 

What is a merge tag?

The merge tab is a generic tag that is used to merge two or more lists and form a single list. It takes lists as an argument and then merges them using an iterator.

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 merge tag work?

To merge the lists, it considers the first elements of each list one at a time till it reaches the last list and then again begins with the second element of the first list until the last list. This goes on till the itertor covers all the elements.

Conclusion

This article will discuss the merge tag in structs in JAVA. We will also discuss the concept of the merge 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