Merge tag example in struts2 framework

Merge tag example in struts2 framework

Previous Home Next

 

Struts2 framework provide Merge component to mixed two different list and map item and print on the browser.
<s:merge var="">
     <s:param value="" />
</s:merge>

The struts2 framework provide following merge tag example,
<s:merge var="TeacherIterator">
     <s:param value="%{malemap}" />
     <s:param value="%{femalemap}" />
</s:merge>

 
Directory Structure of <s:merge> tag Example in Struts 2.0 Using MyEclipse IDE



index.jsp

<%@ taglib uri="/struts-tags" prefix="s" %>
 
<h1>Merge Tag Example in Struts2 Framework</h1>
 
1.Combine Two ArrayList into a single iterator.
<s:merge var="StudentIterator">
     <s:param value="%{boyslist}" />
     <s:param value="%{girlslist}" />
</s:merge>
<ol>
<s:iterator value="%{#StudentIterator}">
     <li><s:property /></li>
</s:iterator>
</ol>
 
2. Combine Two Map into a single iterator.
<s:merge var="TeacherIterator">
     <s:param value="%{malemap}" />
     <s:param value="%{femalemap}" />
</s:merge>
<ol>
<s:iterator value="%{#TeacherIterator}">
     <li><s:property /></li>
</s:iterator>
</ol>
 
3. Combine BoysList and MaleMap into a single iterator.
<s:merge var="both1Iterator">
     <s:param value="%{malemap}" />
     <s:param value="%{boyslist}" />
</s:merge>
<ol>
<s:iterator value="%{#both1Iterator}">
     <li><s:property /></li>
</s:iterator>
</ol>
4. Combine GirlsList and FemaleMap into a single iterator.
<s:merge var="both2Iterator">
     <s:param value="%{femalemap}" />
     <s:param value="%{girlslist}" />
</s:merge>
<ol>
<s:iterator value="%{#both2Iterator}">
     <li><s:property /></li>
</s:iterator>
</ol>

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>f1</filter-name>
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
  <filter-name>f1</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration
2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="demo" extends="struts-default">
<action name="" class="org.r4r.MergeTagAction">
<result name="success" type="dispatcher">/index.jsp</result>
</action>
</package>
</struts>

MergeTagAction.java

package org.r4r;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MergeTagAction {
	List<String> boyslist;
	List<String> girlslist;
	
	Map<String,String> malemap;
	Map<String,String> femalemap;
	
	public String execute(){
		boyslist=new ArrayList<String>();
		boyslist.add("Mukund(BOYS)");
		boyslist.add("Harsh(BOYS)");
		boyslist.add("Somaru(BOYS)");
		boyslist.add("Pramod(BOYS)");
		boyslist.add("Sushant(BOYS)");
		girlslist=new ArrayList<String>();
		girlslist.add("Suman(Girls)");
		girlslist.add("Raman(Girls)");
		girlslist.add("Radha(Girls)");
		girlslist.add("Kavita(Girls)");
		girlslist.add("Babita(Girls)");
		malemap=new HashMap<String, String>();
		malemap.put("one","R.K.Singh(Male)");
		malemap.put("two","H.Vardhan(Male)");
		malemap.put("three","B.M.K.Singh(Male)");
		malemap.put("four","R.K.Rana(Male)");
		malemap.put("five","Ramu yadav(Male)");
		femalemap=new HashMap<String, String>();
		femalemap.put("one","Kavita Dahima(Female)");
		femalemap.put("two","Suman Singh(Female)");
		femalemap.put("three","Kanchan Bala(Female)");
		femalemap.put("four","Babita Singh(Female)");
		femalemap.put("five","Radha(Female)");
		
		return "success";
	}
	public List<String> getBoyslist() {
		return boyslist;
	}
	public void setBoyslist(List<String> boyslist) {
		this.boyslist = boyslist;
	}
	public List<String> getGirlslist() {
		return girlslist;
	}
	public void setGirlslist(List<String> girlslist) {
		this.girlslist = girlslist;
	}
	public Map<String, String> getMalemap() {
		return malemap;
	}
	public void setMalemap(Map<String, String> malemap) {
		this.malemap = malemap;
	}
	public Map<String, String> getFemalemap() {
		return femalemap;
	}
	public void setFemalemap(Map<String, String> femalemap) {
		this.femalemap = femalemap;
	}
}

Output






Previous Home Next