1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts2.components;
23
24 import java.io.Writer;
25
26 import org.apache.struts2.views.annotations.StrutsTag;
27 import org.apache.struts2.views.annotations.StrutsTagAttribute;
28
29 import com.opensymphony.xwork2.ObjectFactory;
30 import com.opensymphony.xwork2.inject.Inject;
31 import com.opensymphony.xwork2.util.ClassLoaderUtil;
32 import com.opensymphony.xwork2.util.ValueStack;
33 import com.opensymphony.xwork2.util.logging.Logger;
34 import com.opensymphony.xwork2.util.logging.LoggerFactory;
35 import com.opensymphony.xwork2.util.reflection.ReflectionProvider;
36
37 /***
38 * <!-- START SNIPPET: javadoc -->
39 * <p>Instantiates a class that conforms to the JavaBeans specification. This tag has a body which can contain
40 * a number of {@link Param} elements to set any mutator methods on that class.</p>
41 * <p/>
42 * <p>If the var attribute is set on the BeanTag, it will place the instantiated bean into the
43 * stack's Context.</p>
44 * <p/>
45 * <!-- END SNIPPET: javadoc -->
46 *
47 *
48 * <!-- START SNIPPET: params -->
49 * <ul>
50 * <li>var - the stack's context name (if supplied) that the created bean will be store under</li>
51 * <li>name* - the class name of the bean to be instantiated (must respect JavaBean specification)</li>
52 * </ul>
53 * <!-- END SNIPPET: params -->
54 *
55 *
56 * <p>Examples:</p>
57 * <p/>
58 * <pre>
59 * <!-- START SNIPPET: examples -->
60 * <-- in freemarker form -->
61 * [@s.bean name="org.apache.struts2.example.counter.SimpleCounter" var="counter"]
62 * [s:param name="foo" value="BAR"/]
63 * The value of foo is : [s:property value="foo"/], when inside the bean tag.<br />
64 * [/s:bean]
65 *
66 * <-- in jsp form -->
67 * <s:bean name="org.apache.struts2.example.counter.SimpleCounter" var="counter">
68 * <s:param name="foo" value="BAR" />
69 * The value of foot is : <s:property value="foo"/>, when inside the bean tag <br />
70 * </s:bean>
71 * <!-- END SNIPPET: examples -->
72 * </pre>
73 * <p/>
74 *
75 * <!-- START SNIPPET: examplesdescription -->
76 * <p>This example instantiates a bean called SimpleCounter and sets the foo property (setFoo('BAR')). The
77 * SimpleCounter object is then pushed onto the Valuestack, which means that we can call its accessor methods (getFoo())
78 * with the Property tag and get their values.</p>
79 * <p/>
80 * <p>In the above example, the id has been set to a value of <i>counter</i>. This means that the SimpleCounter class
81 * will be placed into the stack's context. You can access the SimpleCounter class using a Struts tag:</p>
82 * <p/>
83 * <pre>
84 * <-- jsp form -->
85 * <s:property value="#counter" />
86 *
87 * <-- freemarker form -->
88 * [s:property value="#counter.foo"/]
89 * </pre>
90 * <p/>
91 * <p>In the property tag example, the <i>#</i> tells Ognl to search the context for the SimpleCounter class which has
92 * an id(key) of <i>counter</i></p>
93 * <!-- END SNIPPET: examplesdescription -->
94 *
95 * @see Param
96 */
97 @StrutsTag(name="bean", tldTagClass="org.apache.struts2.views.jsp.BeanTag",
98 description="Instantiate a JavaBean and place it in the context")
99 public class Bean extends ContextBean {
100 protected static final Logger LOG = LoggerFactory.getLogger(Bean.class);
101
102 protected Object bean;
103 protected String name;
104 protected ObjectFactory objectFactory;
105 protected ReflectionProvider reflectionProvider;
106
107 public Bean(ValueStack stack) {
108 super(stack);
109 }
110
111 @Inject
112 public void setObjectFactory(ObjectFactory objectFactory) {
113 this.objectFactory = objectFactory;
114 }
115
116 @Inject
117 public void setReflectionProvider(ReflectionProvider prov) {
118 this.reflectionProvider = prov;
119 }
120
121 public boolean start(Writer writer) {
122 boolean result = super.start(writer);
123
124 ValueStack stack = getStack();
125
126 try {
127 String beanName = findString(name, "name", "Bean name is required. Example: com.acme.FooBean");
128 bean = objectFactory.buildBean(ClassLoaderUtil.loadClass(beanName, getClass()), stack.getContext());
129 } catch (Exception e) {
130 LOG.error("Could not instantiate bean", e);
131
132 return false;
133 }
134
135
136 stack.push(bean);
137
138
139 putInContext(bean);
140
141 return result;
142 }
143
144 public boolean end(Writer writer, String body) {
145 ValueStack stack = getStack();
146 stack.pop();
147
148 return super.end(writer, body);
149 }
150
151 public void addParameter(String key, Object value) {
152 reflectionProvider.setProperty(key, value, bean, getStack().getContext());
153 }
154
155 @StrutsTagAttribute(description="The class name of the bean to be instantiated (must respect JavaBean specification)",
156 required=true)
157 public void setName(String name) {
158 this.name = name;
159 }
160 }