}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String toString() {
return "First name: " + firstName + " - Last name: " + lastName;
}
}
To simplify the application configuration, let's develop a custom editor that converts a string with a
space separator into the Name class's first name and last name, respectively. Listing 5-30 shows the
custom property editor.
Listing 5-30. The NamePropertyEditor Class
package com.apress.prospring3.ch5.pe;
import java.beans.PropertyEditorSupport;
public class NamePropertyEditor extends PropertyEditorSupport {
public void setAsText(String text) throws IllegalArgumentException {
String[] name = text.split("\\s");
Name result = new Name(name[0], name[1]);
setValue(result);
}
}
The editor is very simple. It extends JDK's PropertyEditorSupport class and implements the
setAsText() method. In the method, we simply split the String into a string array with a space as the
delimiter. Afterwards, an instance of Name class is instantiated, passing in the String before the space
character as the first name and passing the String after the space character as the last name. Finally, the
converted value is returned by calling the setValue() method with the result.
To use the NamePropertyEditor in our application, we need to register the editor in Spring's
ApplicationContext. Listing 5-31 shows an ApplicationContext configuration that configures a
CustomEditorConfigurer and the NamePropertyEditor (pe/custom.xml).
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home