Struts 2 s:append control tag

s:append:

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

Syntax:

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

Note: First all elements of first iterator will be iterated, then all elements of second iterator and so on.

Example:

index.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  	<title>Struts 2 s:append 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.w3schools.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 w3schools
 */
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;
	}
 
}

test.jsp

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

Output:

struts 45
 
Download this example.
 
Next Topic: Struts 2 s:generator control tag with example.
Previous Topic: Struts 2 merge control tag with example.