this.prefix = prefix;
}
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
public String toString() {
return "(" + areaCode + ") " + prefix + "-" + suffix;
}
}
We will need a command class to contain a PhoneNumberproperty so that it may be set by
the DataBinder. Of course, Spring MVC doesn’t require nesting your domain class inside some
command bean. If you wish to create a form with input fields directly mapping to properties
of the PhoneNumber, then there is no need for a custom PropertyEditor(because all properties
of a PhoneNumberare Stringin this case). Listing 6-32 illustrates how to convert a single text
field into a (relatively) complex domain object, so we will treat the PhoneNumberas a property
itself.
Listing 6-32.PhoneNumberCommand Bean
public class PhoneNumberCommand {
private PhoneNumber phoneNumber;
public PhoneNumber getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(PhoneNumber phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
For the real fun of this example, we now create the PhoneNumberPropertyEditor(shown in
Listing 6-33) that knows how to convert a string with the format ^(\d{3}) \d{3}-\d{4}$(as a
regular expression) into a PhoneNumberinstance.
Listing 6-33.PhoneNumberEditor Class
public class PhoneNumberEditor extends PropertyEditorSupport {
private Pattern pattern = Pattern.compile("^\\((\\d{3})\\) (\\d{3})-(\\d{4})$");
@Override
CHAPTER 6 ■THE CONTROLLER MENAGERIE 143