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. Coding
  3. Frameworks
  4. Struts 2 Bean Tag Tutorial

Struts 2 Bean Tag Tutorial

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

Join the DZone community and get the full member experience.

Join For Free

We will see how the bean tag works using a currency converter example. In this example we will convert dollars to rupees. We do this using the CurrencyConverter JavaBeans class.

The CurrencyConverter class.

package vaannila;

public class CurrencyConverter {

    private float rupees;
    private float dollars;

    public float getRupees() {
            return dollars * 50;
    }
    public void setRupees(float rupees) {
            this.rupees = rupees;
    }
    public float getDollars() {
            return rupees/50 ;
    }
    public void setDollars(float dollars) {
            this.dollars = dollars;
    }

}

The next step is to create an instance of the CurrencyConverter bean in the jsp page using the bean tag. We can either use the bean tag to push the value onto the ValueStack or we can set a top-level reference to it in the ActionContext. Let's see one by one.

First we will see how we can do this by pushing the value onto the ValueStack. The index.jsp page contains the following code.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Bean Tag Example</title>
</head>
<body>
<s:bean name="vaannila.CurrencyConverter">
	<s:param name="dollars" value="100" />
	100 Dollars = <s:property value="rupees" /> Rupees
</s:bean>

</body>
</html>

The name attribute of the bean tag hold the fully qualified class name of the JavaBean. The param tag is used to set the dollar value. We now use the property tag to retrive the equivalent value in rupees.

The CurrencyConverter bean will resides on the ValueStack till the duration of the bean tag. So its important that we use the property tag within the bean tag.

When you execute the example the following page is displayed.

Now we will see how we can do the same by creating a top-level reference to the bean in the ActionContext. We do this using the following code.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Bean Tag Example</title>
</head>
<body>
<s:bean name="vaannila.CurrencyConverter" var="converter">
	<s:param name="dollars" value="100"></s:param>
</s:bean>
100 Dollars = <s:property value="#converter.rupees" /> Rupees

</body>
</html>

To create an instance of the bean in the ActionContext we need to specify the instance name using the var attribute of the bean tag. Here our instance name is converter. Once we do this we can access the bean values outside the bean tag. Since the value is in the ActionContext we use the # operator to refer it.

You can try the Struts 2 Bean Tag Example by clicking the Download link below.

Source :Download
Source + Lib :Download
Bean (software) Spring Framework

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Steel Threads Are a Technique That Will Make You a Better Engineer
  • Integrate AWS Secrets Manager in Spring Boot Application
  • Demystifying the Infrastructure as Code Landscape
  • Strategies for Kubernetes Cluster Administrators: Understanding Pod Scheduling

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: