Struts 2 Data Tags Example
In this example you will learn how to use the property tag, the set tag and the push tag. These tags are part of the Struts 2 Data Tags. Before we see the syntax of each tag you need to know what an ActionContext and a ValueStack is.
- The ActionContext is a global storage area that holds all the data associated with the processing of a request.
- The ActionContext is thread local this makes the Struts 2 actions thread safe.
- The ValueStack is the part of the ActionContext. In Struts 2 actions resides on the ValueStack.
The property tag is used to retrive the data from the ValueStack or some other object in the ActionContext like application or seesion. Let's see how to display the following details using theproperty tag.
Our Action class AlbumInfoAction contains the following piece of code.
01.
package
vaannila;
02.
03.
public
class
AlbumInfoAction{
04.
05.
private
String title;
06.
private
Artist artist;
07.
08.
public
String populate()
09.
{
10.
title =
"Thriller"
;
11.
artist =
new
Artist(
"Michael Jackson"
,
"King of pop"
);
12.
return
"populate"
;
13.
}
14.
15.
public
String execute()
16.
{
17.
return
"success"
;
18.
}
19.
20.
public
String getTitle() {
21.
return
title;
22.
}
23.
public
void
setTitle(String title) {
24.
this
.title = title;
25.
}
26.
public
Artist getArtist() {
27.
return
artist;
28.
}
29.
public
void
setArtist(Artist artist) {
30.
this
.artist = artist;
31.
}
32.
33.
}
Struts 2 Property Tag Example
Our Artist data class contains the following code.
01.
package
vaannila;
02.
03.
public
class
Artist {
04.
05.
private
String name;
06.
private
String bio;
07.
08.
Artist(String name, String bio)
09.
{
10.
this
.name = name;
11.
this
.bio = bio;
12.
}
13.
public
String getName() {
14.
return
name;
15.
}
16.
public
void
setName(String name) {
17.
this
.name = name;
18.
}
19.
public
String getBio() {
20.
return
bio;
21.
}
22.
public
void
setBio(String bio) {
23.
this
.bio = bio;
24.
}
25.
26.
}
Let's see how we can access the action class attributes using the property tag in the jsp page. ThealbumDetails.jsp page contains the following code.
01.
<%@taglib uri="/struts-tags" prefix="s"%>
02.
<
html
>
03.
<
head
>
04.
<
s:head
/>
05.
<
style
type
=
"text/css"
>
06.
@import url(style.css);
07.
</
style
>
08.
<
title
>Album Details</
title
>
09.
</
head
>
10.
<
body
>
11.
<
div
class
=
"content"
>
12.
<
b
>Album Title :</
b
>
13.
<
s:property
value
=
"title"
/>
14.
15.
<
b
>Artist Name :</
b
>
16.
<
s:property
value
=
"artist.name"
/>
17.
18.
19.
<
b
>Artist Bio :</
b
>
20.
<
s:property
value
=
"artist.bio"
/>
21.
22.
23.
</
div
>
24.
</
body
>
25.
</
html
>
As you can see title is the property of the AlbumInfoAction so we can access it directly. But name andbio are properties of the Artist class so to access them we need to go one step deeper. We need to use a second-level OGNL expression to access them.
Struts 2 Set Tag Example
The set tag is used to assign a property value to another name. This helps in accessing the property in a faster and easier way. To access the artist name we need to go one level deeper and fetch it when we used the property tag, instead you can assign the value to another property in theActionContext and access it directly. The following code shows how to do this.
1.
<
s:set
name
=
"artistName"
value
=
"artist.name"
/>
2.
<
s:set
name
=
"artistBio"
value
=
"artist.bio"
/>
3.
<
b
>Album Title :</
b
> <
s:property
value
=
"title"
/>
4.
5.
<
b
>Artist Name :</
b
> <
s:property
value
=
"#artistName"
/>
6.
7.
<
b
>Artist Bio :</
b
> <
s:property
value
=
"#artistBio"
/>
The property artistName and artistBio will now be stored in the ActionContext. To refer then you need to use the following syntax #objectName.
You can also place the property value in the session map in the following way. Now the value artistName and artistBio will persist throughout the session.
1.
<
s:set
name
=
"artistName"
value
=
"artist.name"
scope
=
"session"
/>
2.
<
s:set
name
=
"artistBio"
value
=
"artist.bio"
scope
=
"session"
/>
3.
<
b
>Album Title :</
b
> <
s:property
value
=
"title"
/>
4.
5.
<
b
>Artist Name :</
b
> <
s:property
value
=
"#session['artistName']"
/>
6.
7.
<
b
>Artist Bio :</
b
> <
s:property
value
=
"#session['artistBio']"
/>
In the same way you can also store the values in other maps avaliable in the ActionContext.
Struts 2 Push Tag Example
You can push a value into the ValueStack using the push tag. The value we pushed using push tag will be on top of the ValueStack, so it can be easily referenced using the first-level OGNL expression instead of a deeper reference. The following code show how to do this.
1.
<
b
>Album Title :</
b
> <
s:property
value
=
"title"
/>
2.
3.
<
s:push
value
=
"artist"
>
4.
<
b
>Artist Name :</
b
> <
s:property
value
=
"name"
/>
5.
6.
<
b
>Artist Bio :</
b
> <
s:property
value
=
"bio"
/>
7.
8.
</
s:push
>
No comments:
Post a Comment