1 18 19 package org.apache.struts.taglib.tiles.util; 20 21 import java.lang.reflect.InvocationTargetException ; 22 23 import javax.servlet.jsp.JspException ; 24 import javax.servlet.jsp.PageContext ; 25 26 import org.apache.commons.beanutils.PropertyUtils; 27 import org.apache.struts.Globals; 28 import org.apache.struts.taglib.tiles.ComponentConstants; 29 import org.apache.struts.tiles.ComponentContext; 30 import org.apache.struts.tiles.ComponentDefinition; 31 import org.apache.struts.tiles.DefinitionsFactoryException; 32 import org.apache.struts.tiles.FactoryNotFoundException; 33 import org.apache.struts.tiles.NoSuchDefinitionException; 34 import org.apache.struts.tiles.TilesUtil; 35 36 43 public class TagUtils { 44 45 46 public static final boolean debug = true; 47 48 55 public static int getScope(String scopeName, int defaultValue) throws JspException { 56 if (scopeName == null) { 57 return defaultValue; 58 } 59 60 if (scopeName.equalsIgnoreCase("component")) { 61 return ComponentConstants.COMPONENT_SCOPE; 62 63 } else if (scopeName.equalsIgnoreCase("template")) { 64 return ComponentConstants.COMPONENT_SCOPE; 65 66 } else if (scopeName.equalsIgnoreCase("tile")) { 67 return ComponentConstants.COMPONENT_SCOPE; 68 69 } else { 70 return org.apache.struts.taglib.TagUtils.getInstance().getScope( 71 scopeName); 72 } 73 } 74 75 93 public static Object getProperty(Object bean, String name) 94 throws 95 IllegalAccessException , 96 InvocationTargetException , 97 NoSuchMethodException { 98 99 return PropertyUtils.getProperty(bean, name); 100 } 101 102 113 public static Object retrieveBean(String beanName, String scopeName, PageContext pageContext) 114 throws JspException { 115 116 if (scopeName == null) { 117 return findAttribute(beanName, pageContext); 118 } 119 120 int scope = getScope(scopeName, PageContext.PAGE_SCOPE); 122 123 return getAttribute(beanName, scope, pageContext); 125 } 126 127 134 public static Object findAttribute(String beanName, PageContext pageContext) { 135 ComponentContext compContext = ComponentContext.getContext(pageContext.getRequest()); 136 137 if (compContext != null) { 138 Object attribute = compContext.findAttribute(beanName, pageContext); 139 if (attribute != null) { 140 return attribute; 141 } 142 } 143 144 return pageContext.findAttribute(beanName); 146 } 147 148 156 public static Object getAttribute(String beanName, int scope, PageContext pageContext) { 157 if (scope == ComponentConstants.COMPONENT_SCOPE) { 158 ComponentContext compContext = ComponentContext.getContext(pageContext.getRequest()); 159 return compContext.getAttribute(beanName); 160 } 161 return pageContext.getAttribute(beanName, scope); 162 } 163 164 181 public static Object getRealValueFromBean( 182 String beanName, 183 String beanProperty, 184 String beanScope, 185 PageContext pageContext) 186 throws JspException { 187 188 try { 189 Object realValue; 190 Object bean = retrieveBean(beanName, beanScope, pageContext); 191 if (bean != null && beanProperty != null) { 192 realValue = PropertyUtils.getProperty(bean, beanProperty); 193 } else { 194 realValue = bean; } 196 return realValue; 197 198 } catch (NoSuchMethodException ex) { 199 throw new JspException ( 200 "Error - component.PutAttributeTag : Error while retrieving value from bean '" 201 + beanName 202 + "' with property '" 203 + beanProperty 204 + "' in scope '" 205 + beanScope 206 + "'. (exception : " 207 + ex.getMessage()); 208 209 } catch (InvocationTargetException ex) { 210 throw new JspException ( 211 "Error - component.PutAttributeTag : Error while retrieving value from bean '" 212 + beanName 213 + "' with property '" 214 + beanProperty 215 + "' in scope '" 216 + beanScope 217 + "'. (exception : " 218 + ex.getMessage()); 219 220 } catch (IllegalAccessException ex) { 221 throw new JspException ( 222 "Error - component.PutAttributeTag : Error while retrieving value from bean '" 223 + beanName 224 + "' with property '" 225 + beanProperty 226 + "' in scope '" 227 + beanScope 228 + "'. (exception : " 229 + ex.getMessage()); 230 } 231 } 232 233 245 public static void setAttribute( 246 PageContext pageContext, 247 String name, 248 Object value, 249 String scope) 250 throws JspException { 251 252 if (scope == null) 253 pageContext.setAttribute(name, value, PageContext.REQUEST_SCOPE); 254 else if (scope.equalsIgnoreCase("page")) 255 pageContext.setAttribute(name, value, PageContext.PAGE_SCOPE); 256 else if (scope.equalsIgnoreCase("request")) 257 pageContext.setAttribute(name, value, PageContext.REQUEST_SCOPE); 258 else if (scope.equalsIgnoreCase("session")) 259 pageContext.setAttribute(name, value, PageContext.SESSION_SCOPE); 260 else if (scope.equalsIgnoreCase("application")) 261 pageContext.setAttribute(name, value, PageContext.APPLICATION_SCOPE); 262 else { 263 throw new JspException ("Error - bad scope name '" + scope + "'"); 264 } 265 } 266 267 276 public static void setAttribute(PageContext pageContext, String name, Object beanValue) 277 throws JspException { 278 pageContext.setAttribute(name, beanValue, PageContext.REQUEST_SCOPE); 279 } 280 281 287 public static void saveException(PageContext pageContext, Throwable exception) { 288 pageContext.setAttribute(Globals.EXCEPTION_KEY, exception, PageContext.REQUEST_SCOPE); 289 } 290 291 297 public static ComponentDefinition getComponentDefinition(String name, PageContext pageContext) 298 throws JspException { 299 300 try { 301 return TilesUtil.getDefinition( 302 name, 303 pageContext.getRequest(), 304 pageContext.getServletContext()); 305 306 } catch (NoSuchDefinitionException ex) { 307 throw new JspException ( 308 "Error : Can't get component definition for '" 309 + name 310 + "'. Check if this name exist in component definitions."); 311 } catch (FactoryNotFoundException ex) { throw new JspException (ex.getMessage()); 313 314 } catch (DefinitionsFactoryException ex) { 315 if (debug) 316 ex.printStackTrace(); 317 saveException(pageContext, ex); 319 throw new JspException (ex.getMessage()); 320 } 321 } 322 323 } 324 | Popular Tags |