Source

xwork / docs / wikidocs / Type Conversion Error Handling.html

Full commit
<html>
    <head>
        <title>XWork : Type Conversion Error Handling</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 Error Handling
                                                    </span>
				    </div>
				    <div class="pagesubheading">
					    This page last changed on Nov 11, 2004 by <font color="#0050B2">plightbo</font>.
				    </div>

				    <p class="paragraph">Type conversion errors are handled by the XWorkConverter whenever any Exception is thrown by a converter during converting a value. Type conversion errors are added to a Map stored in the ActionContext which is available via ActionContext.getContext().getConversionErrors(). This map is a map of field name to values which will allow you to access the original value which failed conversion.</p><div class="panel"><div class="panelContent" style="background-color: #ffffce; ">
<p class="paragraph"><img class="rendericon" src="./icons/emoticons/information.png" height="16" width="16" align="absmiddle" alt="" border="0"/> Tip: Reporting type conversion errors can be done in two ways:
<ul class="star">
<li> On a per-field basis with the conversion validator</li>
<li> Globally via the conversion interceptor</li>
</ul><br/>
The default text for conversion errors isn&#039;t exactly pretty: <em class="emphasis">Invalid field value for field &quot;xxx&quot;</em>. You can change this by defining an i18n property named <b class="strong">invalid.fieldvalue.&#91;propertyName&#93;</b>, such as invalid.fieldvalue.foo.</p>
</div></div><br/>
 
There are 2 ways of automatically populating field errors with the type conversion errors into the field errors of the Action. The first will populate all of the field errors from the conversion errors and is implemented as an Interceptor. There are actually 2 Interceptors, one in XWork and one in WebWork which extends the one in XWork. They differ in the implementation of the method <br/>
 
<div class="code"><div class="codeContent">
<pre><span class="java&#45;keyword">protected</span>&#160;<span class="java&#45;object">boolean</span> shouldAddError(<span class="java&#45;object">String</span> propertyName, <span class="java&#45;object">Object</span> value)</pre>
</div></div>&#160;<p class="paragraph">The XWork version always returns true, whereas the WebWork Interceptor returns false for values of null, &quot;&quot;, and &#123;&quot;&quot;&#125;, preventing type conversion exceptions for these common empty values from propogating to the field errors of the Action. The WebWork version of this Interceptor has been added to the webwork-defaults.xml and to the defaultStack defined therein. <br/>
 
If you choose not to use this Interceptor, you can choose to enable this on a per-field basis by using the Validation framework with the new field validator added for this, defined in the validators.xml file like this:<br/>
 
<div class="code"><div class="codeContent">
<pre>&lt;validator name=<span class="java&#45;quote">&quot;conversion&quot;</span> class=<span class="java&#45;quote">&quot;com.opensymphony.xwork.validator.validators.ConversionErrorFieldValidator&quot;</span>/&gt;</pre>
</div></div></p>This validator will look up the conversion errors and, if it finds a conversion error for the field it is applied to, it will add the appropriate error message to the Action. <br/>
 
Both of these methods use the <br/>

<div class="code"><div class="codeContent">
<pre>XWorkConverter.getConversionErrorMessage(propertyName, stack)</pre>
</div></div><p class="paragraph">which looks up the type conversion error message for the property name as was done previously, by looking for a message keyed by <b class="strong">invalid.fieldvalue.&#91;propertyName&#93;</b> and using a default value if it is not found.</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 Dec 12, 2004 12:35</font></td>
		    </tr>
	    </table>
    </body>
</html>