xwork / docs / wikidocs / Null Property Access.html

Full commit
        <title>XWork : Null Property Access</title>
	    <link rel="stylesheet" href="styles/site.css" type="text/css" />
        <META http-equiv="Content-Type" content="text/html; charset=UTF-8">	    

	    <table class="pagecontent" border="0" cellpadding="0" cellspacing="0" width="100%" bgcolor="#ffffff">
			    <td valign="top" class="pagebody">
				    <div class="pageheader">
					    <span class="pagetitle">
                            XWork : Null Property Access
				    <div class="pagesubheading">
					    This page last changed on Jun 24, 2004 by <font color="#0050B2">unkyaku</font>.

				    <p class="paragraph">Null property access is a unique feature to XWork that allows object graphs to be created at runtime, saving you the headache of having to pre-initialize them.</p><h3 class="heading3"><a name="NullPropertyAccess-Simpleobjectgraphs"> Simple object graphs</a></h3><p class="paragraph">The feature is quite simple: <b class="strong">only</b> during the ParametersInterceptor (for WebWork this would be when http parameters are applied to an action), if an expression being set, such as &quot;document.title&quot;, results in a NullPointerException, XWork will attempt to create the null object and try again. So in this case, if &quot;document&quot; is null, WebWork will construct a new Document object so that setting the title will succeed.</p>This is very useful because it reduces the amount of flattening (and unflattening) you are required to do in your action classes when displaying and setting data from web pages. Rather, you can usually represent the complete object graph by just naming your input fields with well thought-out names (like &quot;document.title&quot;). 
<p class="paragraph"><h3 class="heading3"><a name="NullPropertyAccess-Collections%2CListsandMaps"> Collections, Lists and Maps</a></h3></p>XWork extends this feature even further by offering special support for Collections, Lists and Maps.  If you are providing input for one of these interfaces, XWork can be told what type of objects they will hold and automatically populate them accordingly. What this means is that if you refer to &quot;children[0].name&quot;, XWork will automatically create a new List to hold the children and also add an empty Child object to that list, so that setting a name on the expression &quot;children[0].name&quot; will work correctly. The same goes for maps.  <p class="paragraph">For this to work you must tell the converters what the objects in the List or Map will be. You do this by specifying &quot;Collection_<b class="strong">property</b> = com.acme.CollectionItem&quot; in the conversion properties file. So if you have an action that has a &quot;children&quot; property that should be filled with Child objects, should contain:</p><div class="code"><div class="codeContent">
<pre>Collection&#95;children = come.acme.Child</pre>
</div></div><p class="paragraph">For the purposes of conversion, WebWork considers Collections and Lists to be the same.   If the &quot;children&quot; property was declared to be a Collection, a List would be created and used.</p><h3 class="heading3"><a name="NullPropertyAccess-CustomObjects"> Custom Objects</a></h3><p class="paragraph">Advanced users who need to instantiate custom objects will want to extend <b class="strong">com.opensymphony.xwork.ObjectFactory</b> and override the buildBean(Class) method.  The default implementation is rather trivial:</p><div class="code"><div class="codeContent">
<pre><span class="java&#45;keyword">public</span>&#160;<span class="java&#45;object">Object</span> buildBean(<span class="java&#45;object">Class</span> clazz) <span class="java&#45;keyword">throws</span> Exception &#123;&#10;  <span class="java&#45;keyword">return</span> clazz.newInstance();&#10;&#125;</pre>
</div></div><p class="paragraph">Once you have your own custom ObjectFactory, you&#039;ll need to let XWork know to use it.  You do this by calling ObjectFactory.setObjectFactory(yourObjFactory).</p>

	    <table border="0" cellpadding="0" cellspacing="0" width="100%">
				<td height="12" background="border/border_bottom.gif"><img src="border/spacer.gif" width="1" height="1" border="0"/></td>
			    <td align="center"><font color="grey">Document generated by Confluence on Sep 07, 2004 00:34</font></td>