You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by lu...@apache.org on 2022/11/13 09:39:42 UTC

[struts] branch master updated: Refactors name/value calculation logic

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

lukaszlenart pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/struts.git


The following commit(s) were added to refs/heads/master by this push:
     new a77a80a80 Refactors name/value calculation logic
a77a80a80 is described below

commit a77a80a80380a7ba244a942e40361fd691c324bd
Author: Lukasz Lenart <lu...@apache.org>
AuthorDate: Sun Nov 13 10:39:37 2022 +0100

    Refactors name/value calculation logic
---
 .../java/org/apache/struts2/components/UIBean.java | 75 +++++++++++++---------
 .../site/resources/tags/checkbox-attributes.html   |  2 +-
 2 files changed, 45 insertions(+), 32 deletions(-)

diff --git a/core/src/main/java/org/apache/struts2/components/UIBean.java b/core/src/main/java/org/apache/struts2/components/UIBean.java
index c28e54c66..9dc4807f3 100644
--- a/core/src/main/java/org/apache/struts2/components/UIBean.java
+++ b/core/src/main/java/org/apache/struts2/components/UIBean.java
@@ -45,6 +45,7 @@ import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.function.Function;
 
 /**
  * <p>
@@ -439,6 +440,8 @@ public abstract class UIBean extends Component {
     private static final Logger LOG = LogManager.getLogger(UIBean.class);
 
     protected static final String ATTR_FIELD_VALUE = "fieldValue";
+    protected static final String ATTR_NAME_VALUE = "nameValue";
+    protected static final String ATTR_VALUE = "value";
 
     protected HttpServletRequest request;
     protected HttpServletResponse response;
@@ -797,37 +800,7 @@ public abstract class UIBean extends Component {
             addParameter("title", findString(title));
         }
 
-
-        // see if the value was specified as a parameter already
-        final String NAME_VALUE = "nameValue";
-        if (parameters.containsKey("value")) {
-            parameters.put(NAME_VALUE, parameters.get("value"));
-        } else {
-            if (evaluateNameValue()) {
-                final Class<?> valueClazz = getValueClassType();
-
-                if (valueClazz != null) {
-                    if (value != null) {
-                        addParameter(NAME_VALUE, findValue(value, valueClazz));
-                    } else if (translatedName != null) {
-                        boolean evaluated = !translatedName.equals(this.name);
-                        boolean reevaluate = !evaluated || isAcceptableExpression(translatedName);
-                        if (!reevaluate) {
-                            addParameter(NAME_VALUE, translatedName);
-                        } else {
-                            String expr = completeExpression(translatedName);
-                            addParameter(NAME_VALUE, findValue(expr, valueClazz));
-                        }
-                    }
-                } else {
-                    if (value != null) {
-                        addParameter(NAME_VALUE, findValue(value));
-                    } else if (translatedName != null) {
-                        addParameter(NAME_VALUE, findValue(translatedName));
-                    }
-                }
-            }
-        }
+        applyValueParameter(translatedName);
 
         final Form form = (Form) findAncestor(Form.class);
 
@@ -911,6 +884,46 @@ public abstract class UIBean extends Component {
         evaluateExtraParams();
     }
 
+    /**
+     * Tries to calculate the "value" parameter based either on the provided {@link #value} or {@link #name}
+     * @param translatedName the already evaluated {@link #name}
+     */
+    protected void applyValueParameter(String translatedName) {
+        // see if the value has been specified as a parameter already
+        if (parameters.containsKey(ATTR_VALUE)) {
+            parameters.put(ATTR_NAME_VALUE, parameters.get(ATTR_VALUE));
+        } else {
+            if (evaluateNameValue()) {
+                final Class<?> valueClazz = getValueClassType();
+
+                if (valueClazz != null) {
+                    if (value != null) {
+                        addParameter(ATTR_NAME_VALUE, findValue(value, valueClazz));
+                    } else if (translatedName != null) {
+                        processTranslatedName(translatedName, (expr) -> findValue(expr, valueClazz));
+                    }
+                } else {
+                    if (value != null) {
+                        addParameter(ATTR_NAME_VALUE, findValue(value));
+                    } else if (translatedName != null) {
+                        processTranslatedName(translatedName, this::findValue);
+                    }
+                }
+            }
+        }
+    }
+
+    private void processTranslatedName(String translatedName, Function<String, Object> evaluator) {
+        boolean evaluated = !translatedName.equals(this.name);
+        boolean reevaluate = !evaluated || isAcceptableExpression(translatedName);
+        if (!reevaluate) {
+            addParameter(ATTR_NAME_VALUE, translatedName);
+        } else {
+            String expr = completeExpression(translatedName);
+            addParameter(ATTR_NAME_VALUE, evaluator.apply(expr));
+        }
+    }
+
     protected String escape(String name) {
         // escape any possible values that can make the ID painful to work with in JavaScript
         if (name != null) {
diff --git a/core/src/site/resources/tags/checkbox-attributes.html b/core/src/site/resources/tags/checkbox-attributes.html
index ffc6ab1f8..56d14959f 100644
--- a/core/src/site/resources/tags/checkbox-attributes.html
+++ b/core/src/site/resources/tags/checkbox-attributes.html
@@ -283,7 +283,7 @@
         <td class="tag-attribute">false</td>
         <td class="tag-attribute">false</td>
         <td class="tag-attribute">Boolean</td>
-        <td class="tag-attribute">If set to true, unchecked elements will be submitted with the form. Since Struts 6.1.1 you can use a constant "struts.ui.checkbox.submitUnchecked" to set this property globally</td>
+        <td class="tag-attribute">If set to true, unchecked elements will be submitted with the form. Since Struts 6.1.1 you can use a constant "struts.ui.checkbox.submitUnchecked" to set this attribute globally</td>
     </tr>
     <tr>
         <td class="tag-attribute">tabindex</td>