img
PropertyEditor
Description
The LocaleEditor converts the String representation of a locale, such as en-
LocaleEditor
GB, into a java.util.Locale instance.
Converts a string into the JDK Pattern object or the other way round.
Pattern
PropertiesEditor converts a String in the format key1=value1 key2=value2
PropertiesEditor
keyn=valuen into an instance of java.util.Properties with the
corresponding properties configured.
Performs trimming on the string values before injection. You need to
StringTrimmerEditor
explicitly register this editor.
The URLEditor converts a String representation of a URL into an instance of
URLEditor
java.net.URL.
This set of PropertyEditors provides a good base for working with Spring and makes configuring
your application with common components such as files and URLs much simpler.
Creating a Custom PropertyEditor
Although the built-in PropertyEditors cover some of the standard cases of property type conversion,
there may come a time when you need to create your own PropertyEditor to support a class or a set of
classes you are using in your application.
Spring has full support for registering custom PropertyEditors; the only downside is that the
java.beans.PropertyEditor interface has a lot of methods, many of which are irrelevant to the task
at hand--converting property types. Thankfully, JDK 5 or newer provides the PropertyEditorSupport
class, which your own PropertyEditors can extend, leaving you to implement only a single method:
setAsText().
Let's take a simple example to see implementing custom property editor in action. Suppose we have
a Name class with just two properties, first name and last name. Listing 5-29 shows the class.
Listing 5-29. The Name Class
package com.apress.prospring3.ch5.pe;
public class Name {
private String firstName;
private String lastName;
public Name() {
}
public Name(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home