Announcement Announcement Module
Collapse
No announcement yet.
trouble binding value from request->Object Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • trouble binding value from request->Object

    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);
    }

    Code:
    public interface Car {
        String getColor();
    
        void setColor(String color);
    
        Type getType();
    
        void setType(Type type);
    }
    Next 2 concrete implmentation of these 2 Interfaces: CarImpl and TypeImpl which I will not show.

    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);
        }
    Last edited by nyker; May 18th, 2007, 08:34 PM.

  • #2
    found the problem in my ApplicationContext xml file. All works now.

    Comment

    Working...
    X