Integer Property: <input type="text" name="integerProperty" />
</p>
<p>
Class Property: <input type="text" name="classProperty" />
</p>
<p>
URL Property: <input type="text" name="urlProperty" />
</p>
<p><input type="submit" /></p>
</form>
In Listing 6-24 we simulate a HTTP request that will use the DataBinderto populate these
varied type properties.
Listing 6-24.MultiTypeCommandBean Unit Test
public void setUp() {
bean = new MultiTypeCommandBean();
request = new MockHttpServletRequest();
binder = new ServletRequestDataBinder(bean, "bean");
}
public void testBind() throws Exception {
request.addParameter("intProperty", "34");
request.addParameter("integerProperty", "200");
request.addParameter("classProperty", "java.lang.String");
request.addParameter("urlProperty", "http://www.example.com/");
binder.bind(request);
// all true!
assertEquals(34, bean.getIntProperty());
assertEquals(new Integer(200), bean.getIntegerProperty());
assertEquals(String.class, bean.getClassProperty());
assertEquals(new URL("http://www.example.com/"), bean.getUrlProperty());
}
As you can see, all the property values begin as Strings inside the HTTP request. When the
binder encounters a property type other than String, it will consult its list of PropertyEditors.
When it finds a match based on the property’s class, it will delegate the conversion to the
PropertyEditor. Because Spring provides so many default PropertyEditors, for most cases,
you won’t have to perform extra configuration, and the editors will gracefully perform the
conversions.
138 CHAPTER 6 ■THE CONTROLLER MENAGERIE