Tuesday, August 6, 2013

Struts 2 Control Tags Tutorial

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. 
01.package vaannila;
02. 
03.import java.util.ArrayList;
04.import java.util.List;
05. 
06. 
07.public class AlbumInfoAction{
08. 
09.private String title;
10.private Artist artist;
11.private static List<Song> songs = new ArrayList<Song>();
12. 
13.static {
14.songs.add(new Song("Thriller","Disco"));
15.songs.add(new Song("Beat It","Rock"));
16.songs.add(new Song("Billie Jean","Pop"));
17.}
18. 
19.public String populate()
20.{
21.title = "Thriller";
22.artist = new Artist("Michael Jackson","King of pop");
23.return "populate";
24.}
25. 
26.public String execute()
27.{
28.return "success";
29.}
30. 
31.public String getTitle() {
32.return title;
33.}
34.public void setTitle(String title) {
35.this.title = title;
36.}
37.public Artist getArtist() {
38.return artist;
39.}
40.public void setArtist(Artist artist) {
41.this.artist = artist;
42.}
43. 
44.public List<Song> getSongs() {
45.return songs;
46.}
47. 
48.}
The song class contains the title and the genre attributes.
01.package vaannila;
02. 
03.public class Song {
04. 
05.private String title;
06.private String genre;
07. 
08.Song(String title, String genre)
09.{
10.this.title = title;
11.this.genre = genre;
12.}
13.public String getTitle() {
14.return title;
15.}
16.public void setTitle(String title) {
17.this.title = title;
18.}
19.public String getGenre() {
20.return genre;
21.}
22.public void setGenre(String genre) {
23.this.genre = genre;
24.}
25.}

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.
01.<table class="songTable">
02.<tr class="even">
03.<td><b>Title</b></td>
04.<td><b>Genre</b></td>
05.</tr>
06.<s:iterator value="songs" status="songStatus">
07.<tr
08.class="<s:if test="#songStatus.odd == true ">odd</s:if><s:else>even</s:else>">
09.<td><s:property value="title" /></td>
10.<td><s:property value="genre" /></td>
11.</tr>
12.</s:iterator>
13.</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.
NameReturn TypeDescription
indexintzero-based index value.
countintindex + 1
firstbooleanreturns true if it is the first element
lastbooleanreturns true if it is the last element
evenbooleanreturns true if the count is an even number.
oddbooleanreturns true if the count is an odd number.
modulusinttakes 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.
1.<s:iterator value="songs" status="songStatus">
2.<tr
3.class="<s:if test="#songStatus.odd == true ">odd</s:if><s:else>even</s:else>">
4.<td><s:property value="title" /></td>
5.<td><s:property value="genre" /></td>
6.</tr>
7.</s:iterator>
The elseif tag is also available. You can use it if there are multiple conditions to check.

No comments:

Post a Comment