Struts2 - Subset Tag (Control Tags)

In this section, we are going to describe the subset tag. The subset tag is a generic tag that takes an iterator and outputs a subset of it. It delegates to org.apache.struts2.util.SubsetIteratorFilter internally to perform the subset functionality.

Add the following code snippet into the struts.xml file.

struts.xml

<action name="subsetTag" class="net.struts2.mergeTag">

<result>/pages/genericTags/subsetTag.jsp</result> 

</action>

Create a list in the action class and populate it with various items as shown in the "subsetTag" class.

subsetTag.java

package net.struts2; 
import com.opensymphony.xwork2.ActionSupport; 
import java.util.*;

public class subsetTag extends ActionSupport { 
   private List myList;

   public String execute() throws Exception{ 
      myList = new ArrayList(); 
      myList.add(new Integer(50)); 
      myList.add(new Integer(20)); 
      myList.add(new Integer(100)); 
      myList.add(new Integer(85)); 
      myList.add(new Integer(500));
      return SUCCESS;
    }

   public List getMyList(){ 
     return myList;
   }
}


Now create a jsp page using and tags as shown in the subsetTag.jsp page. The subset tag takes an iterator and outputs a subset of it.

subsetTag.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html> 
 <head>
   <title>Subset Tag Example!</title> 
 </head>
 <body> 
  <h1><span style="background-color: #FFFFcc">Subset Tag Example!</span></h1> 
   <s:subset source="myList">
    <s:iterator> 
     <s:property /><br>
    </s:iterator> 
   </s:subset>
 </body> 
</html>


Output of Subset Tag:

Struts2 - Merge Tag (Control Tags)

In this section, we are going to describe the merge tag. The merge tag is a generic tag that is used to merge iterators. The successive call to the merge iterator causes each merge iterator to have a chance to expose its element, subsequently next call allows the next iterator to expose its element. Once the last iterator is done exposing its element, the first iterator is allowed to do so again (unless it is exhausted of entries).

In the current example, 2 lists being merged, each list have 5 entries, the following will be the logic.

1. Display first element of the first list.
2. Display first element of the second list.
3. Display second element of the first list.
4. Display second element of the second list.
5. Display third element of the first list.
6. Display thrid element of the second list.....and so on.


Add the following code snippet into the struts.xml file.

struts.xml

<action name="mergeTag" class="net.struts2.mergeTag">

<result>/pages/genericTags/mergeTag.jsp</result> 

</action>

Create two lists in the action class and populate them with various items as shown in the "mergeTag" class.

mergeTag.java

package net.roseindia; 

import com.opensymphony.xwork2.ActionSupport; 

import java.util.*;


public class mergeTag extends ActionSupport { 

   private List myList; 

   private List myList1;


   public String execute() throws Exception{ 

     myList = new ArrayList(); 

     myList.add("Deepak Kumar"); 

     myList.add("Sushil Kumar"); 

     myList.add("Vinod Kumar"); 

     myList.add("Amit Kumar");

     myList1 = new ArrayList(); 

     myList1.add("Himanshu Raj"); 

     myList1.add("Mr. khan"); 

     myList1.add("John"); 

     myList1.add("Ravi Ranjan"); 

   return SUCCESS;

  } 

  public List getMyList(){

    return myList;

  }

  public List getMyList1(){ 

   return myList1;

 }

}


Now create a jsp page using and tags as shown in the mergeTag.jsp page. The merge tag is used to merge iterators. The "id" parameter keeps the resultant iterator stored under in the stack's context and the "value" parameter in the is used to get the values contained within the respective iterators.


mergeTag.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>

<html> 

 <head>

   <title>Merge Tag Example!</title> </head>

 <body> 

   <h1><span style="background-color: #FFFFcc">Merge Tag Example!</span></h1>

   <s:merge id="mergeId"> 

    <s:param value="%{myList}" /> 

    <s:param value="%{myList1}" />

   </s:merge>

   <s:iterator value="%{#mergeId}"> 

      <s:property /><br>

   </s:iterator> 

  </body>

</html>


Output of merge Tag:

Struts2 - Iterator Tag (Control Tags)

In this section, we are going to describe the Iterator tag. Iterator tag is used to iterate over a value. An iterable value can be either of: java.util.Collection, java.util.Iterator.

Add the following code snippet into the struts.xml file.

struts.xml

<action name="iteratorTag" class="net.struts2.iteratorTag">

<result>/pages/genericTags/iteratorTag.jsp</result> 

</action>

Create an action class as shown:

iteratorTag.java

package net.struts2; 

import com.opensymphony.xwork2.ActionSupport; 

import java.util.*;


public class iteratorTag extends ActionSupport{

 private List myList;

 public String execute()throws Exception{

    myList = new ArrayList(); 

    myList.add("Fruits"); 

    myList.add("Apple"); 

    myList.add("Mango"); 

    myList.add("Orange"); 

    myList.add("Pine Apple");

    return SUCCESS;

   }

   public List getMyList(){

    return myList;

   }

}

The following example retrieves the value of the getMyList() method of the current object on the value stack and uses it to iterate over. The tag prints out the current value of the iterator.


iteratorTag.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>

<html> 

 <head>

  <title>Iterator Tag Example!</title> 

 </head>

 <body> 

   <h1><span style="background-color: #FFFFcc">Iterator Tag Example!

      </span></h1> 

    <s:iterator value="myList">

      <s:property /><br> 

    </s:iterator>

 </body> 

</html>

Output of An Iterator Tag Example:

Strits2 - Generator Tag (Control Tags)

In this section, we are going to describe the generator tag. The generator tag is a generic tag that is used to generate iterators based on different attributes passed. Here we will not pass any attribute.
Add the following code snippet into the struts.xml file.

sturts.xml
<action name="GeneratorTag" class="net.struts2.GeneratorTag">

<result>/pages/genericTags/GeneratorTag.jsp</result> 

</action>
Create an action class as shown:
GeneratorTag.java

package net.struts2; 

import com.opensymphony.xwork2.ActionSupport; 

import org.apache.struts2.util.IteratorGenerator.Converter;


public class GeneratorTag extends ActionSupport { 

  public String excute() throws Exception{

  return SUCCESS;

 }

}

Create a jsp page where the generator tag generates a simple iterator based on the val attribute supplied and tag prints it out using the tag. The separator attribute is used to separate the val into entries of the iterator.

GeneratorTag.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>

<html> 

 <head>

  <title> Generator Tag Example! </title> 

 </head>

<body> 

<h1><span style="background-color: #FFFFcc">Generator Tag Example!</span></h1>

<h3><font color="#0000FF"> Generates a Simple Iterator </font></h3>

 <s:generator val="%{'sarwar,Deepak Kumar,Sushil Kumar, Vinod Kumar,
Amit Kumar'}" separator=",">

  <s:iterator> 

   <s:property /><br/>

  </s:iterator> 

 </s:generator>

</body> 

</html>

Output of Generator Tag Example:

Struts2 - Append Tag (Control Tags)

In this section, we are going to describe the append tag. The append tag is a generic tag that is used to merge multiple iterators into one iterator. Append Iterator Tag is used to append iterators to form an appended iterator through which the entries goes from one iterator to another after each respective iterator is exhausted of entries.

Add the following code snippet into the struts.xml file.

struts.xml:
<action name="AppendTag" class="net.struts.AppendTag">

<result>/pages/genericTags/AppendTag.jsp</result> 

</action>

Create two lists in the action class and populate them with various items as shown in the "AppendTag" class.


AppendTag.java

package net.stuts2-example; 
import com.opensymphony.xwork2.ActionSupport; 
import java.util.*;

public class AppendTag extends ActionSupport{ 
 private List myList;
 private List myList1;

 public String execute()throws Exception{ 
   myList = new ArrayList(); 
   myList.add("www.struts.net"); 
   myList.add("Deepak Kumar"); 
   myList.add("Sushil Kumar"); 
   myList.add("Vinod Kumar"); 
   myList.add("Amit Kumar");
   myList1 = new ArrayList(); 
   myList1.add("www.javajazzup.com"); 
   myList1.add("Himanshu Raj"); 
   myList1.add("Mr. khan"); 
   myList1.add("John"); 
   myList1.add("Ravi Ranjan");
 return SUCCESS; 
}


 public List getMyList(){
  return myList;
 }

 public List getMyList1(){ 
  return myList1;
 }
}


Now create a jsp page using and tags as shown in the AppendTag.jsp page. The append tag is used to merge multiple iterators into one iterator. The "id" parameter keeps the resultant appended iterator stored under the stack's context and the "value" parameter is used to get the values contained within the resultant iterator.
AppendTag.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html> 
 <head>
   <title> Append Tag Example!</title> 
 </head>
 <body>
  <h1><span style="background-color: #FFFFcc"> Append Tag Example! </span></h1>
   <s:append id="myAppendList"> 
    <s:param value="%{myList}" /> 
    <s:param value="%{myList1}" />
   </s:append>
   <s:iterator value="%{#myAppendList}"> 
     <s:property /><br>
   </s:iterator> 
 </body>
</html>




Output of the Append Tag Example:

Struts2 - Control Tags


In this section we are going to discuss the various control tags ( The Control Tags are used for flow control such as if, else and iterate.)

'If' tag could be used by itself or with 'Else If' Tag and/or single/multiple 'Else' Tag. Create a JSP page IfControlTag.jsp.

Set a property 'technologyName' with a value 'Java' as
<s:set name="technologyName" value="%{'Java'}"/>


Among if, elseif and else tags only one tag evaluates at a time. Evaluation is based upon the conditions being processed. Evaluated conditions must be of Boolean type. This is illustrated in the following Jsp page.

[Note:If the condition in tag evaluates to 'true' then only this tag is evaluated and others are discarded. As illustrated in the example.
If the condition in tag evaluates to 'false' and tag evaluates to 'true' then the body of the tag is processed.
If the condition in tag and tags evaluates to 'false' then only the tag is processed. ]

IfControlTag.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html> <head>
<title>Struts 2 Control Tag Example</title> </head>
<body> <s:set name="technologyName" value="%{'Java'}"/>
<s:if test="%{#technologyName=='Java'}"> <div>
<s:property value="%{#technologyName}" /></div>
</s:if>
<s:elseif test="%{#technologyName=='Jav'}"> <div>
<s:property value="%{#technologyName}" /></div>
</s:elseif>
<s:else> <div>
Technology Value is not Java</div>
</s:else>
</body> 
</html>

struts.xml: Add the following xml snippet in the struts.xml file.

<action name="doIf" >
<result>/pages/genericTags/IfControlTag.jsp</result> </action>

index.jsp: Add the following jsp snippet in the index.jsp file.

<ul>
<li><a href="roseindia/doIf.action">IF Control Tag Example</a></li> </ul>

In the IfControlTag.jsp only tag evaluates to true

<s:if test="%{#technologyName=='Java'}"> <div><s:property value="%{#technologyName}" /></div> </s:if>


So we get the output equal to Java