KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > servlet > tags > basic > GetPropertyTag


1 package jodd.servlet.tags.basic;
2
3 import java.io.IOException;
4
5 import javax.servlet.http.HttpServletRequest;
6 import javax.servlet.jsp.JspWriter;
7 import javax.servlet.jsp.tagext.TagSupport;
8
9 import jodd.bean.BeanUtil;
10 import jodd.util.StringUtil;
11
12 /**
13  * Reads property from an java bean object stored in specified scope. Bean
14  * object is retrieved by name and then property value is read from it.
15  */

16 public class GetPropertyTag extends TagSupport {
17
18     private String beanName = null;
19     /**
20      * Sets the bean name.
21      *
22      * @param v
23      */

24     public void setBean(String v) {
25         beanName = v;
26     }
27     /**
28      * Returns bean name.
29      *
30      * @return bean name
31      */

32     protected String getBean() {
33         return beanName;
34     }
35
36     private String name = "";
37     /**
38      * Sets the property name.
39      *
40      * @param v
41      */

42     public void setName(String v) {
43         name = v;
44     }
45     /**
46      * Returns bean parameter name.
47      *
48      * @return bean parameter name
49      */

50     protected String getName() {
51         return name;
52     }
53
54     private String scope = "page";
55     /**
56      * Sets the bean scope. May be one of the following:<br>
57      * request<br>
58      * session<br>
59      * page (i.e. pageScope, which is also default scope)<br>
60      *
61      * @param v
62      */

63     public void setScope(String v) {
64         scope = v;
65     }
66
67     /**
68      * Get the bean object from the required scope.
69      *
70      * @return bean object
71      */

72     protected Object getBeanObject() {
73         Object bean = null;
74         if (scope.equals("page")) {
75             bean = pageContext.getAttribute(beanName);
76         } else if (scope.equals("request")) {
77             bean = ((HttpServletRequest)pageContext.getRequest()).getAttribute(beanName);
78         } else if (scope.equals("session")) {
79             bean = ((HttpServletRequest)pageContext.getRequest()).getSession().getAttribute(beanName);
80         }
81         return bean;
82     }
83
84     public int doStartTag() {
85         try {
86             JspWriter out = pageContext.getOut();
87             Object bean = getBeanObject();
88             if (bean != null) {
89                 out.print(StringUtil.toString(BeanUtil.getProperty(bean, name)));
90             }
91         } catch (IOException ioe) {
92         }
93         return SKIP_BODY;
94     }
95 }
96
Popular Tags