[prev in list] [next in list] [prev in thread] [next in thread] 

List:       struts-dev
Subject:    [struts] 01/01: WW-5302 Evaluates attributes before using them to generate the id attribute
From:       lukaszlenart () apache ! org
Date:       2023-04-10 9:01:57
Message-ID: 20230410090156.57C7D44007A () gitbox2-he-fi ! apache ! org
[Download RAW message or body]

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

lukaszlenart pushed a commit to branch WW-5302-unevaluated-id
in repository https://gitbox.apache.org/repos/asf/struts.git

commit a6608c16d24afcb2160af726bf87ddf1ad1cb845
Author: Lukasz Lenart <lukaszlenart@apache.org>
AuthorDate: Sun Apr 9 11:50:47 2023 +0200

    WW-5302 Evaluates attributes before using them to generate the id attribute
---
 .../org/apache/struts2/components/FormButton.java  | 18 +++++-----
 core/src/main/resources/template/simple/submit.ftl |  2 +-
 .../apache/struts2/components/FormButtonTest.java  | 29 ++++++++++++----
 .../apache/struts2/views/jsp/ui/SubmitTest.java    | 39 ++++++++++++++++++++--
 .../org/apache/struts2/views/jsp/ui/Submit-13.txt  |  1 +
 .../org/apache/struts2/views/jsp/ui/Submit-14.txt  |  1 +
 6 files changed, 72 insertions(+), 18 deletions(-)

diff --git a/core/src/main/java/org/apache/struts2/components/FormButton.java \
b/core/src/main/java/org/apache/struts2/components/FormButton.java index \
                d9e75f1e4..0ed08d47b 100644
--- a/core/src/main/java/org/apache/struts2/components/FormButton.java
+++ b/core/src/main/java/org/apache/struts2/components/FormButton.java
@@ -96,33 +96,33 @@ public abstract class FormButton extends ClosingUIBean {
      * </ol>
      */
     protected void populateComponentHtmlId(Form form) {
-        String _tmp_id = "";
+        String tmpId = "";
         if (id != null) {
             // this check is needed for backwards compatibility with 2.1.x
-            _tmp_id = findString(id);
+            tmpId = findString(id);
         } else {
             if (form != null && form.getParameters().get("id") != null) {
-                _tmp_id = _tmp_id + form.getParameters().get("id").toString() + "_";
+                tmpId = tmpId + form.getParameters().get("id").toString() + "_";
             }
             if (name != null) {
-                _tmp_id = _tmp_id + escape(name);
+                tmpId = tmpId + escape(findString(name));
             } else if (action != null || method != null) {
                 if (action != null) {
-                    _tmp_id = _tmp_id + escape(action);
+                    tmpId = tmpId + escape(findString(action));
                 }
                 if (method != null) {
-                    _tmp_id = _tmp_id + "_" + escape(method);
+                    tmpId = tmpId + "_" + escape(findString(method));
                 }
             } else {
                 // if form is null, this component is used, without a form, i guess
                 // there's not much we could do then.
                 if (form != null) {
-                    _tmp_id = _tmp_id + form.getSequence();
+                    tmpId = tmpId + form.getSequence();
                 }
             }
         }
-        addParameter("id", _tmp_id);
-        addParameter("escapedId", escape(_tmp_id));
+        addParameter("id", tmpId);
+        addParameter("escapedId", escape(tmpId));
     }
 
     /**
diff --git a/core/src/main/resources/template/simple/submit.ftl \
b/core/src/main/resources/template/simple/submit.ftl index 48026b3e5..a932103f4 \
                100644
--- a/core/src/main/resources/template/simple/submit.ftl
+++ b/core/src/main/resources/template/simple/submit.ftl
@@ -88,4 +88,4 @@
 <#include "/${parameters.templateDir}/${parameters.expandTheme}/common-attributes.ftl" \
/>  <#include "/${parameters.templateDir}/${parameters.expandTheme}/dynamic-attributes.ftl" \
/>  />
-</#if>
\ No newline at end of file
+</#if>
diff --git a/core/src/test/java/org/apache/struts2/components/FormButtonTest.java \
b/core/src/test/java/org/apache/struts2/components/FormButtonTest.java index \
                bf0fe952d..a390682d5 100644
--- a/core/src/test/java/org/apache/struts2/components/FormButtonTest.java
+++ b/core/src/test/java/org/apache/struts2/components/FormButtonTest.java
@@ -18,6 +18,7 @@
  */
 package org.apache.struts2.components;
 
+import com.opensymphony.xwork2.TestBean;
 import org.apache.struts2.StrutsInternalTestCase;
 import org.springframework.mock.web.MockHttpServletRequest;
 import org.springframework.mock.web.MockHttpServletResponse;
@@ -31,7 +32,7 @@ import com.opensymphony.xwork2.util.ValueStack;
  */
 public class FormButtonTest extends StrutsInternalTestCase {
 
-    public void testPopulateComponentHtmlId1() throws Exception {
+    public void testPopulateComponentHtmlId1() {
         MockHttpServletRequest req = new MockHttpServletRequest();
         MockHttpServletResponse res = new MockHttpServletResponse();
         ValueStack stack = ActionContext.getContext().getValueStack();
@@ -47,7 +48,7 @@ public class FormButtonTest extends StrutsInternalTestCase {
         assertEquals("submitId", submit.getParameters().get("id"));
     }
 
-    public void testPopulateComponentHtmlId2() throws Exception {
+    public void testPopulateComponentHtmlId2() {
         MockHttpServletRequest req = new MockHttpServletRequest();
         MockHttpServletResponse res = new MockHttpServletResponse();
         ValueStack stack = ActionContext.getContext().getValueStack();
@@ -63,7 +64,7 @@ public class FormButtonTest extends StrutsInternalTestCase {
         assertEquals("formId_submitName", submit.getParameters().get("id"));
     }
 
-    public void testPopulateComponentHtmlId3() throws Exception {
+    public void testPopulateComponentHtmlId3() {
         MockHttpServletRequest req = new MockHttpServletRequest();
         MockHttpServletResponse res = new MockHttpServletResponse();
         ValueStack stack = ActionContext.getContext().getValueStack();
@@ -80,7 +81,7 @@ public class FormButtonTest extends StrutsInternalTestCase {
         assertEquals("formId_submitAction_submitMethod", \
submit.getParameters().get("id"));  }
 
-    public void testPopulateComponentHtmlId4() throws Exception {
+    public void testPopulateComponentHtmlId4() {
         MockHttpServletRequest req = new MockHttpServletRequest();
         MockHttpServletResponse res = new MockHttpServletResponse();
         ValueStack stack = ActionContext.getContext().getValueStack();
@@ -93,7 +94,7 @@ public class FormButtonTest extends StrutsInternalTestCase {
         assertEquals("submitId", submit.getParameters().get("id"));
     }
 
-    public void testPopulateComponentHtmlId5() throws Exception {
+    public void testPopulateComponentHtmlId5() {
         MockHttpServletRequest req = new MockHttpServletRequest();
         MockHttpServletResponse res = new MockHttpServletResponse();
         ValueStack stack = ActionContext.getContext().getValueStack();
@@ -106,7 +107,7 @@ public class FormButtonTest extends StrutsInternalTestCase {
         assertEquals("submitName", submit.getParameters().get("id"));
     }
 
-    public void testPopulateComponentHtmlId6() throws Exception {
+    public void testPopulateComponentHtmlId6() {
         MockHttpServletRequest req = new MockHttpServletRequest();
         MockHttpServletResponse res = new MockHttpServletResponse();
         ValueStack stack = ActionContext.getContext().getValueStack();
@@ -119,4 +120,20 @@ public class FormButtonTest extends StrutsInternalTestCase {
 
         assertEquals("submitAction_submitMethod", submit.getParameters().get("id"));
     }
+
+    public void testPopulateComponentHtmlId7() {
+        MockHttpServletRequest req = new MockHttpServletRequest();
+        MockHttpServletResponse res = new MockHttpServletResponse();
+        ValueStack stack = ActionContext.getContext().getValueStack();
+        TestBean bean = new TestBean();
+        bean.setName("secondAction");
+        stack.push(bean);
+
+        Submit submit = new Submit(stack, req, res);
+        submit.setName("%{name}");
+
+        submit.populateComponentHtmlId(null);
+
+        assertEquals("secondAction", submit.getParameters().get("id"));
+    }
 }
diff --git a/core/src/test/java/org/apache/struts2/views/jsp/ui/SubmitTest.java \
b/core/src/test/java/org/apache/struts2/views/jsp/ui/SubmitTest.java index \
                99eff80d5..2e80b4656 100644
--- a/core/src/test/java/org/apache/struts2/views/jsp/ui/SubmitTest.java
+++ b/core/src/test/java/org/apache/struts2/views/jsp/ui/SubmitTest.java
@@ -18,16 +18,15 @@
  */
 package org.apache.struts2.views.jsp.ui;
 
+import com.opensymphony.xwork2.TestBean;
 import org.apache.struts2.TestAction;
 import org.apache.struts2.views.jsp.AbstractUITagTest;
 
 import java.util.HashMap;
 import java.util.Map;
 
-
 /**
  * Unit test for {@link SubmitTag}.
- *
  */
 public class SubmitTest extends AbstractUITagTest {
 
@@ -699,4 +698,40 @@ public class SubmitTest extends AbstractUITagTest {
 
         verify(TextFieldTag.class.getResource("Submit-12.txt"));
     }
+
+    public void testSubmitWithGeneratedId_shouldUseEvaluatedName() throws Exception \
{ +        TestAction testAction = (TestAction) action;
+        testAction.setFoo("entryEdit");
+
+        SubmitTag tag = new SubmitTag();
+        tag.setTheme("simple");
+        tag.setPageContext(pageContext);
+        tag.setName("%{foo}!saveDraft");
+        tag.setValue("Save");
+
+        tag.doStartTag();
+        tag.doEndTag();
+
+        verify(TextFieldTag.class.getResource("Submit-13.txt"));
+    }
+
+    public void testSubmitWithGeneratedId_shouldUseEvaluatedAction() throws \
Exception { +        TestAction testAction = (TestAction) action;
+        testAction.setFoo("entryEdit");
+
+        TestBean bean = new TestBean();
+        bean.setName("mainAction");
+        stack.push(bean);
+
+        SubmitTag tag = new SubmitTag();
+        tag.setTheme("simple");
+        tag.setPageContext(pageContext);
+        tag.setAction("%{name}!saveDraft");
+        tag.setValue("Save");
+
+        tag.doStartTag();
+        tag.doEndTag();
+
+        verify(TextFieldTag.class.getResource("Submit-14.txt"));
+    }
 }
diff --git a/core/src/test/resources/org/apache/struts2/views/jsp/ui/Submit-13.txt \
b/core/src/test/resources/org/apache/struts2/views/jsp/ui/Submit-13.txt new file mode \
100644 index 000000000..939463118
--- /dev/null
+++ b/core/src/test/resources/org/apache/struts2/views/jsp/ui/Submit-13.txt
@@ -0,0 +1 @@
+<input type="submit" value="Save" id="entryEdit_saveDraft" \
                name="entryEdit!saveDraft"/>
diff --git a/core/src/test/resources/org/apache/struts2/views/jsp/ui/Submit-14.txt \
b/core/src/test/resources/org/apache/struts2/views/jsp/ui/Submit-14.txt new file mode \
100644 index 000000000..481c5472e
--- /dev/null
+++ b/core/src/test/resources/org/apache/struts2/views/jsp/ui/Submit-14.txt
@@ -0,0 +1 @@
+<input type="submit" value="Save" id="mainAction_saveDraft" \
name="action:mainAction!saveDraft"/>


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic