You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "J.Teo" <jc...@yahoo.com> on 2001/03/08 08:32:37 UTC

Help with BeanUtils.populate()

Hi,

First off, let me say that I'm not building on top of the Struts framework.
Rather, I'm leveraging the libraries and tag libraries.

I'm writing my own controller servlet and I tried to use BeanUtils to populate
my bean with:

public class MyController {
 .
 .
 .

    public static void populate(Object dobean,
                                HttpServletRequest req)
        throws IllegalAccessException,
               InvocationTargetException
    {

        Map paramMap = new Hashtable();

        Enumeration names = req.getParameterNames();

        while (names.hasMoreElements()) {
            String name, value;
           
            name = (String) names.nextElement();
            value = req.getParameter(name);
            paramMap.put(name, value);

        }

        BeanUtils.populate(dobean, paramMap);
    }
 .
 .
 .
}


And I get:


----------------------------------------
java.lang.IllegalArgumentException: argument type mismatch
        at java.lang.reflect.Method.invoke(Native Method)
        at
org.apache.struts.util.PropertyUtils.setSimpleProperty(PropertyUtils.java:825)
        at
org.apache.struts.util.PropertyUtils.setNestedProperty(PropertyUtils.java:756)
        at
org.apache.struts.util.PropertyUtils.setProperty(PropertyUtils.java:782)
        at org.apache.struts.util.BeanUtils.populate(BeanUtils.java:541)
----------------------------------------


I know building the Map just to use BeanUtils.populate() seems redundant, but
... ignore that for now.

It seems like the above would only work for bean properties of type String? But
doesn't BeanUtils.populate() claim to do the type conversion based on reflection
on the setter methods?

Is there anything in the library that can help me or do I have to write my own
populate() that handles the type conversion?

Thanks.


-jay