Results 1 to 2 of 2

Thread: trouble binding value from request->Object

  1. #1
    Join Date
    May 2007
    Posts
    15

    Default 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 at 07:34 PM.

  2. #2
    Join Date
    May 2007
    Posts
    15

    Default

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •