1. opensymphony
  2. xwork


xwork / docs / conversionerrorhandling.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="http://www.w3.org/1999/xhtml" lang="en_US" xml:lang="en_US">
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  <title>XWork Documentation</title>
  <link type="text/css" href="main.css" rel="STYLESHEET"/>
  <div id="page-logo">
    <a href="index.html"><img src="logo.png" border="0"/></a>
    <div class="snip-title">
	  <h1 class="snip-name">Type Conversion Error Handling
<div id="snip-content" class="snip-content">

 <div class="snip-attachments"></div>
 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 class="paragraph"/>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<p class="paragraph"/><div class="code"><pre><span class="java&#45;keyword">protected</span> <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><p class="paragraph"/>The XWork version always returns true, whereas the WebWork Interceptor returns false for values of null, "", and {""}, 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.<p class="paragraph"/>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:<p class="paragraph"/><div class="code"><pre>&#60;validator name=<span class="java&#45;quote">"conversion"</span> class=<span class="java&#45;quote">"com.opensymphony.xwork.validator.validators.ConversionErrorFieldValidator"</span>/&#62;</pre></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.<p class="paragraph"/>Both of these methods use the<p class="paragraph"/><div class="code"><pre>XWorkConverter.getConversionErrorMessage(propertyName, stack)</pre></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 "invalid.fieldvalue.propertyName" and using a default value if it is not found.