In this tutorial we will discuss about the control tags in struts2. The Struts 2 tags have a set of tags that make it easy to control the flow of page execution. Following is the list of important Struts 2 Control Tags:

The if and else tags:

These tags perform basic condition flow found in every language. ‘If’ tag could be used by itself or with ‘Else If’ Tag and/or single/multiple ‘Else’ Tag as shown below:

<s:if test="%{#name=='Dinesh'}">
  This is Male Category
 </s:if>
 <s:elseif test="%{#name=='Sweety'}">
     This is Female Category
 </s:elseif>
 <s:else>
     Other Category
 </s:else>

Click here for detail example.

The iterator tags:

These iterator will iterate over a value. An iterable value can be any of: java.util.Collection, java.util.Iterator. While iterating over an iterator, you can use Sort tag to sort the result or SubSet tag to to get a sub set of the list or array.

The following example retrieves the value of the getUsers() 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.

<s:iterator value="users">
  <p>User Name is: <s:property name="userName"/></p>
</s:iterator>

Click here for detail Example

The append tag:

These append tag take two or more lists as parameters and append them all together as shown below:

<s:append var="myAppendIterator">
     <s:param value="%{myList1}" />
     <s:param value="%{myList2}" />
     <s:param value="%{myList3}" />
</s:append>
<s:iterator value="%{#myAppendIterator}">
     <s:property />
</s:iterator>

Click here for detail Example.

The merge tag:

These merge tag take two or more lists as parameters and merge them all together as shown below:

<s:merge var="myMergedIterator">
     <s:param value="%{myList1}" />
     <s:param value="%{myList2}" />
     <s:param value="%{myList3}" />
</s:merge>
<s:iterator value="%{#myMergedIterator}">
     <s:property />
</s:iterator>

Click here for Detail Example.

The generator tag:

These generator tag generates an iterator based on the val attribute supplied. Following generator tag generates an iterator and print it out using the iterator tag.

<s:generator val="%{'dinesh,sweety,aadesh,vinesh,sandhya'}">
 <s:iterator>
     <s:property /><br/>
 </s:iterator>
</s:generator>

Click here for detail Example.

The sort tag:

Struts 2 sort tag is used to sort a List using a java.util.Comparator. In this example, we will create 6 Users objects and add all into an ArrayList, and use the sort tag to sort the ArrayList based on the User’s property.

<s:sort comparator="#userNameComparator" source="users">
<ol>
<s:iterator>
     <li><s:property value="userName" />, 
         <s:property value="userName" />, 
         <s:property value="age" />
     </li>
</s:iterator>
</ol>
</s:sort>

Click here for detail Example.

The subset tag:

A JSP page to show the use of subset tag to output a subset of an iterator

<s:subset source="myList">
  <s:iterator>
  <s:property /><br>
  </s:iterator>
  </s:subset>

Click here for detail  Example.

 

<<Previous <<   || Index ||   >>Next >>
Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

2 years ago