FileDocCategorySizeDatePackage
Bean.javaAPI DocExample5649Mon Jul 23 13:26:36 BST 2007org.apache.struts2.components

Bean

public class Bean extends Component

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

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

  • id - the stack's context id (if supplied) that the created bean will be store under
  • name* - the class name of the bean to be instantiated (must respect JavaBean specification)

Examples:


<-- in freemarker form -->
[@s.bean name="org.apache.struts2.example.counter.SimpleCounter" id="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" id="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

see
Param

Fields Summary
protected static Log
log
protected Object
bean
protected String
name
protected com.opensymphony.xwork2.ObjectFactory
objectFactory
Constructors Summary
public Bean(com.opensymphony.xwork2.util.ValueStack stack)


       
        super(stack);
    
Methods Summary
public voidaddParameter(java.lang.String key, java.lang.Object value)

        OgnlUtil.setProperty(key, value, bean, getStack().getContext());
    
public booleanend(java.io.Writer writer, java.lang.String body)

        ValueStack stack = getStack();
        stack.pop();

        return super.end(writer, body);
    
public voidsetName(java.lang.String name)

        this.name = name;
    
public voidsetObjectFactory(com.opensymphony.xwork2.ObjectFactory objectFactory)

        this.objectFactory = objectFactory;
    
public booleanstart(java.io.Writer writer)

        boolean result = super.start(writer);

        ValueStack stack = getStack();

        try {
            String beanName = findString(name, "name", "Bean name is required. Example: com.acme.FooBean");
            bean = objectFactory.buildBean(ClassLoaderUtil.loadClass(beanName, getClass()), stack.getContext());
        } catch (Exception e) {
            log.error("Could not instantiate bean", e);

            return false;
        }

        // push bean on stack
        stack.push(bean);

        // store for reference later
        if (getId() != null) {
            getStack().getContext().put(getId(), bean);
        }

        return result;