Struts 2 Bean Tag Example

In Struts 2, the  “bean” tag is used to create a Java class and place it in the stack’s Context.

In this lesson, we will use the “bean” tag to instantiate a Java object inside a JSP page, and then display the content of this object in the same page.


 1. Project structure



2. struts.xml

We simply send the user to the index page.


3. Action class

Here is our action class:


4. Bean class

We create a simple Java class named Page, with one attribute name.


5. JSP page

We create a JSP page, in which we use the “bean” tag to instantiate the Page object by setting the name property.

The var parameter of the bean tag is used to push the Page object into the stack’s context.

In the end of the JSP page, We display the value of the name property by calling the reference of the Page object.

File : index.jsp


6. Test It

Open the browser and type in : http://localhost:8080/Struts2BeanTagExample/page.action



Download source code