Menu
Struts 2 > Struts 2 Set Tag Example

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 the ActionContext and access it directly. The following code shows how to do this.


<s:set name="artistName" value="artist.name" />
<s:set name="artistBio" value="artist.bio" />
<b>Album Title :</b> <s:property value="title" /> <br>
<b>Artist Name :</b> <s:property value="#artistName" /> <br>
<b>Artist Bio :</b> <s:property value="#artistBio" /> <br>

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.

<s:set name="artistName" value="artist.name" scope="session" />
<s:set name="artistBio" value="artist.bio" scope="session" />
<b>Album Title :</b> <s:property value="title" /> <br>
<b>Artist Name :</b> <s:property value="#session['artistName']" /> <br>
<b>Artist Bio :</b> <s:property value="#session['artistBio']" /> <br>


In the same way you can also store the values in other maps avaliable in the ActionContext.

Subscribe
Contact Us  |  Copyright © 2009 vaannila.com