1 16 package org.apache.taglibs.jsp12; 17 18 import java.beans.BeanInfo ; 19 import java.beans.Introspector ; 20 import java.beans.IntrospectionException ; 21 import java.beans.PropertyDescriptor ; 22 23 import java.io.IOException ; 24 25 import java.lang.reflect.InvocationTargetException ; 26 import java.lang.reflect.Method ; 27 28 import javax.servlet.jsp.tagext.TagSupport ; 29 30 35 36 public class GetPropertyTag extends TagSupport { 37 38 String _name = null; 39 String _property = null; 40 41 public void setName(String name) { 42 _name = name; 43 } 44 45 public void setProperty(String property) { 46 _property = property; 47 } 48 49 public int doStartTag() { 50 Object o = pageContext.getAttribute(_name); 51 52 if (o == null) { 53 return EVAL_BODY_INCLUDE; 54 } 55 56 Class cls = o.getClass(); 58 BeanInfo beanInfo; 59 try { 60 beanInfo = Introspector.getBeanInfo(cls); 61 } catch (IntrospectionException e) { 62 e.printStackTrace(); 63 return EVAL_BODY_INCLUDE; 64 } 65 66 PropertyDescriptor descriptors[] = beanInfo.getPropertyDescriptors(); 67 Method getterMethod = null; 68 for (int i = 0; i < descriptors.length; ++i) { 69 if (descriptors[i].getName().equals(_property)) { 70 getterMethod = descriptors[i].getReadMethod(); 71 } 72 } 73 74 if (getterMethod != null) { 75 try { 76 pageContext.getOut().print(getterMethod.invoke(o, new Object [0])); 77 } catch (IllegalAccessException e) { 78 e.printStackTrace(); 79 } catch (InvocationTargetException e) { 80 e.printStackTrace(); 81 } catch (IOException e) { 82 e.printStackTrace(); 83 } 84 } 85 return EVAL_BODY_INCLUDE; 86 } 87 88 public void release() { 89 _name = null; 90 _property = null; 91 } 92 } 93 | Popular Tags |