Meyyappan has posted 101 posts at DZone. View Full User Profile

Struts 2 Bean Tag Tutorial

06.14.2012
| 20573 views |
  • submit to reddit

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
Published at DZone with permission of its author, Meyyappan Muthuraman.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Tom Cat replied on Tue, 2012/09/18 - 9:24pm

oh yeah

Ratnesh Singh replied on Fri, 2012/11/16 - 7:36am

Very good explanation

Darko Marković replied on Tue, 2013/10/08 - 10:59am

There is a mistake here, rupees have the same value as dollars, as concluded by the Game Grumps (but it's a secret to everybody). All jokes aside, this was a thorough explanation of an excellent example of using Beans. I'm just going to add the option of typing in the value and picking what to convert, because you seem to already have that in your set methods in your converter class.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.