DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Data Engineering
  3. Data
  4. Struts 2 Data Tags Tutorial

Struts 2 Data Tags Tutorial

Meyyappan Muthuraman user avatar by
Meyyappan Muthuraman
·
Jun. 14, 12 · Tutorial
Like (0)
Save
Tweet
Share
58.07K Views

Join the DZone community and get the full member experience.

Join For Free

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"%>
<html>
<head>
<s:head />
<style type="text/css">
@import url(style.css);
</style>
<title>Album Details</title>
</head>
<body>
<div class="content">
    <b>Album Title :</b>
    <s:property value="title" /> <br>
    <b>Artist Name :</b>
    <s:property value="artist.name" />
    <br>
    <b>Artist Bio :</b>
    <s:property value="artist.bio" />
	<br>
</div>
</body>
</html>

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>
</s:push>

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

Source :Download
Source + Lib :Download
Data (computing) Property (programming)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Orchestration Pattern: Managing Distributed Transactions
  • Implementing PEG in Java
  • A Gentle Introduction to Kubernetes
  • Beyond Coding: The 5 Must-Have Skills to Have If You Want to Become a Senior Programmer

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: