001: package com.opensymphony.webwork.components;
002:
003: import com.opensymphony.util.ClassLoaderUtil;
004: import com.opensymphony.xwork.ObjectFactory;
005: import com.opensymphony.xwork.util.OgnlUtil;
006: import com.opensymphony.xwork.util.OgnlValueStack;
007: import org.apache.commons.logging.Log;
008: import org.apache.commons.logging.LogFactory;
009:
010: import java.io.Writer;
011:
012: /**
013: * <!-- START SNIPPET: javadoc -->
014: * <p>Instantiates a class that conforms to the JavaBeans specification. This tag has a body which can contain
015: * a number of {@link Param} elements to set any mutator methods on that class.</p>
016: * <p/>
017: * <p>If the id attribute is set on the BeanTag, it will place the instantiated bean into the
018: * stack's Context.</p>
019: * <p/>
020: * <!-- END SNIPPET: javadoc -->
021: *
022: *
023: * <!-- START SNIPPET: params -->
024: * <ul>
025: * <li>id - the stack's context id (if supplied) that the created bean will be store under</li>
026: * <li>name* - the class name of the bean to be instantiated (must respect JavaBean specification)</li>
027: * </ul>
028: * <!-- END SNIPPET: params -->
029: *
030: *
031: * <p>Examples:</p>
032: * <p/>
033: * <pre>
034: * <!-- START SNIPPET: examples -->
035: * <-- in freemarker form -->
036: * [ww.bean name="com.opensymphony.webwork.example.counter.SimpleCounter" id="counter"]
037: * [ww:param name="foo" value="BAR"/]
038: * The value of foo is : [ww:property value="foo"/], when inside the bean tag.<br />
039: * [/ww:bean]
040: *
041: * <-- in jsp form -->
042: * <ww:bean name="com.opensymphony.webwork.example.counter.SimpleCounter" id="counter">
043: * <ww:param name="foo" value="BAR" />
044: * The value of foot is : <ww:property value="foo"/>, when inside the bean tag <br />
045: * </ww:bean>
046: * <!-- END SNIPPET: examples -->
047: * </pre>
048: * <p/>
049: *
050: * <!-- START SNIPPET: examplesdescription -->
051: * <p>This example instantiates a bean called SimpleCounter and sets the foo property (setFoo('BAR')). The
052: * SimpleCounter object is then pushed onto the Valuestack, which means that we can called its accessor methods (getFoo())
053: * with the Property tag and get their values.</p>
054: * <p/>
055: * <p>In the above example, the id has been set to a value of <i>counter</i>. This means that the SimpleCounter class
056: * will be placed into the stack's context. You can access the SimpleCounter class using WW's tag:</p>
057: * <p/>
058: * <pre>
059: * <-- jsp form -->
060: * <ww:property value="#counter" />
061: *
062: * <-- freemarker form -->
063: * [ww:property value="#counter.foo"/]
064: * </pre>
065: * <p/>
066: * <p>In the property tag example, the <i>#</i> tells Ognl to search the context for the SimpleCounter class which has
067: * an id(key) of <i>counter</i></p>
068: * <!-- END SNIPPET: examplesdescription -->
069: *
070: * @author $author$
071: * @author Rick Salsa (rsal@mb.sympatico.ca)
072: * @author Brock Bulger
073: * @author Rene Gielen
074: * @version $Revision: 2468 $
075: * @since 2.2
076: *
077: * @see Param
078: *
079: * @ww.tag name="bean" tld-body-content="JSP" tld-tag-class="com.opensymphony.webwork.views.jsp.BeanTag"
080: * description="Instantiate a JavaBean and place it in the context."
081: */
082: public class Bean extends Component {
083: protected static Log log = LogFactory.getLog(Bean.class);
084:
085: protected Object bean;
086: protected String name;
087:
088: public Bean(OgnlValueStack stack) {
089: super (stack);
090: }
091:
092: public boolean start(Writer writer) {
093: boolean result = super .start(writer);
094:
095: OgnlValueStack stack = getStack();
096:
097: try {
098: String beanName = findString(name, "name",
099: "Bean name is required. Example: com.acme.FooBean");
100: bean = ObjectFactory.getObjectFactory().buildBean(
101: ClassLoaderUtil.loadClass(beanName, getClass()),
102: stack.getContext());
103: } catch (Exception e) {
104: log.error("Could not instantiate bean", e);
105:
106: return false;
107: }
108:
109: // push bean on stack
110: stack.push(bean);
111:
112: // store for reference later
113: if (getId() != null) {
114: getStack().getContext().put(getId(), bean);
115: }
116:
117: return result;
118: }
119:
120: public boolean end(Writer writer, String body) {
121: OgnlValueStack stack = getStack();
122: stack.pop();
123:
124: return super .end(writer, body);
125: }
126:
127: public void addParameter(String key, Object value) {
128: OgnlUtil.setProperty(key, value, bean, getStack().getContext());
129: }
130:
131: /**
132: * the class name of the bean to be instantiated (must respect JavaBean specification)
133: * @ww.tagattribute required="true" type="String"
134: */
135: public void setName(String name) {
136: this.name = name;
137: }
138: }
|