struts-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Eric ...@ir.iit.edu>
Subject Re: complex, multidimensional properties
Date Thu, 09 Nov 2000 09:54:49 GMT
I have modified struts to support nested, indexed property names
(i.e. zoos[1].animals[1]) in its forms.  Can someone tell me why what
I have done is wrong and why nobody in their right mind would ever do
it?  :) I'm not very clear on the distinction between BeanUtils'
property handling methods and PropertyUtils' ones...they seem to
overlap and repeat code (albeit in different ways).

Here is my modified BeanUtils.populate:

    public static void populate(Object bean, String prefix, String suffix,
                                HttpServletRequest request)
        throws ServletException {

        try {
            Enumeration names = request.getParameterNames();
            while (names.hasMoreElements()) {
                String name = (String) names.nextElement();
                String stripped = name;
                if (prefix != null) {
                    if (!stripped.startsWith(prefix))
                        continue;
                    stripped = stripped.substring(prefix.length());
                }
                if (suffix != null) {
                    if (!stripped.endsWith(suffix))
                        continue;
                    stripped =
                        stripped.substring(0, stripped.length() - suffix.length());
                }

                // Problems that arise because of
                // this switch to setProperty:
                // 1. prefix and suffix have little 
                //    meaning for nested properties
                // 2. setProperty does no type conversions...although
                //    it seems like the
                //    various "convert" methods in BeanUtils could be
                //    used in it

                // this conversion seems to be necessary to support
                // both String and String[]
                String[] values = request.getParameterValues(name);
                if (values != null) {
                    try {
                        if (values.length == 1)
                            PropertyUtils.setProperty(bean, stripped, values[0]);
                        else
                            PropertyUtils.setProperty(bean, stripped, values);
                    } catch (NoSuchMethodException e) {
                        // ignore form parameters without corresponding properties
                    }
                }
            }
        } catch (Exception e) {
            throw new ServletException("BeanUtils.populate", e);
        }
    }

Thanks,
Eric

On Wed, Nov 08, 2000 at 05:30:58PM -0800, Craig R. McClanahan wrote:
> Eric wrote:
> 
> > Sorry to post a similar question many times, but I'm striving to get
> > an answer before the prototype of my project is due.
> >
> 
> Sorry for not being able to give a comprehensive answer earlier ... I've
> had more emergencies this week than I care to think about :-(.
> 
> >
> > I'm looking for a way to deal with properties that correspond to form
> > parameters such as Zoo1Animal1, Zoo1Animal2, Zoo2Animal1, etc.  I
> > would like them represented as two arrays in two seperate beans (a
> > Zoo array of AnimalForm objects and an Animal array of Strings in the
> > AnimalForm bean).  This seems like something that could be used by a
> > lot of complex input forms (especially ones that strive to keep inputs
> > on one page).
> >
> 
> I am still not sure that I understand the data model you are trying to
> achieve.  Could you sketch out a few simple class and method signatures
> that illustrate the pattern?
> 
> >
> > My question is, why does BeanUtils.populate not use
> > PropertyUtils.setProperty?  It seems like PropertyUtils.setProperty
> > would do exactly what I want...is it used anywhere?  Is there some tag
> > that corresponds to it?
> >
> 
> PropertyUtils.setProperty does not do any type conversions -- it assumes
> that your arguments are compatible and will throw exceptions if not.
> BeanUtils.populate knows that the input values are strings, so it looks at
> what the data type of the underlying property is and tries to do the right
> thing.  (In the future, it will also respect the existence of
> PropertyEditor classes if you have them for the data types used by your
> beans).
> 
> If you want to populate things yourself with PropertyUtils.setProperty,
> this should be possible -- you have access to all of the incoming
> information in the request.  But you will need to be responsible for
> converting from Strings (the way you receive all request parameters) to
> the corresponding data types.
> 
> >
> > Also, is what I'm considering reasonable?  Has anyone dealt with
> > issues like this before?  Is it possible for me to modify
> > BeanUtils.populate so that it uses PropertyUtils.setProperty and thus
> > provides the functionality I desire?  Are there some inherant problems
> > with this that I'm not seeing?
> >
> > thanx,
> > eric.
> 
> Craig McClanahan
> 
> 

-- 
 _____  _ 
| ____|(_)     http://www.iit.edu/~jenseri
|  _|  | |     Page me via ICQ at
| |___ | |     http://wwp.mirabilis.com/19022931
|______/ |     or by mailing 19022931@pager.mirabilis.com
     |__/

Mime
View raw message