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.
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”]
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; }
}
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:
