webwork / docs / wikidocs / Type Conversion.html

        <title>WebWork 2 : Type Conversion</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">
                            WebWork 2 : Type Conversion
				    <div class="pagesubheading">
					    This page last changed on Jul 19, 2004 by <font color="#0050B2">unkyaku</font>.

				    <p class="paragraph">WebWork has very advanced type conversion support thanks to XWork.  For more details please consult <a href="" title="Type Conversion">XW:Type Conversion</a>.</p><h2 class="heading2"><a name="TypeConversion-ASimpleExample"> A Simple Example</a></h2><p class="paragraph">Type conversion is great for situations where you need to turn a String in to a more complex object. Because the web is type-agnostic (everything is a String in HTTP), WebWork&#039;s type conversion features are very useful. For instance, if you were prompting a user to enter in coordinates in the form of a String (&quot;3, 22&quot;), you could have WebWork do the conversion both from String to Point and from Point to String.</p>Using this &quot;point&quot; example, if your action (or another compound object in which you are setting properties on) has a corresponding file, WebWork will use the configured type converters for conversion to and from strings. So turning &quot;3, 22&quot; in to new Point(3, 22) is done by merely adding the following entry to<br/>

<div class="code"><div class="codeContent">
<pre>point = com.acme.PointConverter&#10;# note: PointerConverter must implement ognl.TypeConverter &#10;#          or extend ognl.DefaultTypeConverter</pre>
</div></div><p class="paragraph">Your type converter should be sure to check what class type it is being requested to convert. Because it is used for both to and from strings, you will need to split the conversion method in to two parts: one that turns Strings in to Points, and one that turns Points in to Strings.</p>After this is done, you can now reference your point (&lt;ww:property value=&quot;point&quot;/&gt;) and it will be printed as &quot;3, 22&quot; again. As such, if you submit this back to an action, it will be converted back to a Point once again.<p class="paragraph">NOTE: Type conversion should not be used as a substitute for i18n. It is not recommended to use this feature to print out properly formatted dates. Rather, you should use the i18n features of WebWork (and consult the JavaDocs for JDK&#039;s MessageFormat object) to see how a properly formatted date should be displayed.</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 Oct 15, 2004 02:04</font></td>