You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Alec Bau <Al...@msdw.com> on 2000/08/14 17:17:02 UTC

REPOST 2: Bug in BeanUtils.populate?

I'm re-posting this again since I didn't get any response on that.
Please somebody (Craig?) let me know if there are plans to fix this or
not! I can't  re-apply my fix with each new Struts release.

Currently if prefix or suffix matches against parameter
BeanUtils.populate always generates NullPointerException from
Properties.put because of the null value. Moreover the code to strip
prefix is also wrong. Here's the fix I use:

         String name = (String) names.nextElement();
/*add*/  String striped = name;
         if (prefix != null) {
            if (!name.startsWith(prefix))
               continue;
/*old        name = name.substring(0, prefix.length()); */
/*new*/      striped = name.substring(prefix.length());
         }
         if (suffix != null) {
             if (!name.endsWith(suffix))
               continue;
/*old    name = name.substring(0, name.length() - suffix.length()); */
/*new*/  striped = name.substring(0, name.length() - suffix.length());
         }
/*old    properties.put(name, request.getParameterValues(name)); */
/*new*/  properties.put(striped, request.getParameterValues(name));

Please let me know.
Thanks,
    Alec

-------- Original Message --------
   From: Alec Bau <Al...@msdw.com>
Subject: [Fwd: Bug in BeanUtils.populate ?]
     To: struts-dev@jakarta.apache.org

I didn't get any response on struts-user so I'm re-posting this message
here since struts-dev seems to be a more appropriate place.

-------- Original Message --------
   From: Alec Bau <Al...@msdw.com>
Subject: Bug in BeanUtils.populate ?
     To: struts-user@jakarta.apache.org

To fight problem with image buttons (reported earlier) I was trying to
use form suffix (ActionMapping.getFormSuffix) support to map <name>.x/y
parameters. I think there's a bug in BeanUtils.populate(bean, prefix,
suffix, request) method that's called from
ActionServlet.processActionForm method.

Here's the abstract from BeanUtils.populate:

 // Build a list of relevant request parameters from this request
 Hashtable properties = new Hashtable();
 Enumeration names = request.getParameterNames();
 while (names.hasMoreElements()) {
     String name = (String) names.nextElement();
     if (prefix != null) {
        if (!name.startsWith(prefix))
           continue;
        name = name.substring(0, prefix.length());
     }
     if (suffix != null) {
        if (!name.endsWith(suffix))
           continue;
        name = name.substring(0, name.length() - suffix.length());
     }
     properties.put(name, request.getParameterValues(name));
 }

For non null suffix or prefix  last line stores null as a value since
request parameter value couldn't be found for the name that's already
stripped of prefix and/or suffix. Later because of that
BeanUtils.populate(bean, properties) skips parameter processing
altogether. Perhaps loop body should be changed to something like that:

     String name = (String) names.nextElement();
     String striped = name;
     if (prefix != null) {
        if (!name.startsWith(prefix))
           continue;
        striped = name.substring(0, prefix.length());
     }
     if (suffix != null) {
        if (!name.endsWith(suffix))
           continue;
        striped = name.substring(0, name.length() - suffix.length());
     }
     properties.put(striped, request.getParameterValues(name));

Thanks.

Re: REPOST 2: Bug in BeanUtils.populate?

Posted by Craig McClanahan <Cr...@eng.sun.com>.
Alec Bau wrote:

>
> I'm re-posting this again since I didn't get any response on that.
> Please somebody (Craig?) let me know if there are plans to fix this or
> not! I can't  re-apply my fix with each new Struts release.
>

Sorry Alec ... I am embarassingly far behind on applying patches.  I
will apply this one right now, and it will show up in tonight's build of
Struts.

Craig