This page last changed on Nov 11, 2004 by plightbo.

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.

Tip: Reporting type conversion errors can be done in two ways:

  • On a per-field basis with the conversion validator
  • Globally via the conversion interceptor

The default text for conversion errors isn't exactly pretty: Invalid field value for field "xxx". You can change this by defining an i18n property named invalid.fieldvalue.[propertyName], such as invalid.fieldvalue.foo.


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
protected boolean shouldAddError(String propertyName, Object value)
 

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.
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:

<validator name="conversion" class="com.opensymphony.xwork.validator.validators.ConversionErrorFieldValidator"/>

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.
Both of these methods use the
XWorkConverter.getConversionErrorMessage(propertyName, stack)

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.

Document generated by Confluence on Dec 12, 2004 12:35