You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by hr...@apache.org on 2005/11/12 17:52:23 UTC
svn commit: r332798 - in /struts/core/trunk: conf/java/
src/java/org/apache/struts/action/ src/java/org/apache/struts/config/
src/java/org/apache/struts/mock/ src/test/org/apache/struts/action/
Author: hrabago
Date: Sat Nov 12 08:52:08 2005
New Revision: 332798
URL: http://svn.apache.org/viewcvs?rev=332798&view=rev
Log:
Add support for resetting DynaActionForm property values through configuration.
Modified:
struts/core/trunk/conf/java/struts-config_1_3.dtd
struts/core/trunk/src/java/org/apache/struts/action/DynaActionForm.java
struts/core/trunk/src/java/org/apache/struts/config/FormPropertyConfig.java
struts/core/trunk/src/java/org/apache/struts/mock/MockHttpServletRequest.java
struts/core/trunk/src/test/org/apache/struts/action/TestDynaActionForm.java
struts/core/trunk/src/test/org/apache/struts/action/TestDynaActionFormClass.java
Modified: struts/core/trunk/conf/java/struts-config_1_3.dtd
URL: http://svn.apache.org/viewcvs/struts/core/trunk/conf/java/struts-config_1_3.dtd?rev=332798&r1=332797&r2=332798&view=diff
==============================================================================
--- struts/core/trunk/conf/java/struts-config_1_3.dtd (original)
+++ struts/core/trunk/conf/java/struts-config_1_3.dtd Sat Nov 12 08:52:08 2005
@@ -152,9 +152,18 @@
objects initialized to the zero-argument instantiation of that
object class. For example, Strings will be initialized to ""
-
name The name of the JavaBean property described by this element.
+ reset The flag that indicates when this property should be reset
+ to its "initial" value when the form's "reset()" method is
+ called. If this is set to "true", the property is always
+ reset when "reset()" is called. This can also be set to
+ one or more HTTP methods, such as GET or POST. In such a
+ case, the property will be reset only when the HTTP method
+ used for the request being processed is included in this
+ attribute's value(s). Multiple HTTP methods can be
+ specified by separating them with whitespace or commas.
+
size The number of array elements to create if the value of the
"type" attribute specifies an array, but there is no value
specified for the "initial" attribute.
@@ -167,6 +176,7 @@
<!ATTLIST form-property className %ClassName; #IMPLIED>
<!ATTLIST form-property initial CDATA #IMPLIED>
<!ATTLIST form-property name %PropName; #REQUIRED>
+<!ATTLIST form-property reset %Boolean; #IMPLIED>
<!ATTLIST form-property size %Integer; #IMPLIED>
<!ATTLIST form-property type %ClassName; #REQUIRED>
Modified: struts/core/trunk/src/java/org/apache/struts/action/DynaActionForm.java
URL: http://svn.apache.org/viewcvs/struts/core/trunk/src/java/org/apache/struts/action/DynaActionForm.java?rev=332798&r1=332797&r2=332798&view=diff
==============================================================================
--- struts/core/trunk/src/java/org/apache/struts/action/DynaActionForm.java (original)
+++ struts/core/trunk/src/java/org/apache/struts/action/DynaActionForm.java Sat Nov 12 08:52:08 2005
@@ -23,6 +23,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
+import java.util.StringTokenizer;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
@@ -130,21 +131,57 @@
/**
- * <p>Reset bean properties to their default state, as needed. This method
- * is called before the properties are repopulated by the controller.</p>
- *
- * <p>The default implementation (since Struts 1.1) does nothing.
- * Subclasses may override this method to reset bean properties to
- * default values, or the <code>initialize</code> method may be used to
- * initialize property values to those provided in the form property
- * configuration information (which was the behavior of
- * this method in some release candidates).</p>
+ * <p>Reset the properties to their <code>initial</code> value if their
+ * <code>reset</code> configuration is set to true or if <code>reset</code>
+ * is set to a list of HTTP request methods that includes the method of
+ * given <code>request</code> object.</p>
*
* @param mapping The mapping used to select this instance
* @param request The servlet request we are processing
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
- super.reset(mapping, request);
+
+ String name = getDynaClass().getName();
+ if (name == null) {
+ return;
+ }
+
+ FormBeanConfig config =
+ mapping.getModuleConfig().findFormBeanConfig(name);
+ if (config == null) {
+ return;
+ }
+
+ // look for properties we should reset
+ FormPropertyConfig[] props = config.findFormPropertyConfigs();
+ for (int i = 0; i < props.length; i++) {
+
+ String resetValue = props[i].getReset();
+ // skip this property if there's no reset value
+ if ((resetValue == null) || (resetValue.length() <= 0)) {
+ continue;
+ }
+
+ boolean reset = Boolean.valueOf(resetValue).booleanValue();
+ if (!reset) {
+ // check for the request method
+
+ // use a StringTokenizer with the default delimiters + a comma
+ StringTokenizer st = new StringTokenizer(resetValue, ", \t\n\r\f");
+ while (st.hasMoreTokens()) {
+ String token = st.nextToken();
+ if (token.equalsIgnoreCase(request.getMethod())) {
+ reset = true;
+ break;
+ }
+ }
+ }
+
+ if (reset) {
+ set(props[i].getName(), props[i].initial());
+ }
+ }
+
}
Modified: struts/core/trunk/src/java/org/apache/struts/config/FormPropertyConfig.java
URL: http://svn.apache.org/viewcvs/struts/core/trunk/src/java/org/apache/struts/config/FormPropertyConfig.java?rev=332798&r1=332797&r2=332798&view=diff
==============================================================================
--- struts/core/trunk/src/java/org/apache/struts/config/FormPropertyConfig.java (original)
+++ struts/core/trunk/src/java/org/apache/struts/config/FormPropertyConfig.java Sat Nov 12 08:52:08 2005
@@ -79,16 +79,54 @@
* @param name Name of this property
* @param type Fully qualified class name of this property
* @param initial Initial value of this property (if any)
+ * @param reset The conditions under which this property will be reset
+ * to its initial value.
+ */
+ public FormPropertyConfig(String name, String type,
+ String initial, String reset) {
+
+ this(name, type, initial, reset, 0);
+
+ }
+
+
+ /**
+ * Constructor that preconfigures the relevant properties.
+ *
+ * @param name Name of this property
+ * @param type Fully qualified class name of this property
+ * @param initial Initial value of this property (if any)
* @param size Size of the array to be created if this property is an
* array with no defined initial value
*/
public FormPropertyConfig(String name, String type,
String initial, int size) {
+ this(name, type, initial, null, size);
+
+ }
+
+
+ /**
+ * Constructor that preconfigures the relevant properties.
+ *
+ * @param name Name of this property
+ * @param type Fully qualified class name of this property
+ * @param initial Initial value of this property (if any)
+ * @param size Size of the array to be created if this property is an
+ * array with no defined initial value
+ * @param reset The conditions under which this property will be reset
+ * to its initial value.
+ */
+ public FormPropertyConfig(String name, String type,
+ String initial, String reset,
+ int size) {
+
super();
setName(name);
setType(type);
setInitial(initial);
+ setReset(reset);
setSize(size);
}
@@ -136,6 +174,29 @@
/**
+ * <p>The conditions under which the property described by this element
+ * should be reset to its <code>initial</code> value when the form's
+ * <code>reset</code> method is called.</p>
+ * <p>This may be set to true (to always reset the property) or a
+ * comma-separated list of HTTP request methods.</p>
+ * @since Struts 1.3
+ */
+ protected String reset = null;
+
+ public String getReset() {
+ return (this.reset);
+ }
+
+ public void setReset(String reset) {
+ if (configured) {
+ throw new IllegalStateException("Configuration is frozen");
+ }
+ this.reset = reset;
+ }
+
+
+
+ /**
* <p>The size of the array to be created if this property is an array
* type and there is no specified <code>initial</code> value. This
* value must be non-negative.</p>
@@ -372,6 +433,8 @@
sb.append(this.type);
sb.append(",initial=");
sb.append(this.initial);
+ sb.append(",reset=");
+ sb.append(this.reset);
sb.append("]");
return (sb.toString());
Modified: struts/core/trunk/src/java/org/apache/struts/mock/MockHttpServletRequest.java
URL: http://svn.apache.org/viewcvs/struts/core/trunk/src/java/org/apache/struts/mock/MockHttpServletRequest.java?rev=332798&r1=332797&r2=332798&view=diff
==============================================================================
--- struts/core/trunk/src/java/org/apache/struts/mock/MockHttpServletRequest.java (original)
+++ struts/core/trunk/src/java/org/apache/struts/mock/MockHttpServletRequest.java Sat Nov 12 08:52:08 2005
@@ -143,6 +143,12 @@
* The HttpSession with which we are associated.
*/
protected HttpSession session = null;
+
+
+ /**
+ * The HTTP request method.
+ */
+ protected String method = null;
// --------------------------------------------------------- Public Methods
@@ -170,6 +176,11 @@
public void setLocale(Locale locale) {
this.locale = locale;
}
+
+
+ public void setMethod(String method) {
+ this.method = method;
+ }
public void setPathElements(String contextPath, String servletPath,
@@ -233,7 +244,7 @@
public String getMethod() {
- throw new UnsupportedOperationException();
+ return (method);
}
Modified: struts/core/trunk/src/test/org/apache/struts/action/TestDynaActionForm.java
URL: http://svn.apache.org/viewcvs/struts/core/trunk/src/test/org/apache/struts/action/TestDynaActionForm.java?rev=332798&r1=332797&r2=332798&view=diff
==============================================================================
--- struts/core/trunk/src/test/org/apache/struts/action/TestDynaActionForm.java (original)
+++ struts/core/trunk/src/test/org/apache/struts/action/TestDynaActionForm.java Sat Nov 12 08:52:08 2005
@@ -32,6 +32,7 @@
import org.apache.struts.config.FormBeanConfig;
import org.apache.struts.config.ModuleConfig;
import org.apache.struts.config.impl.ModuleConfigImpl;
+import org.apache.struts.mock.MockHttpServletRequest;
/**
@@ -611,6 +612,61 @@
assertTrue("Can not see unknown key",
!dynaForm.contains("mappedProperty", "Unknown Key"));
+ }
+
+
+ /**
+ * Test the reset method when the request method is GET.
+ */
+ public void testResetGet() {
+ // set a choice set of props with non-initial values
+ dynaForm.set("booleanProperty", Boolean.FALSE);
+ dynaForm.set("booleanSecond", Boolean.FALSE);
+ dynaForm.set("doubleProperty", new Double(456.0));
+ dynaForm.set("floatProperty", new Float((float) 456.0));
+ dynaForm.set("intProperty", new Integer(456));
+
+ MockHttpServletRequest request = new MockHttpServletRequest();
+ request.setMethod("GET");
+ dynaForm.reset(mapping, request);
+
+ assertEquals("booleanProperty should be reset", Boolean.TRUE,
+ (Boolean) dynaForm.get("booleanProperty"));
+ assertEquals("booleanSecond should be reset", Boolean.TRUE,
+ (Boolean) dynaForm.get("booleanSecond"));
+ assertEquals("doubleProperty should be reset", new Double(321.0),
+ (Double) dynaForm.get("doubleProperty"));
+ assertEquals("floatProperty should NOT be reset", new Float((float) 456.0),
+ (Float) dynaForm.get("floatProperty"));
+ assertEquals("intProperty should NOT be reset", new Integer(456),
+ (Integer) dynaForm.get("intProperty"));
+ }
+
+ /**
+ * Test the reset method when the request method is GET.
+ */
+ public void testResetPost() {
+ // set a choice set of props with non-initial values
+ dynaForm.set("booleanProperty", Boolean.FALSE);
+ dynaForm.set("booleanSecond", Boolean.FALSE);
+ dynaForm.set("doubleProperty", new Double(456.0));
+ dynaForm.set("floatProperty", new Float((float) 456.0));
+ dynaForm.set("intProperty", new Integer(456));
+
+ MockHttpServletRequest request = new MockHttpServletRequest();
+ request.setMethod("POST");
+ dynaForm.reset(mapping, request);
+
+ assertEquals("booleanProperty should be reset", Boolean.TRUE,
+ (Boolean) dynaForm.get("booleanProperty"));
+ assertEquals("booleanSecond should be reset", Boolean.TRUE,
+ (Boolean) dynaForm.get("booleanSecond"));
+ assertEquals("doubleProperty should NOT be reset", new Double(456),
+ (Double) dynaForm.get("doubleProperty"));
+ assertEquals("floatProperty should be reset", new Float((float) 123.0),
+ (Float) dynaForm.get("floatProperty"));
+ assertEquals("intProperty should NOT be reset", new Integer(456),
+ (Integer) dynaForm.get("intProperty"));
}
Modified: struts/core/trunk/src/test/org/apache/struts/action/TestDynaActionFormClass.java
URL: http://svn.apache.org/viewcvs/struts/core/trunk/src/test/org/apache/struts/action/TestDynaActionFormClass.java?rev=332798&r1=332797&r2=332798&view=diff
==============================================================================
--- struts/core/trunk/src/test/org/apache/struts/action/TestDynaActionFormClass.java (original)
+++ struts/core/trunk/src/test/org/apache/struts/action/TestDynaActionFormClass.java Sat Nov 12 08:52:08 2005
@@ -85,10 +85,10 @@
* creating our <code>FormBeanConfig</code>.
*/
protected static final FormPropertyConfig[] dynaProperties = {
- new FormPropertyConfig("booleanProperty", "boolean", "true"),
- new FormPropertyConfig("booleanSecond", "boolean", "true"),
- new FormPropertyConfig("doubleProperty", "double", "321.0"),
- new FormPropertyConfig("floatProperty", "float", "123.0"),
+ new FormPropertyConfig("booleanProperty", "boolean", "true", "true"),
+ new FormPropertyConfig("booleanSecond", "boolean", "true", "true"),
+ new FormPropertyConfig("doubleProperty", "double", "321.0", "GET"),
+ new FormPropertyConfig("floatProperty", "float", "123.0", "POST, HEAD"),
new FormPropertyConfig("intArray", "int[]",
"{ 0, 10,20, \"30\" '40' }"),
new FormPropertyConfig("intIndexed", "int[]",
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org