Menu
Struts 2 > Struts 2 Control Tags Example

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;
    }
}
Subscribe
Contact Us  |  Copyright © 2009 vaannila.com