DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Data Engineering
  3. Data
  4. Struts 2 Control Tags Tutorial

Struts 2 Control Tags Tutorial

Meyyappan Muthuraman user avatar by
Meyyappan Muthuraman
·
Jun. 14, 12 · Tutorial
Like (0)
Save
Tweet
Share
46.51K Views

Join the DZone community and get the full member experience.

Join For Free

Struts 2 Control Tags Example

In this example you will see how display the following details using the Struts 2 iterator tag and the if and else tags.

In our AlbumInfoAction class we populate the artist and the song details using the following code. 

package vaannila;

import java.util.ArrayList;
import java.util.List;


public class AlbumInfoAction{

private String title;
private Artist artist;
private static List<Song> songs = new ArrayList<Song>();

    static {
        songs.add(new Song("Thriller","Disco"));
        songs.add(new Song("Beat It","Rock"));
        songs.add(new Song("Billie Jean","Pop"));
    }

    public String populate()
    {
        title = "Thriller";
        artist = new Artist("Michael Jackson","King of pop");
        return "populate";
    }

    public String execute()
    {
        return "success";
    }

    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public Artist getArtist() {
        return artist;
    }
    public void setArtist(Artist artist) {
        this.artist = artist;
    }

    public List<Song> getSongs() {
        return songs;
    }

}

The song class contains the title and the genre attributes.

package vaannila;

public class Song {

    private String title;
    private String genre;

    Song(String title, String genre)
    {
        this.title = title;
        this.genre = genre;
    }
    public String getTitle() {
            return title;
    }
    public void setTitle(String title) {
            this.title = title;
    }
    public String getGenre() {
            return genre;
    }
    public void setGenre(String genre) {
            this.genre = genre;
    }
}

Struts 2 Iterator Tag Example

In Stuts 2 the iterator tag is used to loop over a collection of objects. The iterator tag can iterate over any Collection, Map, Enumeration, Iterator, or array.

<table class="songTable">
    <tr class="even">
        <td><b>Title</b></td>
        <td><b>Genre</b></td>
    </tr>
    <s:iterator value="songs" status="songStatus">
            <tr
                class="<s:if test="#songStatus.odd == true ">odd</s:if><s:else>even</s:else>">
                <td><s:property value="title" /></td>
                <td><s:property value="genre" /></td>
            </tr>
    </s:iterator>
</table>

We use the iterator tag to iterator over the collection of songs. Here the Song property is of type ArrayList. To know more about the iterator status we can create an instance of the IteratorStatus object by specifying a value to the status attribute of the iterator tag. The newly created instance will be placed in the ActionContext which can be refered using the the following OGNL expression #statusName.

The table show the different properties of the IteratorStatus object.

Name Return Type Description
index int zero-based index value.
count int index + 1
first boolean returns true if it is the first element
last boolean returns true if it is the last element
even boolean returns true if the count is an even number.
odd boolean returns true if the count is an odd number.
modulus int takes an int value and returns the modulus of count.

Struts 2 If and Else Tags Example

We use the if and else tags to highlight the even and odd rows in different colors. We use the IteratorStatus class methods to find whether the row is even or odd. The following code shows how to do this.

<s:iterator value="songs" status="songStatus">
<tr
class="<s:if test="#songStatus.odd == true ">odd</s:if><s:else>even</s:else>">
<td><s:property value="title" /></td>
<td><s:property value="genre" /></td>
</tr>
</s:iterator>

The elseif tag is also available. You can use it if there are multiple conditions to check.

You can download the Struts 2 Control Tags Example by clicking the Download link below.

Source :Download
Source + Lib :Download
Data Types Database Object (computer science) Property (programming) Attribute (computing) Data structure Download Modulus (algebraic number theory) Element

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Tracking Software Architecture Decisions
  • Monolithic First
  • How To Choose the Right Streaming Database
  • Introduction to Spring Cloud Kubernetes

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: