Hi All.

I have been testing Jelly and JellySwing extensively and am encouraged by their potential as an Java alternative to Windows Forms .NET.

I recently came across a problem with tag attributes which correspond to arrays in JavaBeans. For example, given the following bean:

 public class SampleBean {
   public String[] theArray;
   ...
   public String[] getTheArray() {
     return this.theArray;
   }
   public void setTheArray(String[] anArray) {
     this.theArray = anArray;
   }
 }

... I hoped to be able to set the value of "theArray" using XML similar to the following:

<jelly:useBean var="sampleBean" class="SampleBean" theArray="element1, element2, element3"/>

However, instead of converting the attribute value to an array of three elements (ie. new String[3] {"element1", "element2", "element3"}) as intended, the result was an array with one element consisting of the entire attribute value (ie. new String[1] {"element1, element2, element3"}). After some investigation I was able to determine that this behavior is caused by the use of the methods BeanUtils.setProperty() and BeanUtils.populate() which are "...customized for extracting String-based request parameters from an HTTP request" (http://jakarta.apache.org/commons/beanutils/api/org/apache/commons/beanutils/BeanUtils.html#populate(java.lang.Object,%20java.util.Map)). I was able to produce the desired behavior by changing all references to these methods to the alternative functions BeanUtils.copyProperty() and BeanUtils.copyProperties(), respectively, which properly convert the attribute values.

Attached is a diff for the offending classes contained in the Jelly and JellySwing tag libraries. Also note that a quick search revealed classes in other Jelly tag libraries which will require changes:

jelly-tags/ant/src/java/org/apache/commons/jelly/tags/ant/AntTag.java
jelly-tags/ant/src/java/org/apache/commons/jelly/tags/ant/FileScannerTag.java
jelly-tags/bean/src/java/org/apache/commons/jelly/tags/bean/BeanTag.java
jelly-tags/dynabean/src/java/org/apache/commons/jelly/tags/dynabean/SetTag.java
jelly-tags/swt/src/java/org/apache/commons/jelly/tags/swt/LayoutTagSupport.java


Hope this helps.
Geoff.
Index: jelly-tags/swing/src/java/org/apache/commons/jelly/tags/swing/ComponentTag.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-commons/jelly/jelly-tags/swing/src/java/org/apache/commons/jelly/tags/swing/ComponentTag.java,v
retrieving revision 1.17
diff -u -r1.17 ComponentTag.java
--- jelly-tags/swing/src/java/org/apache/commons/jelly/tags/swing/ComponentTag.java    
 25 Feb 2004 01:31:56 -0000      1.17
+++ jelly-tags/swing/src/java/org/apache/commons/jelly/tags/swing/ComponentTag.java    
 5 May 2004 18:04:14 -0000
@@ -59,7 +59,7 @@
 
     /** the factory of widgets */
     private Factory factory;
-    
+
     public ComponentTag() {
     }
 
@@ -82,7 +82,7 @@
         if ( component != null ) {
             // lets just try set the 'action' property
             try {
-                BeanUtils.setProperty( component, "action", action );
+                BeanUtils.copyProperty( component, "action", action );
             } catch (IllegalAccessException e) {
                 throw new JellyTagException(e);
             } catch (InvocationTargetException e) {
@@ -99,7 +99,7 @@
         if ( component != null ) {
             // lets just try set the 'font' property
             try {
-                BeanUtils.setProperty( component, "font", font );
+                BeanUtils.copyProperty( component, "font", font );
             } 
             catch (IllegalAccessException e) {
                 throw new JellyTagException(e);
@@ -118,7 +118,7 @@
         if ( component != null ) {
             try {
                 // lets just try set the 'border' property
-                BeanUtils.setProperty( component, "border", border );
+                BeanUtils.copyProperty( component, "border", border );
             }
             catch (IllegalAccessException e) {
                 throw new JellyTagException(e);
@@ -142,7 +142,7 @@
             
             try {
                 // lets just try set the 'layout' property
-                BeanUtils.setProperty( component, "layout", layout );
+                BeanUtils.copyProperty( component, "layout", layout );
             }
             catch (IllegalAccessException e) {
                 throw new JellyTagException(e);
@@ -383,7 +383,7 @@
                 }
                 else {
                     try {
-                        BeanUtils.setProperty(component, name, value);
+                        BeanUtils.copyProperty(component, name, value);
                     } catch (IllegalAccessException e) {
                         throw new JellyTagException(e);
                     } catch (InvocationTargetException e) {
@@ -393,7 +393,7 @@
             }
             else {
                 try {
-                    BeanUtils.setProperty(bean, name, value);
+                    BeanUtils.copyProperty(bean, name, value);
                 } catch (IllegalAccessException e) {
                     throw new JellyTagException(e);
                 } catch (InvocationTargetException e) {
Index: jelly-tags/swing/src/java/org/apache/commons/jelly/tags/swing/ConstraintTag.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-commons/jelly/jelly-tags/swing/src/java/org/apache/commons/jelly/tags/swing/ConstraintTag.java,v
retrieving revision 1.9
diff -u -r1.9 ConstraintTag.java
--- jelly-tags/swing/src/java/org/apache/commons/jelly/tags/swing/ConstraintTag.java   
 1 Mar 2004 12:35:18 -0000       1.9
+++ jelly-tags/swing/src/java/org/apache/commons/jelly/tags/swing/ConstraintTag.java   
 5 May 2004 18:04:14 -0000
@@ -98,7 +98,7 @@
                } else {
             
             try {
-              BeanUtils.setProperty( bean, name, value );
+              BeanUtils.copyProperty( bean, name, value );
             } catch (IllegalAccessException e) {
                 throw new JellyTagException(e.toString());
             } catch (InvocationTargetException e) {
Index: src/java/org/apache/commons/jelly/tags/core/SetPropertiesTag.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-commons/jelly/src/java/org/apache/commons/jelly/tags/core/SetPropertiesTag.java,v
retrieving revision 1.7
diff -u -r1.7 SetPropertiesTag.java
--- src/java/org/apache/commons/jelly/tags/core/SetPropertiesTag.java   24 Feb 2004 
14:10:38 -0000      1.7
+++ src/java/org/apache/commons/jelly/tags/core/SetPropertiesTag.java   5 May 2004 
18:04:15 -0000
@@ -80,7 +80,7 @@
      */
     protected void setBeanProperties(Object bean, Map attributes) throws 
JellyTagException {
         try {
-            BeanUtils.populate(bean, attributes);
+            BeanUtils.copyProperties(bean, attributes);
         } catch (IllegalAccessException e) {
             throw new JellyTagException("could not set the properties on a bean",e);
         } catch (InvocationTargetException e) {
Index: src/java/org/apache/commons/jelly/tags/core/SetTag.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-commons/jelly/src/java/org/apache/commons/jelly/tags/core/SetTag.java,v
retrieving revision 1.17
diff -u -r1.17 SetTag.java
--- src/java/org/apache/commons/jelly/tags/core/SetTag.java     24 Feb 2004 14:10:38 
-0000      1.17
+++ src/java/org/apache/commons/jelly/tags/core/SetTag.java     5 May 2004 18:04:15 
-0000
@@ -172,7 +172,7 @@
                 map.put( property, value );
             }
             else {
-                BeanUtils.setProperty( target, property, value );       
+                BeanUtils.copyProperty( target, property, value );       
             }
         } catch (InvocationTargetException e) {
             log.error( "Failed to set the property: " + property + " on bean: " + 
target + " to value: " + value + " due to exception: " + e, e );
Index: src/java/org/apache/commons/jelly/tags/core/UseBeanTag.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-commons/jelly/src/java/org/apache/commons/jelly/tags/core/UseBeanTag.java,v
retrieving revision 1.14
diff -u -r1.14 UseBeanTag.java
--- src/java/org/apache/commons/jelly/tags/core/UseBeanTag.java 24 Feb 2004 14:10:38 
-0000      1.14
+++ src/java/org/apache/commons/jelly/tags/core/UseBeanTag.java 5 May 2004 18:04:15 
-0000
@@ -166,7 +166,7 @@
      */
     protected void setBeanProperties(Object bean, Map attributes) throws 
JellyTagException {
         try {
-            BeanUtils.populate(bean, attributes);
+            BeanUtils.copyProperties(bean, attributes);
         } catch (IllegalAccessException e) {
             throw new JellyTagException("could not set the properties of the bean",e);
         } catch (InvocationTargetException e) {

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to