Over a million developers have joined DZone.

Struts 2 Data Tags Tutorial

DZone's Guide to

Struts 2 Data Tags Tutorial

· ·
Free Resource

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

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 the property tag.

Our Action class AlbumInfoAction contains the following piece of code.

package vaannila;

public class AlbumInfoAction{

    private String title;
    private Artist artist;

    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;



Struts 2 Property Tag Example

Our Artist data class contains the following code.

package vaannila;

public class Artist {

    private String name;
    private String bio;

    Artist(String name, String bio)
        this.name = name;
        this.bio = bio;
    public String getName() {
        return name;
    public void setName(String name) {
        this.name = name;
    public String getBio() {
        return bio;
    public void setBio(String bio) {
        this.bio = bio;


Let's see how we can access the action class attributes using the property tag in the jsp page. The albumDetails.jsp page contains the following code.

<%@taglib uri="/struts-tags" prefix="s"%>
<s:head />
<style type="text/css">
@import url(style.css);
<title>Album Details</title>
<div class="content">
    <b>Album Title :</b>
    <s:property value="title" /> <br>
    <b>Artist Name :</b>
    <s:property value="artist.name" />
    <b>Artist Bio :</b>
    <s:property value="artist.bio" />

As you can see title is the property of the AlbumInfoAction so we can access it directly. But name and bio 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 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.


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.

<b>Album Title :</b> <s:property value="title" /> <br>
<s:push value="artist">
<b>Artist Name :</b> <s:property value="name" /> <br>
<b>Artist Bio :</b> <s:property value="bio" /> <br>

You can download and try the example by clicking the Download link below.

Source : Download
Source + Lib : Download

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}