Hi I am having some trouble binding request parameters to an Interfaced Object. Here's my setup:
Note: All this code works fine when Type is just a class and not an interface. What's wrong here?
Code:public interface Type { String getBrand(); void setBrand(String brand); String getMake(); void setMake(String make); }
Next 2 concrete implmentation of these 2 Interfaces: CarImpl and TypeImpl which I will not show.Code:public interface Car { String getColor(); void setColor(String color); Type getType(); void setType(Type type); }
Below is the TypePropertyEditor I defined that converts say String "Honda,Accord" to a TypeImpl object.
Code:public class TypePropertyEditor extends PropertyEditorSupport { @Override public void setAsText(String text) throws IllegalArgumentException { if (text==null || !text.matches("\\s*\\w+\\s*,\\s*\\w+\\s*")) throw new IllegalArgumentException(); String[] sa = text.split(","); Type type = new TypeImpl(sa[0].trim(),sa[1].trim()); setValue(type); } @Override public String getAsText() { Type type = (Type)getValue(); return type.getBrand()+","+type.getMake(); } }
Finally here is a failed test I wrote:
Code:public void testB() { MockHttpServletRequest request = new MockHttpServletRequest(); request.addParameter("color","red"); request.addParameter("type","Honda,Accord"); Car car = new CarImpl(new TypeImpl(),null); ServletRequestDataBinder binder = new ServletRequestDataBinder(car); binder.registerCustomEditor(Type.class, new TypePropertyEditor()); binder.bind(request); System.out.println(car); assertEquals(new CarImpl(new TypeImpl("Honda","Accord"), "red"), car); }


Reply With Quote