Source

xwork / docs / wikidocs / Null Property Access.html

<html>
    <head>
        <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">	    
    </head>

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

				    <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, YourAction-conversion.properties 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>

				    
                    			    </td>
		    </tr>
	    </table>
	    <table border="0" cellpadding="0" cellspacing="0" width="100%">
			<tr>
				<td height="12" background="border/border_bottom.gif"><img src="border/spacer.gif" width="1" height="1" border="0"/></td>
			</tr>
		    <tr>
			    <td align="center"><font color="grey">Document generated by Confluence on Sep 07, 2004 00:34</font></td>
		    </tr>
	    </table>
    </body>
</html>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.