Append tag in struts2 framework

Append tag in struts2 framework

Previous Home Next

 

Struts2 framework provide Append component to mixed two different list item and print on the browser.

<s:append var=""></s:append>

The struts2 framework provide following append tag example,

<s:append var="TeacherIterator">
     <s:param value="%{malemap}" />
     <s:param value="%{femalemap}" />
</s:append>

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



index.jsp

<%@ taglib uri="/struts-tags" prefix="s" %>
 
<h1>Append Tag Example in Struts2 Framework</h1>
 
1.Combine Two ArrayList into a single iterator.
<s:append var="StudentIterator">
     <s:param value="%{boyslist}" />
     <s:param value="%{girlslist}" />
</s:append>
<ol>
<s:iterator value="%{#StudentIterator}">
     <li><s:property /></li>
</s:iterator>
</ol>
 
2. Combine Two Map into a single iterator.
<s:append var="TeacherIterator">
     <s:param value="%{malemap}" />
     <s:param value="%{femalemap}" />
</s:append>
<ol>
<s:iterator value="%{#TeacherIterator}">
     <li><s:property /></li>
</s:iterator>
</ol>
 
3. Combine BoysList and MaleMap into a single iterator.
<s:append var="both1Iterator">
     <s:param value="%{malemap}" />
     <s:param value="%{boyslist}" />
</s:append>
<ol>
<s:iterator value="%{#both1Iterator}">
     <li><s:property /></li>
</s:iterator>
</ol>
4. Combine GirlsList and FemaleMap into a single iterator.
<s:append var="both2Iterator">
     <s:param value="%{femalemap}" />
     <s:param value="%{girlslist}" />
</s:append>
<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.AppandAction">
<result name="success" type="dispatcher">/index.jsp</result>
</action>
</package>
</struts>

AppandAction.java

package org.r4r;

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

public class AppandAction {
	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