This is an automated email from the ASF dual-hosted git repository.

svenmeier pushed a commit to branch wicket-8.x
in repository https://gitbox.apache.org/repos/asf/wicket.git


The following commit(s) were added to refs/heads/wicket-8.x by this push:
     new 2298bdf  WICKET-6642 revert SubmitLink to request Form url
2298bdf is described below

commit 2298bdff67c39d5a4d61b6415e2502d0bab231ca
Author: Sven Meier <svenme...@apache.org>
AuthorDate: Mon Mar 11 10:14:48 2019 +0100

    WICKET-6642 revert SubmitLink to request Form url
    
    and use findSubmittingButton once again
---
 .../org/apache/wicket/markup/html/form/Form.java   | 43 ++++++++++++-
 .../apache/wicket/markup/html/form/SubmitLink.java | 11 ++--
 .../wicket/markup/html/form/SubmitLinkTest.java    | 75 ++++++++++++++++++++++
 .../wicket/stateless/StatelessFormUrlTest.java     |  2 +-
 4 files changed, 123 insertions(+), 8 deletions(-)

diff --git 
a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Form.java 
b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Form.java
index b26bec3..551eb3c 100644
--- a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Form.java
+++ b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Form.java
@@ -423,6 +423,8 @@ public class Form<T> extends WebMarkupContainer
        }
 
        /**
+        * THIS METHOD IS NOT PART OF THE WICKET PUBLIC API. DO NOT USE IT!
+        * <p>
         * Gets the IFormSubmittingComponent which submitted this form.
         * 
         * @return The component which submitted this form, or null if the 
processing was not triggered
@@ -513,7 +515,7 @@ public class Form<T> extends WebMarkupContainer
        }
 
        /**
-        * Generate a piece of JavaScript that submits the form to the given 
URL.
+        * Generate a piece of JavaScript that submits the form to the given 
URL of an {@link IRequestListener}.
         * 
         * Warning: This code should only be called in the rendering phase for 
form components inside
         * the form because it uses the css/javascript id of the form which can 
be stored in the markup.
@@ -534,7 +536,8 @@ public class Form<T> extends WebMarkupContainer
                        
                        // parameter must be sent as hidden field, as it would 
be ignored in the action URL
                        int i = action.indexOf('?');
-                       if (i != -1) {
+                       if (i != -1)
+                       {
                                
writeParamsAsHiddenFields(Strings.split(action.substring(i + 1), '&'), buffer);
                                
                                action = action.substring(0, i);
@@ -549,6 +552,40 @@ public class Form<T> extends WebMarkupContainer
        }
 
        /**
+        * Generate a piece of JavaScript that submits the form with the given 
{@link IFormSubmittingComponent}.
+        * 
+        * @param submitter
+        *            the submitter
+        * @return the javascript code that submits the form.
+        * 
+        * @see #findSubmittingButton()
+        */
+       public final CharSequence getJsForSubmitter(IFormSubmittingComponent 
submitter)
+       {
+               Form<?> root = getRootForm();
+
+               String param = submitter.getInputName() + "=x";
+
+               AppendingStringBuffer buffer = new AppendingStringBuffer();
+               buffer.append(String.format("var f = 
document.getElementById('%s');", root.getMarkupId()));
+               if (root.encodeUrlInHiddenFields())
+               {
+                       
buffer.append(String.format("document.getElementById('%s').innerHTML += '", 
root.getHiddenFieldsId()));
+                       
+                       writeParamsAsHiddenFields(new String[] {param}, buffer);
+                       
+                       buffer.append("';");
+               }
+               else
+               {
+                       String action = root.getActionUrl().toString();
+                       buffer.append("f.action += '" + (action.indexOf('?') > 
-1 ? '&' : '?') + param + "';");
+               }
+               buffer.append("f.submit();");
+               return buffer;
+       }
+
+       /**
         * Gets the maximum size for uploads. If null, the setting
         * {@link 
org.apache.wicket.settings.ApplicationSettings#getDefaultMaximumUploadSize()} 
is used.
         * 
@@ -743,7 +780,7 @@ public class Form<T> extends WebMarkupContainer
                        // Tells FormComponents that a new user input has come
                        inputChanged();
 
-                       // First, see if the processing was triggered by a 
Wicket IFormSubmittingComponent
+                       // First, see if the processing was triggered by a 
IFormSubmittingComponent
                        if (submitter == null)
                        {
                                submitter = findSubmittingButton();
diff --git 
a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/SubmitLink.java 
b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/SubmitLink.java
index f7f181b..d83726e 100644
--- 
a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/SubmitLink.java
+++ 
b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/SubmitLink.java
@@ -19,7 +19,6 @@ package org.apache.wicket.markup.html.form;
 import org.apache.wicket.IRequestListener;
 import org.apache.wicket.markup.ComponentTag;
 import org.apache.wicket.model.IModel;
-import org.apache.wicket.request.mapper.parameter.PageParameters;
 
 /**
  * A link which can be used exactly like a Button to submit a Form. The 
onclick of the link will use
@@ -214,8 +213,7 @@ public class SubmitLink extends AbstractSubmitLink 
implements IRequestListener
                                script.append("if (typeof ff.onsubmit === 
'function' && ff.onsubmit() == false) return false;");
                        }
                        
-                       CharSequence url = urlForListener(new PageParameters());
-                       script.append(root.getJsForListenerUrl(url));
+                       script.append(root.getJsForSubmitter(this));
                        script.append("return false;");
                        
                        return script;
@@ -226,10 +224,15 @@ public class SubmitLink extends AbstractSubmitLink 
implements IRequestListener
                }
        }
 
+       /**
+        * @deprecated do not override, will be removed in Wicket 9
+        */
+       @Deprecated
        @Override
        public void onRequest()
        {
-               getForm().onFormSubmitted(this);
+               // with WICKET-6642 SubmitLink was reverted to use a request to 
the form once again,
+               // see Form#getJsForSubmitter(IFormSubmittingComponent)
        }
 
        /**
diff --git 
a/wicket-core/src/test/java/org/apache/wicket/markup/html/form/SubmitLinkTest.java
 
b/wicket-core/src/test/java/org/apache/wicket/markup/html/form/SubmitLinkTest.java
new file mode 100644
index 0000000..027a3fd
--- /dev/null
+++ 
b/wicket-core/src/test/java/org/apache/wicket/markup/html/form/SubmitLinkTest.java
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.markup.html.form;
+
+
+import org.apache.wicket.MarkupContainer;
+import org.apache.wicket.markup.IMarkupResourceStreamProvider;
+import org.apache.wicket.markup.html.WebPage;
+import org.apache.wicket.util.resource.IResourceStream;
+import org.apache.wicket.util.resource.StringResourceStream;
+import org.apache.wicket.util.tester.WicketTestCase;
+import org.junit.Test;
+
+/**
+ * Test for {@link SubmitLink}.
+ */
+public class SubmitLinkTest extends WicketTestCase
+{
+       /**
+        * WICKET-6642
+        */
+       @Test
+       public void getFormSubmitter()
+       {
+               TestPage testPage = new TestPage();
+               tester.startPage(testPage);
+               
+               tester.clickLink("form:link");
+       }
+
+       /** */
+       public static class TestPage extends WebPage implements 
IMarkupResourceStreamProvider
+       {
+               private static final long serialVersionUID = 1L;
+               Form<Void> form;
+               SubmitLink link;
+
+               /** */
+               TestPage()
+               {
+                       add(form = new Form<Void>("form")
+                       {
+                               protected void onSubmit()
+                               {
+                                       super.onSubmit();
+                                       
+                                       assertEquals(link, 
findSubmittingButton());
+                               }
+                       });
+                       form.add(link = new SubmitLink("link"));
+               }
+
+               @Override
+               public IResourceStream getMarkupResourceStream(MarkupContainer 
container,
+                       Class<?> containerClass)
+               {
+                       return new StringResourceStream("<html><body>"
+                               + "<form wicket:id=\"form\"><a 
wicket:id=\"link\"></a></form></body></html>");
+               }
+       }
+}
diff --git 
a/wicket-core/src/test/java/org/apache/wicket/stateless/StatelessFormUrlTest.java
 
b/wicket-core/src/test/java/org/apache/wicket/stateless/StatelessFormUrlTest.java
index 17b3f8d..d94c094 100644
--- 
a/wicket-core/src/test/java/org/apache/wicket/stateless/StatelessFormUrlTest.java
+++ 
b/wicket-core/src/test/java/org/apache/wicket/stateless/StatelessFormUrlTest.java
@@ -56,7 +56,7 @@ public class StatelessFormUrlTest extends WicketTestCase
        public void submitLinkInputNameNotEncodedIntoFormAction()
        {
                
tester.executeUrl("?0-1.IFormSubmitListener-form&text=newValue&submitLink=x");
-               
assertFalse(tester.getLastResponseAsString().contains("submitLink=x"));
+               assertEquals("./?-1.-form", 
tester.getTagById("form1").getAttribute("action"));
        }
 
        /**

Reply via email to