Source

xwork / docs / wikidocs / Type Conversion In Collections.html

Full commit
<html>
    <head>
        <title>XWork : Type Conversion In Collections</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 : Type Conversion In Collections
                                                    </span>
				    </div>
				    <div class="pagesubheading">
					    This page last changed on Jul 21, 2004 by <font color="#0050B2">unkyaku</font>.
				    </div>

				    <p class="paragraph">There is a limit to how much type conversion XWork can do automatically when it works with collections.</p>Internally, XWork uses XWorkList and XWorkMap.  They basically extend ArrayList and HashMap respectively to support type conversion.  Anything that gets inserted into them are automatically converted if they&#039;re not already the required type.<p class="paragraph">In situations where the collection is null, XWork will create either an XWorkList or an XWorkMap and set it back on to the target (see <a href="Null Property Access.html" title="Null Property Access">Null Property Access</a> for details), and everything works beautifully.  However, if the collection is not null, and the collections returned are neither an XWorkList nor an XWorkMap, there&#039;s no way type conversion can be performed.  In this case, the you want type conversion to work, you will have to seriously consider using XWorkList or XWorkMap instead of ArrayList or HashMap.</p><h3 class="heading3"><a name="TypeConversionInCollections-Example"> Example</a></h3><p class="paragraph">Imagine a User object that has a Collection of e-mail addresses.  Now, suppose the input is a String array &#91; &quot;foo@bar.com&quot; &#93;, and the target is &quot;emails&#91;1&#93;&quot;.  What we&#039;d really like XWork to to is convert that String array into a String before inserting it into the collection.  The first thing we&#039;d do is create a User-conversion.properties with the line:</p><div class="code"><div class="codeContent">
<pre>Collection&#95;emails = java.lang.String</pre>
</div></div>
<p class="paragraph">If User.getEmails() returns null, then XWork will create a new XWorkList and call User.setEmails() with it.  It will then add an empty string at position &#91;0&#93; and insert &quot;foo@bar.com&quot; at position &#91;1&#93; (the array is automatically converted into a String).</p>If User.getEmails() returns something other than XWorkList, when XWork tries to set emails&#91;1&#93; to &#91; &quot;foo@bar.com&quot; &#93;, no type connversion will occur.  In addition, if the array is empty, ArrayList will throw an IndexOutOfBoundsException.

				    
                    			    </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>