Fork me on GitHub
Edit on GitHub << back to Tag Reference

bean

Please make sure you have read the Tag Syntax document and understand how tag attribute syntax works.

Description

Instantiates a class that conforms to the JavaBeans specification. This tag has a body which can contain a number of param elements to set any mutator methods on that class.

If the var attribute is set on the BeanTag, it will place the instantiated bean into the stack’s Context.

Instantiate a JavaBean and place it in the context

Attributes

Dynamic Attributes Allowed:

false

Name

Required

Default

Evaluated

Type

Description

name true false String The class name of the bean to be instantiated (must respect JavaBean specification)
performClearTagStateForTagPoolingServers false false false Boolean Whether to clear all tag state during doEndTag() processing (if applicable)
var false false String Name used to reference the value pushed into the Value Stack (scope: action).

Examples

in freemarker form

 [@s.bean name="org.apache.struts2.example.counter.SimpleCounter" var="counter"]
   [s:param name="foo" value="BAR"/]
   The value of foo is : [s:property value="foo"/], when inside the bean tag.
 [/s:bean]

in jsp form

 <s:bean name="org.apache.struts2.example.counter.SimpleCounter" var="counter">
   <s:param name="foo" value="BAR" />
   The value of foot is : <s:property value="foo"/>, when inside the bean tag <br />
 </s:bean>

This example instantiates a bean called SimpleCounter and sets the foo property (setFoo('BAR')). The SimpleCounter object is then pushed onto the ValueStack, which means that we can call its accessor methods (getFoo()) with the property tag and get their values.

In the above example, the id has been set to a value of counter. This means that the SimpleCounter class will be placed into the stack’s context. You can access the SimpleCounter class using a Struts tag:

jsp form

 <s:property value="#counter" />

freemarker form

 [s:property value="#counter.foo"/]

In the property tag example, the # tells Ognl to search the context for the SimpleCounter class which has an id(key) of counter.