KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > views > jsp > BeanTag


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.views.jsp;
6
7 import com.opensymphony.xwork.ObjectFactory;
8 import com.opensymphony.xwork.util.OgnlUtil;
9 import com.opensymphony.xwork.util.OgnlValueStack;
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12
13 import javax.servlet.jsp.JspException JavaDoc;
14 import java.util.Map JavaDoc;
15
16
17 /**
18  * <p>BeanTag will instantiate a class that conforms to the JavaBeans specification. This tag has a body which can contain
19  * a number of ParamTag {@link ParamTag} elements to set any mutator methods on that class.</p>
20  * <p>If the id attribute is set on the BeanTag, it will place the instantiated bean into the PageContext and the
21  * ActionContext.</p>
22  * <p>Examples:</p>
23  * <pre>
24  * <ww:bean name="'com.opensymphony.webwork.example.counter.SimpleCounter'" id="counter">
25  * <ww:param name="'foo'" value="'BAR'"/>
26  * <p/>
27  * The value of foo is : <ww:property value="foo"/>, when inside the bean tag.<br />
28  * </ww:bean>
29  * </pre>
30  * <p>This example instantiates a bean called SimpleCounter and sets the foo property (setFoo('BAR')). The
31  * SimpleCounter object is then pushed onto the Valuestack, which means that we can called its accessor methods (getFoo())
32  * with the Property tag and get their values.</p>
33  * <p>In the above example, the id has been set to a value of <i>counter</i>. This means that the SimpleCounter class
34  * will be placed into the PageContext and ActionContext. You can access theah SimpleCounter class using JSTL:</p>
35  * <pre>
36  * <c:out value="${counter.foo}"/>
37  * </pre>
38  * <p> or using the webwork Property tag:</p>
39  * <pre>
40  * <ww:property value="#counter.foo"/>
41  * </pre>
42  * <p>In the property tag example, the <i>#</i> tells Ognl to search the context for the SimpleCounter class which has
43  * an id(key) of <i>counter</i></p>
44  *
45  * @author $author$
46  * @author Rick Salsa (rsal@mb.sympatico.ca)
47  * @author Brock Bulger
48  * @version $Revision: 1.14 $
49  */

50 public class BeanTag extends WebWorkTagSupport implements ParamTag.Parametric {
51     //~ Static fields/initializers /////////////////////////////////////////////
52

53     protected static Log log = LogFactory.getLog(BeanTag.class);
54
55     //~ Instance fields ////////////////////////////////////////////////////////
56

57     protected Object JavaDoc bean;
58     protected String JavaDoc name;
59
60     //~ Methods ////////////////////////////////////////////////////////////////
61

62     public void setName(String JavaDoc name) {
63         this.name = name;
64     }
65
66     public Map JavaDoc getParameters() {
67         return null;
68     }
69
70     public void addParameter(String JavaDoc key, Object JavaDoc value) {
71         OgnlUtil.setProperty(key, value, bean, getStack().getContext());
72     }
73
74     public int doEndTag() throws JspException JavaDoc {
75         OgnlValueStack stack = getStack();
76         stack.pop();
77
78         return SKIP_BODY;
79     }
80
81     public int doStartTag() throws JspException JavaDoc {
82         if (name == null) {
83             throw new JspException JavaDoc("Bean name must be specified.");
84         }
85
86         try {
87             bean = ObjectFactory.getObjectFactory().buildBean(Class.forName(findString(name)));
88         } catch (Exception JavaDoc e) {
89             log.error("Could not instantiate bean", e);
90
91             return SKIP_PAGE;
92         }
93
94         OgnlValueStack stack = getStack();
95
96         // push bean on stack
97
stack.push(bean);
98
99         // Store as attribute in page context and in the ActionContext for use with the property tag.
100
if (getId() != null) {
101             pageContext.setAttribute(getId(), bean);
102             getStack().getContext().put(getId(), bean);
103         }
104
105         return EVAL_BODY_INCLUDE;
106     }
107 }
108
Popular Tags