Struts 2 merge control tag

s:merge:

The s:merge control tag is used to merge the two or more iterator (List or Map) values into a single iterator value. It uses s:param tag to take the iterator values as parameters.

Syntax: 

<s:merge var="varName">
     <s:param value="%{iterator1}" />
     <s:param value="%{iterator2}" />
</s:merge>

Note: If iterators are of list type then the output will be first element of iterator1, first element of second iterator, second element of first iterator, second element of second iterator and so on. In case of map iterator the result will with random order.

Example:

index.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  	<title>Struts 2 s:merge control tag example</title>
	<META HTTP-EQUIV="Refresh" CONTENT="0;URL=Test.action">
  </head>  
  <body>
  </body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 
 
 	<filter>
	  <filter-name>struts2</filter-name>
	  <filter-class>
        	org.apache.struts2.dispatcher.ng.
        	filter.StrutsPrepareAndExecuteFilter
          </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>

struts.xml

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
 
<struts>
 
	<package name="default" extends="struts-default">
		<action name="Test" class="com.w3spoint.action.Test">
			<result name="success">/test.jsp</result>
		</action>
	</package>
 
</struts>

Test.java

import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
 
/**
 * This class is used as an action class.
 * @author w3spoint
 */
public class Test extends ActionSupport{
	//data members
	private List<String> subjectList1;
	private List<String> subjectList2;
 
	//business logic
	public String execute(){
		subjectList1 = new ArrayList<String>();
		subjectList1.add("Java");
		subjectList1.add("DBMS");
		subjectList1.add("Networing");
		subjectList1.add("Compiler");
 
		subjectList2 = new ArrayList<String>();
		subjectList2.add("C");
		subjectList2.add("C++");
		subjectList2.add("Operating System");
 
		return SUCCESS;	
	}
 
	//getter setters
	public List<String> getSubjectList1() {
		return subjectList1;
	}
 
	public void setSubjectList1(List<String> subjectList1) {
		this.subjectList1 = subjectList1;
	}
 
	public List<String> getSubjectList2() {
		return subjectList2;
	}
 
	public void setSubjectList2(List<String> subjectList2) {
		this.subjectList2 = subjectList2;
	}
 
}

welcome.jsp

<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
	<head>
		<title>Struts 2 s:merge control tag example</title>
	</head>
	<body>
		<h3>This is a s:merge control tag example.</h3>
 
		<s:merge var="subjectList">
		     <s:param value="%{subjectList1}" />
		     <s:param value="%{subjectList2}" />
		</s:merge>
 
		<h4>Iterator after merging two iterators</h4>
		<s:iterator value="subjectList">
			<s:property/><br/>
		</s:iterator>
 
	</body>
</html>

Output:

struts 44   Download this example.   Next Topic: Struts 2 s:append control tag with example. Previous Topic: Struts 2 iterator control tag with example.

 

Please follow and like us:
Content Protection by DMCA.com