Pages Navigation Menu

Coding is much easier than you think

Struts 2 Bean Tag

Struts 2 Bean Tag

The bean tag is like a hybrid of the set and push tags. The main difference is that you don’t need to work with an existing object. You can create an instance of an object and either push it onto the ValueStack or set a top-level reference to it in the Action-Context. By default, the object will be pushed onto the ValueStack and will remain there for the duration of the tag.
** UPDATE: Struts 2 Complete tutorial now available here.
In this tutorial let us learn how to use the Struts 2 Bean Tag with the help of a DollarConverter bean example. In this example we will convert dollars to rupees.

Download It – BeanTag

1.Bean Class
File name : DollarConverter .java

package com.simplecode.action;

public class DollarConverter  {

	private double rupees;
	private double dollars;

	public double getRupees() {
		return dollars * 55;

	public void setRupees(double rupees) {
		this.rupees = rupees;

	public double getDollars() {
		return rupees / 55;

	public void setDollars(double dollars) {
		this.dollars = dollars;

The next step is to create an instance of the DollarConverter bean in the jsp page using the bean tag.
We can use the bean tag in two ways.

  1. By pushing the value onto the ValueStack.
  2. By setting a top-level reference to it in the ActionContext.

Let’s implement both the methods one by one.
Method 1
Hear we will see how we can do this by pushing the value onto the ValueStack. We do this using the following code in jsp.

<h5>Method 1 - push the value onto the ValueStack</h5>
<s:bean name="com.simplecode.action.DollarConverter ">
<s:param name="dollars" value="100" />
100 Dollars = <s:property value="rupees" /> Rupees
<br />

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 DollarConverter 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.

Method 2
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.

<h5>Method 2 - Set a top-level reference to it in the ActionContext</h5>
<s:bean name="com.simplecode.action.DollarConverter " var="converter">
<s:param name="dollars" value="100"></s:param>
100 Dollars = <s:property value="#converter.rupees" /> Rupees

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.
4. Demo

Bean tag


  1. Struts 2 Bean Tag Tutorial –

About Mohaideen Jamil

I'm a Full stack developer of IT exp in J2EE, AngularJs, MicroServices, Docker, Spring (Boot, MVC, Cloud), Bluemix, DevOps. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.

%d bloggers like this: