1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 * <-- in freemarker form -->
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 * <-- in jsp form -->
60 * <s:bean name="org.apache.struts2.example.counter.SimpleCounter" id="counter">
61 * <s:param name="foo" value="BAR" />
62 * The value of foot is : <s:property value="foo"/>, when inside the bean tag <br />
63 * </s:bean>
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 * <-- jsp form -->
78 * <s:property value="#counter" />
79 *
80 * <-- freemarker form -->
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
118 stack.push(bean);
119
120
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 }