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 21:53:53 UTC

Re: cvs commit: jakarta-struts/src/share/org/apache/struts/util BeanUtils.java

Graig,

1st thanks for the response and fix!

>This patch is slightly simpler than the one submitted by Alec Bau  <Al...@msdw.com>,
>and covers the case where both prefix and suffix are specified

This fix may be simpler than the one I've suggested but unfortunately it still doesn't work!
The culprit here is the line:

     properties.put(name, request.getParameterValues(name));

The "name" is already stripped of prefix/suffix.  As a result "request.getParameterValues(name)" will always return "null" since
request doesn't have parameter with stripped name. And that throws "NullPointerException" in "properties.put"!

My understanding is that the main idea was to map parameter names with common prefixes/suffixes (such as "name.x" in case of image
buttons) to a single setter method, right?

If so, here's I'm repeating again the modified (covers the case with both prefix and suffix specified ) original fix as it applies
to the newest source version:

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

Current source also doesn't cover the case when both prefix and suffix are specified and name doesn't start with prefix. In this
case suffix test will be skipped. Is this by design so that both prefix AND suffix should match the name?

If it's, preferably, OR then following simple fix should apply:

-   if (prefix != null) {
+   if ((prefix != null) && name.startsWith(prefix)) {
-       if (!name.startsWith(prefix))
-         continue;

Thanks,
    Alec

craigmcc@locus.apache.org wrote:

> craigmcc    00/08/14 11:12:33
>
>   Modified:    src/share/org/apache/struts/util BeanUtils.java
>   Log:
>   Correctly handle the stripping of prefixes and suffixes from parameter
>   names.  This patch is slightly simpler than the one submitted by Alec Bau
>   <Al...@msdw.com>, and covers the case where both prefix and suffix are
>   specified.
>
>   Revision  Changes    Path
>   1.6       +5 -5      jakarta-struts/src/share/org/apache/struts/util/BeanUtils.java
>
>   Index: BeanUtils.java
>   ===================================================================
>   RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/util/BeanUtils.java,v
>   retrieving revision 1.5
>   retrieving revision 1.6
>   diff -u -r1.5 -r1.6
>   --- BeanUtils.java    2000/08/14 04:42:59     1.5
>   +++ BeanUtils.java    2000/08/14 18:12:31     1.6
>   @@ -1,7 +1,7 @@
>    /*
>   - * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/util/BeanUtils.java,v 1.5 2000/08/14 04:42:59 craigmcc Exp $
>   - * $Revision: 1.5 $
>   - * $Date: 2000/08/14 04:42:59 $
>   + * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/util/BeanUtils.java,v 1.6 2000/08/14 18:12:31 craigmcc Exp $
>   + * $Revision: 1.6 $
>   + * $Date: 2000/08/14 18:12:31 $
>     *
>     * ====================================================================
>     *
>   @@ -82,7 +82,7 @@
>     * @author Craig R. McClanahan
>     * @author Ralph Schaer
>     * @author Chris Audley
>   - * @version $Revision: 1.5 $ $Date: 2000/08/14 04:42:59 $
>   + * @version $Revision: 1.6 $ $Date: 2000/08/14 18:12:31 $
>     */
>
>    public final class BeanUtils {
>   @@ -544,7 +544,7 @@
>             if (prefix != null) {
>                 if (!name.startsWith(prefix))
>                     continue;
>   -             name = name.substring(0, prefix.length());
>   +             name = name.substring(prefix.length());
>             }
>             if (suffix != null) {
>                 if (!name.endsWith(suffix))
>
>
>

Re: Re: cvs commit: jakarta-struts/src/share/org/apache/struts/util BeanUtils.java

Posted by Alec Bau <Al...@msdw.com>.
Craig,

Mea Culpa! I've made a mistake in a patch. When dealing with suffix
line:

+       striped = striped.substring(0, name.length() - suffix.length());

should be changed to:

+       striped = striped.substring(0, striped.length() -
suffix.length());

Otherwise suffix striping for the name that's already striped of prefix
won't work!

Thanks again for the fast response and an excellent framework,
    Alec

Craig McClanahan wrote:

> Alec Bau wrote:
>
>> Graig,
>>
>> 1st thanks for the response and fix!
>>
>> >This patch is slightly simpler than the one submitted by Alec Bau
>> <Al...@msdw.com>,
>> >and covers the case where both prefix and suffix are specified
>>
>> This fix may be simpler than the one I've suggested but
>> unfortunately it still doesn't work!
>> The culprit here is the line:
>>
>>      properties.put(name, request.getParameterValues(name));
>>
>
> You are right, of course :-).  Revised patch forthcoming.
>
> Craig
>

Re: cvs commit: jakarta-struts/src/share/org/apache/struts/util BeanUtils.java

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

> Graig,
>
> 1st thanks for the response and fix!
>
> >This patch is slightly simpler than the one submitted by Alec Bau
> <Al...@msdw.com>,
> >and covers the case where both prefix and suffix are specified
>
> This fix may be simpler than the one I've suggested but unfortunately
> it still doesn't work!
> The culprit here is the line:
>
>      properties.put(name, request.getParameterValues(name));
>

You are right, of course :-).  Revised patch forthcoming.

Craig