JavaScud | Forum | JIRA | Blog |
  Dashboard > WebWork2文档中文化计划 > ... > Cookbook > Describing a bean in velocity
  WebWork2文档中文化计划 Log In View a printable version of the current page.  
  Describing a bean in velocity
Added by scud, last edited by scud on Mar 30, 2006  (view change)
Labels: 
(None)

The follow snippet might be useful during debugging to list the properties inside an
arbitary bean. Or for handing to a UI developer that use unaware of the getters/setters inside an object.

## prints out the property names for a bean
#macro (describeBean $name) 
#set($bu = $webwork.bean("com.opensymphony.util.BeanUtils"))
	#foreach($propName in $bu.getPropertyNames($name))
		<li>$propName</li>
	#end
#end

i.e. assuming $obj is a PersonObject that has properties(firstName, lastName, and zip).

#describeBean($obj)

would print
<li>firstName</li>
<li>lastName</li>
<li>zip</li>

One might also expand upon this to build a dynamic interface with via reflection. e.g.

$webwork.evalute("$obj.${propName}")
Site running on a free Atlassian Confluence Open Source Project License granted to WebWork China. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.3 Build:#808 May 29, 2007) - Bug/feature request - Contact Administrators