You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by cr...@apache.org on 2001/01/10 02:54:22 UTC
cvs commit: jakarta-struts/web/test html-multibox.jsp html-setters.jsp
craigmcc 01/01/09 17:54:22
Modified: src/share/org/apache/struts/util BeanUtils.java
PropertyUtils.java RequestUtils.java
src/test/org/apache/struts/test TestBean.java
web/test html-multibox.jsp html-setters.jsp
Log:
Fixes for using nested and indexed properties in HTML forms! You can now
use simple/nested/indexed expressions like this:
<html:text property="mailingAddress.street"/>
and the appropriate processing will be applied to get the street address
(for initial display) and set the street address (when the form is
processed).
Two pages exist in the "struts-test" unit test webapp - each of them
should remember any changes you make from one screen to the next, because
they are captured automatically in an ActionForm.
TODO: Verify that I did not break file uploads while making these
changes.
Revision Changes Path
1.24 +107 -34 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.23
retrieving revision 1.24
diff -u -r1.23 -r1.24
--- BeanUtils.java 2001/01/08 22:20:26 1.23
+++ BeanUtils.java 2001/01/10 01:54:21 1.24
@@ -1,7 +1,7 @@
/*
- * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/util/BeanUtils.java,v 1.23 2001/01/08 22:20:26 craigmcc Exp $
- * $Revision: 1.23 $
- * $Date: 2001/01/08 22:20:26 $
+ * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/util/BeanUtils.java,v 1.24 2001/01/10 01:54:21 craigmcc Exp $
+ * $Revision: 1.24 $
+ * $Date: 2001/01/10 01:54:21 $
*
* ====================================================================
*
@@ -64,6 +64,7 @@
import java.beans.BeanInfo;
+import java.beans.IndexedPropertyDescriptor;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
@@ -74,7 +75,6 @@
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
-import org.apache.struts.upload.FormFile;
/**
@@ -83,12 +83,29 @@
* @author Craig R. McClanahan
* @author Ralph Schaer
* @author Chris Audley
- * @version $Revision: 1.23 $ $Date: 2001/01/08 22:20:26 $
+ * @version $Revision: 1.24 $ $Date: 2001/01/10 01:54:21 $
*/
public final class BeanUtils {
+ // ------------------------------------------------------ Private Variables
+
+
+ /**
+ * The debugging detail level for this component.
+ */
+ private static int debug = 0;
+
+ public static int getDebug() {
+ return (debug);
+ }
+
+ public static void setDebug(int newDebug) {
+ debug = newDebug;
+ }
+
+
// --------------------------------------------------------- Public Classes
@@ -431,11 +448,9 @@
if ((bean == null) || (properties == null))
return;
- // Identify the property descriptors supported by our JavaBean
- PropertyDescriptor descriptors[] =
- PropertyUtils.getPropertyDescriptors(bean);
- if (descriptors.length < 1)
- return;
+ if (debug >= 1)
+ System.out.println("BeanUtils.populate(" + bean + ", " +
+ properties + ")");
// Loop through the property name/value pairs to be set
Iterator names = properties.keySet().iterator();
@@ -443,28 +458,66 @@
// Identify the property name and value(s) to be assigned
String name = (String) names.next();
+ if (name == null)
+ continue;
Object value = properties.get(name); // String or String[]
- if (value == null)
+
+ /*
+ if (debug >= 1)
+ System.out.println(" name='" + name + "', value.class='" +
+ (value == null ? "NONE" :
+ value.getClass().getName()) + "'");
+ */
+
+ // Get the property descriptor of the requested property (if any)
+ PropertyDescriptor descriptor = null;
+ try {
+ descriptor = PropertyUtils.getPropertyDescriptor(bean, name);
+ } catch (Throwable t) {
+ /*
+ if (debug >= 1)
+ System.out.println(" getPropertyDescriptor: " + t);
+ */
+ descriptor = null;
+ }
+ if (descriptor == null) {
+ /*
+ if (debug >= 1)
+ System.out.println(" No such property, skipping");
+ */
continue;
+ }
+ /*
+ if (debug >= 1)
+ System.out.println(" Property descriptor is '" +
+ descriptor + "'");
+ */
// Identify the relevant setter method (if there is one)
Method setter = null;
- Class parameterTypes[] = null;
- for (int i = 0; i < descriptors.length; i++) {
- if (!name.equals(descriptors[i].getName()))
- continue;
- setter = descriptors[i].getWriteMethod();
- if (setter == null)
- continue;
- parameterTypes = setter.getParameterTypes();
- if (parameterTypes.length != 1) {
- setter = null;
- continue;
- }
- break;
- }
+ if (descriptor instanceof IndexedPropertyDescriptor)
+ setter = ((IndexedPropertyDescriptor) descriptor).
+ getIndexedWriteMethod();
if (setter == null)
+ setter = descriptor.getWriteMethod();
+ if (setter == null) {
+ if (debug >= 1)
+ System.out.println(" No setter method, skipping");
continue;
+ }
+ Class parameterTypes[] = setter.getParameterTypes();
+ /*
+ if (debug >= 1)
+ System.out.println(" Setter method is '" +
+ setter.getName() + "(" +
+ parameterTypes[0].getName() +
+ (parameterTypes.length > 1 ?
+ ", " + parameterTypes[1].getName() : "" )
+ + ")'");
+ */
+ Class parameterType = parameterTypes[0];
+ if (parameterTypes.length > 1)
+ parameterType = parameterTypes[1]; // Indexed setter
// Convert the parameter value as required for this setter method
Object parameters[] = new Object[1];
@@ -473,27 +526,47 @@
String values[] = new String[1];
values[0] = (String) value;
parameters[0] = ConvertUtils.convert((String[]) values,
- parameterTypes[0]);
- } else {
+ parameterType);
+ } else if (value instanceof String[]) {
parameters[0] = ConvertUtils.convert((String[]) value,
- parameterTypes[0]);
+ parameterType);
+ } else {
+ parameters[0] = value;
}
} else {
if (value instanceof String) {
parameters[0] = ConvertUtils.convert((String) value,
- parameterTypes[0]);
- } else if (value instanceof FormFile) {
- parameters[0] = value;
- } else {
+ parameterType);
+ } else if (value instanceof String[]) {
parameters[0] = ConvertUtils.convert(((String[]) value)[0],
- parameterTypes[0]);
+ parameterType);
+ } else {
+ parameters[0] = value;
}
}
// Invoke the setter method
- setter.invoke(bean, parameters);
+ /*
+ if (debug >= 1)
+ System.out.println(" Setting to " +
+ (parameters[0] == null ? "NULL" :
+ "'" + parameters[0] + "'"));
+ */
+ try {
+ PropertyUtils.setProperty(bean, name, parameters[0]);
+ } catch (NoSuchMethodException e) {
+ if (debug >= 1) {
+ System.out.println(" CANNOT HAPPEN: " + e);
+ e.printStackTrace(System.out);
+ }
+ }
}
+
+ /*
+ if (debug >= 1)
+ System.out.println("============================================");
+ */
}
1.11 +34 -4 jakarta-struts/src/share/org/apache/struts/util/PropertyUtils.java
Index: PropertyUtils.java
===================================================================
RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/util/PropertyUtils.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- PropertyUtils.java 2001/01/09 01:45:10 1.10
+++ PropertyUtils.java 2001/01/10 01:54:21 1.11
@@ -1,7 +1,7 @@
/*
- * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/util/PropertyUtils.java,v 1.10 2001/01/09 01:45:10 craigmcc Exp $
- * $Revision: 1.10 $
- * $Date: 2001/01/09 01:45:10 $
+ * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/util/PropertyUtils.java,v 1.11 2001/01/10 01:54:21 craigmcc Exp $
+ * $Revision: 1.11 $
+ * $Date: 2001/01/10 01:54:21 $
*
* ====================================================================
*
@@ -116,7 +116,7 @@
* @author Craig R. McClanahan
* @author Ralph Schaer
* @author Chris Audley
- * @version $Revision: 1.10 $ $Date: 2001/01/09 01:45:10 $
+ * @version $Revision: 1.11 $ $Date: 2001/01/10 01:54:21 $
*/
public final class PropertyUtils {
@@ -149,6 +149,20 @@
/**
+ * The debugging detail level for this component.
+ */
+ private static int debug = 0;
+
+ public static int getDebug() {
+ return (debug);
+ }
+
+ public static void setDebug(int newDebug) {
+ debug = newDebug;
+ }
+
+
+ /**
* The cache of PropertyDescriptor arrays for beans we have already
* introspected, keyed by the fully qualified class name of this object.
*/
@@ -604,6 +618,10 @@
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
+ if (debug >= 1)
+ System.out.println("setIndexedProperty('" + bean + ", " +
+ name + ", " + value + ")");
+
// Identify the index of the requested individual property
int delim = name.indexOf(INDEXED_DELIM);
int delim2 = name.indexOf(INDEXED_DELIM2);
@@ -647,6 +665,10 @@
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
+ if (debug >= 1)
+ System.out.println("setIndexedProperty('" + bean + ", " +
+ name + ", " + index + ", " + value + ")");
+
// Retrieve the property descriptor for the specified property
PropertyDescriptor descriptor =
getPropertyDescriptor(bean, name);
@@ -707,6 +729,10 @@
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
+ if (debug >= 1)
+ System.out.println("setNestedProperty('" + bean + ", " +
+ name + ", " + value + ")");
+
while (true) {
int delim = name.indexOf(NESTED_DELIM);
if (delim < 0)
@@ -776,6 +802,10 @@
String name, Object value)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
+
+ if (debug >= 1)
+ System.out.println("setSimpleProperty('" + bean + ", " +
+ name + ", " + value + ")");
// Retrieve the property setter method for the specified property
PropertyDescriptor descriptor =
1.4 +4 -6 jakarta-struts/src/share/org/apache/struts/util/RequestUtils.java
Index: RequestUtils.java
===================================================================
RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/util/RequestUtils.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- RequestUtils.java 2001/01/08 00:17:43 1.3
+++ RequestUtils.java 2001/01/10 01:54:21 1.4
@@ -1,7 +1,7 @@
/*
- * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/util/RequestUtils.java,v 1.3 2001/01/08 00:17:43 craigmcc Exp $
- * $Revision: 1.3 $
- * $Date: 2001/01/08 00:17:43 $
+ * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/util/RequestUtils.java,v 1.4 2001/01/10 01:54:21 craigmcc Exp $
+ * $Revision: 1.4 $
+ * $Date: 2001/01/10 01:54:21 $
*
* ====================================================================
*
@@ -86,7 +86,7 @@
* in the Struts controller framework.
*
* @author Craig R. McClanahan
- * @version $Revision: 1.3 $ $Date: 2001/01/08 00:17:43 $
+ * @version $Revision: 1.4 $ $Date: 2001/01/10 01:54:21 $
*/
public class RequestUtils {
@@ -309,8 +309,6 @@
String name = (String) names.nextElement();
String stripped = name;
int subscript = stripped.lastIndexOf("[");
- if (subscript >= 0) // Remove subscript expression
- stripped = stripped.substring(0, subscript);
if (prefix != null) {
if (!stripped.startsWith(prefix))
continue;
1.4 +37 -10 jakarta-struts/src/test/org/apache/struts/test/TestBean.java
Index: TestBean.java
===================================================================
RCS file: /home/cvs/jakarta-struts/src/test/org/apache/struts/test/TestBean.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- TestBean.java 2001/01/08 00:17:43 1.3
+++ TestBean.java 2001/01/10 01:54:21 1.4
@@ -1,7 +1,7 @@
/*
- * $Header: /home/cvs/jakarta-struts/src/test/org/apache/struts/test/TestBean.java,v 1.3 2001/01/08 00:17:43 craigmcc Exp $
- * $Revision: 1.3 $
- * $Date: 2001/01/08 00:17:43 $
+ * $Header: /home/cvs/jakarta-struts/src/test/org/apache/struts/test/TestBean.java,v 1.4 2001/01/10 01:54:21 craigmcc Exp $
+ * $Revision: 1.4 $
+ * $Date: 2001/01/10 01:54:21 $
*
* ====================================================================
*
@@ -72,7 +72,7 @@
* General purpose test bean for Struts custom tag tests.
*
* @author Craig R. McClanahan
- * @version $Revision: 1.3 $ $Date: 2001/01/08 00:17:43 $
+ * @version $Revision: 1.4 $ $Date: 2001/01/10 01:54:21 $
*/
public class TestBean extends ActionForm {
@@ -124,7 +124,7 @@
/**
- * An integer array that is accessed as an array as well as indexed.
+ * Integer arrays that are accessed as an array as well as indexed.
*/
private int intArray[] = { 0, 10, 20, 30, 40 };
@@ -136,15 +136,27 @@
this.intArray = intArray;
}
+ private int intIndexed[] = { 0, 10, 20, 30, 40 };
+
public int getIntIndexed(int index) {
- return (intArray[index]);
+ return (intIndexed[index]);
}
public void setIntIndexed(int index, int value) {
- intArray[index] = value;
+ intIndexed[index] = value;
}
+ private int intMultibox[] = new int[0];
+
+ public int[] getIntMultibox() {
+ return (this.intMultibox);
+ }
+
+ public void setIntMultibox(int intMultibox[]) {
+ this.intMultibox = intMultibox;
+ }
+
/**
* An integer property.
*/
@@ -200,7 +212,7 @@
/**
- * A String array that is accessed as an array as well as indexed.
+ * String arrays that are accessed as an array as well as indexed.
*/
private String stringArray[] =
{ "String 0", "String 1", "String 2", "String 3", "String 4" };
@@ -213,14 +225,27 @@
this.stringArray = stringArray;
}
+ private String stringIndexed[] =
+ { "String 0", "String 1", "String 2", "String 3", "String 4" };
+
public String getStringIndexed(int index) {
- return (stringArray[index]);
+ return (stringIndexed[index]);
}
public void setStringIndexed(int index, String value) {
- stringArray[index] = value;
+ stringIndexed[index] = value;
+ }
+
+
+ private String stringMultibox[] = new String[0];
+
+ public String[] getStringMultibox() {
+ return (this.stringMultibox);
}
+ public void setStringMultibox(String stringMultibox[]) {
+ this.stringMultibox = stringMultibox;
+ }
/**
* A String property.
@@ -245,6 +270,8 @@
public void reset(ActionMapping mapping, HttpServletRequest request) {
booleanProperty = false;
+ intMultibox = new int[0];
+ stringMultibox = new String[0];
if (nested != null)
nested.reset(mapping, request);
1.2 +20 -20 jakarta-struts/web/test/html-multibox.jsp
Index: html-multibox.jsp
===================================================================
RCS file: /home/cvs/jakarta-struts/web/test/html-multibox.jsp,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- html-multibox.jsp 2001/01/08 22:20:51 1.1
+++ html-multibox.jsp 2001/01/10 01:54:22 1.2
@@ -27,55 +27,55 @@
<tr>
<th align="right">String 0</th>
<td align="left">
- <html:multibox property="stringArray" value="String 0"/>
+ <html:multibox property="stringMultibox" value="String 0"/>
</td>
<th align="right">(nested) String 0</th>
<td align="left">
- <html:multibox property="nested.stringArray" value="String 0"/>
+ <html:multibox property="nested.stringMultibox" value="String 0"/>
</td>
</tr>
<tr>
<th align="right">String 1</th>
<td align="left">
- <html:multibox property="stringArray" value="String 1"/>
+ <html:multibox property="stringMultibox" value="String 1"/>
</td>
<th align="right">(nested) String 1</th>
<td align="left">
- <html:multibox property="nested.stringArray" value="String 1"/>
+ <html:multibox property="nested.stringMultibox" value="String 1"/>
</td>
</tr>
<tr>
<th align="right">String 2</th>
<td align="left">
- <html:multibox property="stringArray" value="String 2"/>
+ <html:multibox property="stringMultibox" value="String 2"/>
</td>
<th align="right">(nested) String 2</th>
<td align="left">
- <html:multibox property="nested.stringArray" value="String 2"/>
+ <html:multibox property="nested.stringMultibox" value="String 2"/>
</td>
</tr>
<tr>
<th align="right">String 3</th>
<td align="left">
- <html:multibox property="stringArray" value="String 3"/>
+ <html:multibox property="stringMultibox" value="String 3"/>
</td>
<th align="right">(nested) String 3</th>
<td align="left">
- <html:multibox property="nested.stringArray" value="String 3"/>
+ <html:multibox property="nested.stringMultibox" value="String 3"/>
</td>
</tr>
<tr>
<th align="right">String 4</th>
<td align="left">
- <html:multibox property="stringArray" value="String 4"/>
+ <html:multibox property="stringMultibox" value="String 4"/>
</td>
<th align="right">(nested) String 4</th>
<td align="left">
- <html:multibox property="nested.stringArray" value="String 4"/>
+ <html:multibox property="nested.stringMultibox" value="String 4"/>
</td>
</tr>
@@ -86,55 +86,55 @@
<tr>
<th align="right">0</th>
<td align="left">
- <html:multibox property="intArray" value="0"/>
+ <html:multibox property="intMultibox" value="0"/>
</td>
<th align="right">(nested) 0</th>
<td align="left">
- <html:multibox property="nested.intArray" value="0"/>
+ <html:multibox property="nested.intMultibox" value="0"/>
</td>
</tr>
<tr>
<th align="right">10</th>
<td align="left">
- <html:multibox property="intArray" value="10"/>
+ <html:multibox property="intMultibox" value="10"/>
</td>
<th align="right">(nested) 10</th>
<td align="left">
- <html:multibox property="nested.intArray" value="10"/>
+ <html:multibox property="nested.intMultibox" value="10"/>
</td>
</tr>
<tr>
<th align="right">20</th>
<td align="left">
- <html:multibox property="intArray" value="20"/>
+ <html:multibox property="intMultibox" value="20"/>
</td>
<th align="right">(nested) 20</th>
<td align="left">
- <html:multibox property="nested.intArray" value="20"/>
+ <html:multibox property="nested.intMultibox" value="20"/>
</td>
</tr>
<tr>
<th align="right">30</th>
<td align="left">
- <html:multibox property="intArray" value="30"/>
+ <html:multibox property="intMultibox" value="30"/>
</td>
<th align="right">(nested) 30</th>
<td align="left">
- <html:multibox property="nested.intArray" value="30"/>
+ <html:multibox property="nested.intMultibox" value="30"/>
</td>
</tr>
<tr>
<th align="right">40</th>
<td align="left">
- <html:multibox property="intArray" value="40"/>
+ <html:multibox property="intMultibox" value="40"/>
</td>
<th align="right">(nested) 40</th>
<td align="left">
- <html:multibox property="nested.intArray" value="40"/>
+ <html:multibox property="nested.intMultibox" value="40"/>
</td>
</tr>
1.4 +24 -18 jakarta-struts/web/test/html-setters.jsp
Index: html-setters.jsp
===================================================================
RCS file: /home/cvs/jakarta-struts/web/test/html-setters.jsp,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- html-setters.jsp 2001/01/08 20:37:26 1.3
+++ html-setters.jsp 2001/01/10 01:54:22 1.4
@@ -20,6 +20,10 @@
<table border="0" width="100%">
<tr>
+ <th align="center" colspan="4">Scalar Properties</th align="center">
+ </tr>
+
+ <tr>
<th align="right">booleanProperty</th>
<td align="left">
<html:checkbox property="booleanProperty"/>
@@ -84,52 +88,54 @@
<html:text property="nested.stringProperty" size="32"/>
</td>
</tr>
+
+ <tr>
+ <th align="center" colspan="4">Indexed Properties</th align="center">
+ </tr>
-<%--
<tr>
- <th align="right">intArray[0]</th>
+ <th align="right">intIndexed[0]</th>
<td align="left">
- <html:text property="intArray[0]" size="32"/>
+ <html:text property="intIndexed[0]" size="32"/>
</td>
- <th align="right">nested.intArray[0]</th>
+ <th align="right">nested.intIndexed[0]</th>
<td align="left">
- <html:text property="nested.intArray[0]" size="32"/>
+ <html:text property="nested.intIndexed[0]" size="32"/>
</td>
</tr>
<tr>
- <th align="right">intArray[1]</th>
+ <th align="right">intIndexed[1]</th>
<td align="left">
- <html:text property="intArray[1]" size="32"/>
+ <html:text property="intIndexed[1]" size="32"/>
</td>
- <th align="right">nested.intArray[1]</th>
+ <th align="right">nested.intIndexed[1]</th>
<td align="left">
- <html:text property="nested.intArray[1]" size="32"/>
+ <html:text property="nested.intIndexed[1]" size="32"/>
</td>
</tr>
<tr>
- <th align="right">stringArray[0]</th>
+ <th align="right">stringIndexed[0]</th>
<td align="left">
- <html:text property="stringArray[0]" size="32"/>
+ <html:text property="stringIndexed[0]" size="32"/>
</td>
- <th align="right">nested.stringArray[0]</th>
+ <th align="right">nested.stringIndexed[0]</th>
<td align="left">
- <html:text property="nested.stringArray[0]" size="32"/>
+ <html:text property="nested.stringIndexed[0]" size="32"/>
</td>
</tr>
<tr>
- <th align="right">stringArray[1]</th>
+ <th align="right">stringIndexed[1]</th>
<td align="left">
- <html:text property="stringArray[1]" size="32"/>
+ <html:text property="stringIndexed[1]" size="32"/>
</td>
- <th align="right">nested.stringArray[1]</th>
+ <th align="right">nested.stringIndexed[1]</th>
<td align="left">
- <html:text property="nested.stringArray[1]" size="32"/>
+ <html:text property="nested.stringIndexed[1]" size="32"/>
</td>
</tr>
---%>
<tr>
<td> </td>