View Javadoc

1   /*
2    * $Id: Bean.java 454565 2006-10-10 00:02:56Z jmitchell $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.components;
19  
20  import java.io.Writer;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  
25  import com.opensymphony.xwork2.util.ClassLoaderUtil;
26  import com.opensymphony.xwork2.ObjectFactory;
27  import com.opensymphony.xwork2.util.OgnlUtil;
28  import com.opensymphony.xwork2.util.ValueStack;
29  
30  /***
31   * <!-- START SNIPPET: javadoc -->
32   * <p>Instantiates a class that conforms to the JavaBeans specification. This tag has a body which can contain
33   * a number of {@link Param} elements to set any mutator methods on that class.</p>
34   * <p/>
35   * <p>If the id attribute is set on the BeanTag, it will place the instantiated bean into the
36   * stack's Context.</p>
37   * <p/>
38   * <!-- END SNIPPET: javadoc -->
39   *
40   *
41   * <!-- START SNIPPET: params -->
42   * <ul>
43   * 		<li>id - the stack's context id (if supplied) that the created bean will be store under</li>
44   * 		<li>name* - the class name of the bean to be instantiated (must respect JavaBean specification)</li>
45   * </ul>
46   * <!-- END SNIPPET: params -->
47   *
48   *
49   * <p>Examples:</p>
50   * <p/>
51   * <pre>
52   * <!-- START SNIPPET: examples -->
53   * &lt;-- in freemarker form --&gt;
54   * [@s.bean name="org.apache.struts2.example.counter.SimpleCounter" id="counter"]
55   *   [s:param name="foo" value="BAR"/]
56   *   The value of foo is : [s:property value="foo"/], when inside the bean tag.<br />
57   * [/s:bean]
58   *
59   * &lt;-- in jsp form --&gt;
60   * &lt;s:bean name="org.apache.struts2.example.counter.SimpleCounter" id="counter"&gt;
61   * 	 &lt;s:param name="foo" value="BAR" /&gt;
62   *   The value of foot is : &lt;s:property value="foo"/&gt;, when inside the bean tag &lt;br /&gt;
63   * &lt;/s:bean&gt;
64   * <!-- END SNIPPET: examples -->
65   * </pre>
66   * <p/>
67   *
68   * <!-- START SNIPPET: examplesdescription -->
69   * <p>This example instantiates a bean called SimpleCounter and sets the foo property (setFoo('BAR')). The
70   * SimpleCounter object is then pushed onto the Valuestack, which means that we can called its accessor methods (getFoo())
71   * with the Property tag and get their values.</p>
72   * <p/>
73   * <p>In the above example, the id has been set to a value of <i>counter</i>. This means that the SimpleCounter class
74   * will be placed into the stack's context. You can access the SimpleCounter class using a Struts tag:</p>
75   * <p/>
76   * <pre>
77   * &lt;-- jsp form --&gt;
78   * &lt;s:property value="#counter" /&gt;
79   *
80   * &lt;-- freemarker form --&gt;
81   * [s:property value="#counter.foo"/]
82   * </pre>
83   * <p/>
84   * <p>In the property tag example, the <i>#</i> tells Ognl to search the context for the SimpleCounter class which has
85   * an id(key) of <i>counter</i></p>
86   * <!-- END SNIPPET: examplesdescription -->
87   *
88   * @see Param
89   *
90   * @s.tag name="bean" tld-body-content="JSP" tld-tag-class="org.apache.struts2.views.jsp.BeanTag"
91   * description="Instantiate a JavaBean and place it in the context."
92   */
93  public class Bean extends Component {
94      protected static Log log = LogFactory.getLog(Bean.class);
95  
96      protected Object bean;
97      protected String name;
98  
99      public Bean(ValueStack stack) {
100         super(stack);
101     }
102 
103     public boolean start(Writer writer) {
104         boolean result = super.start(writer);
105 
106         ValueStack stack = getStack();
107 
108         try {
109             String beanName = findString(name, "name", "Bean name is required. Example: com.acme.FooBean");
110             bean = ObjectFactory.getObjectFactory().buildBean(ClassLoaderUtil.loadClass(beanName, getClass()), stack.getContext());
111         } catch (Exception e) {
112             log.error("Could not instantiate bean", e);
113 
114             return false;
115         }
116 
117         // push bean on stack
118         stack.push(bean);
119 
120         // store for reference later
121         if (getId() != null) {
122             getStack().getContext().put(getId(), bean);
123         }
124 
125         return result;
126     }
127 
128     public boolean end(Writer writer, String body) {
129         ValueStack stack = getStack();
130         stack.pop();
131 
132         return super.end(writer, body);
133     }
134 
135     public void addParameter(String key, Object value) {
136         OgnlUtil.setProperty(key, value, bean, getStack().getContext());
137     }
138 
139     /***
140      * the class name of the bean to be instantiated (must respect JavaBean specification)
141      * @s.tagattribute required="true" type="String"
142      */
143     public void setName(String name) {
144         this.name = name;
145     }
146 }