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 typeArrayList. To know more
about the iterator status we can create an instance of the IteratorStatusobject 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 theIteratorStatus 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.
|
No comments:
Post a Comment