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

<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 Jun 03, 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. <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></p>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 class="paragraph">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</p><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 &quot;invalid.fieldvalue.propertyName&quot; 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 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.