Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Panel
borderStylesolid
titleTable of contents
Table of Contents
minLevel1

Using custom converters

A powerful feature of Wicket is that you can customize how input gets converted from strings (because that is what you
get from your web browser) to your model objects.

...

To provide your own IConverterFactory, you need to override getConverterFactory in your application class. For example:

Code Block
/**
{panel}
 * @see wicket.Application#getConverterFactory()
 */
{panel}
public IConverterFactory getConverterFactory()
{
{panel}
  return new IConverterFactory()
  {
    public IConverter newConverter(final Locale locale)
    {
      final Converter converter = new Converter(locale);
      NumberToStringConverter numberToStringConverter = new NumberToStringConverter();
      NumberFormat fmt = NumberFormat.getInstance(locale);
      fmt.setMinimumFractionDigits(2);
      fmt.setMaximumFractionDigits(2);
      numberToStringConverter.setNumberFormat(locale, fmt);
      final StringConverter stringConverter = new StringConverter();
      stringConverter.set(Double.class, numberToStringConverter);
      stringConverter.set(Double.TYPE, numberToStringConverter);
      converter.set(String.class, stringConverter);
      return converter;
    }
  };
{panel}
}

Providing a custom converter for specific components

...

Code Block
public final class FormInputModel implements Serializable
{
{panel}
  ...
  private URL urlProperty;
  ...
{panel}

{panel}
  /**
   * Gets the urlProperty.
   * @return urlProperty
   */
  public URL getUrlProperty()
  {
    return urlProperty;
  }
{panel}

{panel}
  /**
   * Sets the urlProperty.
   * @param urlProperty urlProperty
   */
  public void setUrlProperty(URL urlProperty)
  {
    this.urlProperty = urlProperty;
  }
  ...
{panel}
}

Our converter:

Code Block
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Locale;

import wicket.util.convert.ConversionException;
import wicket.util.convert.IConverter;

/**
{panel}
 * Converts from and to URLs.
 *
 * @author Eelco Hillenius
 */
{panel}
public class URLConverter implements IConverter
{
	/**
	 * Construct.
	 */
	public URLConverter()
	{
	}

	/**
	 * @see wicket.util.convert.IConverter#convert(java.lang.Object, java.lang.Class)
	 */
	public Object convert(Object value, Class c)
	{
		if (value == null)
		{
			return null;
		}

		if (c == URL.class)
		{
			if (value.getClass() == URL.class)
			{
				return value;
			}

			try
			{
				return new URL(value.toString());
			}
			catch (MalformedURLException e)
			{
				throw new ConversionException("'" + value + "' is not a valid URL");
			}
		}
		return value.toString();
	}

	/**
	 * @see wicket.util.convert.ILocalizable#setLocale(java.util.Locale)
	 */
	public void setLocale(Locale locale)
	{
	}

	/**
	 * @see wicket.util.convert.ILocalizable#getLocale()
	 */
	public Locale getLocale()
	{
		return Locale.getDefault();
	}
}

...

Code Block
add(new TextField("urlProperty", URL.class)
{
{panel}
  public IConverter getConverter()
  {
    return new URLConverter();
  }
{panel}
});

The above textfield will now allways use the URLConverter instead of the application-wide converter. We don't have to provide the type parameter in TextField to make the conversion happening. However, the good thing about providing the type parameter is that any input is validated first (using the custom converter as well) before trying to update the model, so that if conversion fails, you'll get a feedback message instead of a stacktrace. It also forces the component to convert to the given type, even though Ognl might find another target type.

...