From 26728639eccea4542c4effa0a71528ef5dee2ff2 Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Thu, 23 May 2013 06:31:48 +0000 Subject: [PATCH] WW-3623 Extends id generation logic to support case when tag doesn't have id, key nor name defined and some generics fix git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@1485592 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/struts2/components/Component.java | 16 ++++---- .../org/apache/struts2/components/UIBean.java | 37 ++++++++++--------- .../apache/struts2/components/UIBeanTest.java | 26 ++++++++++--- 3 files changed, 48 insertions(+), 31 deletions(-) diff --git a/core/src/main/java/org/apache/struts2/components/Component.java b/core/src/main/java/org/apache/struts2/components/Component.java index 5fbfa908d..f7c114b73 100644 --- a/core/src/main/java/org/apache/struts2/components/Component.java +++ b/core/src/main/java/org/apache/struts2/components/Component.java @@ -53,7 +53,7 @@ public class Component { public static final String COMPONENT_STACK = "__component_stack"; protected ValueStack stack; - protected Map parameters; + protected Map parameters; protected ActionMapper actionMapper; protected boolean throwExceptionOnELFailure; private UrlHelper urlHelper; @@ -65,7 +65,7 @@ public class Component { */ public Component(ValueStack stack) { this.stack = stack; - this.parameters = new LinkedHashMap(); + this.parameters = new LinkedHashMap(); getComponentStack().push(this); } @@ -107,10 +107,10 @@ public class Component { * Gets the component stack of this component. * @return the component stack of this component, never null. */ - public Stack getComponentStack() { - Stack componentStack = (Stack) stack.getContext().get(COMPONENT_STACK); + public Stack getComponentStack() { + Stack componentStack = (Stack) stack.getContext().get(COMPONENT_STACK); if (componentStack == null) { - componentStack = new Stack(); + componentStack = new Stack(); stack.getContext().put(COMPONENT_STACK, componentStack); } return componentStack; @@ -449,7 +449,7 @@ public class Component { * Gets the parameters. * @return the parameters. Is never null. */ - public Map getParameters() { + public Map getParameters() { return parameters; } @@ -457,7 +457,7 @@ public class Component { * Adds all the given parameters to this component's own parameters. * @param params the parameters to add. */ - public void addAllParameters(Map params) { + public void addAllParameters(Map params) { parameters.putAll(params); } @@ -472,7 +472,7 @@ public class Component { */ public void addParameter(String key, Object value) { if (key != null) { - Map params = getParameters(); + Map params = getParameters(); if (value == null) { params.remove(key); 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 100a54ed8..6d9b826e9 100644 --- a/core/src/main/java/org/apache/struts2/components/UIBean.java +++ b/core/src/main/java/org/apache/struts2/components/UIBean.java @@ -43,7 +43,6 @@ import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.HashMap; import java.util.HashSet; -import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -806,7 +805,7 @@ public abstract class UIBean extends Component { if ( name != null ) { // list should have been created by the form component - List tags = (List) form.getParameters().get("tagNames"); + List tags = (List) form.getParameters().get("tagNames"); tags.add(name); } } @@ -829,8 +828,8 @@ public abstract class UIBean extends Component { Map overallTooltipConfigMap = getTooltipConfig(form); overallTooltipConfigMap.putAll(tooltipConfigMap); // override parent form's tooltip config - for (Iterator i = overallTooltipConfigMap.entrySet().iterator(); i.hasNext(); ) { - Map.Entry entry = (Map.Entry) i.next(); + for (Object o : overallTooltipConfigMap.entrySet()) { + Map.Entry entry = (Map.Entry) o; addParameter((String) entry.getKey(), entry.getValue()); } } @@ -930,14 +929,14 @@ public abstract class UIBean extends Component { protected Map getTooltipConfig(UIBean component) { Object tooltipConfigObj = component.getParameters().get("tooltipConfig"); - Map tooltipConfig = new LinkedHashMap(); + Map tooltipConfig = new LinkedHashMap(); if (tooltipConfigObj instanceof Map) { // we get this if its configured using // 1] UI component's tooltipConfig attribute OR // 2] param tag value attribute - tooltipConfig = new LinkedHashMap((Map)tooltipConfigObj); + tooltipConfig = new LinkedHashMap((Map)tooltipConfigObj); } else if (tooltipConfigObj instanceof String) { // we get this if its configured using @@ -945,17 +944,16 @@ public abstract class UIBean extends Component { String tooltipConfigStr = (String) tooltipConfigObj; String[] tooltipConfigArray = tooltipConfigStr.split("\\|"); - for (int a=0; a 1) { value = configEntry[1].trim(); tooltipConfig.put(key, value); - } - else { + } else { if (LOG.isWarnEnabled()) { - LOG.warn("component "+component+" tooltip config param "+key+" has no value defined, skipped"); + LOG.warn("component " + component + " tooltip config param " + key + " has no value defined, skipped"); } } } @@ -970,7 +968,7 @@ public abstract class UIBean extends Component { } /** - * Create HTML id element for the component and populate this component parmaeter + * Create HTML id element for the component and populate this component parameter * map. Additionally, a parameter named escapedId is populated which contains the found id value filtered by * {@link #escape(String)}, needed eg. for naming Javascript identifiers based on the id value. * @@ -981,18 +979,23 @@ public abstract class UIBean extends Component { *
  • [this_component_name]
  • * * - * @param form + * @param form enclosing form tag */ protected void populateComponentHtmlId(Form form) { String tryId; + String generatedId; if (id != null) { // this check is needed for backwards compatibility with 2.1.x tryId = findStringIfAltSyntax(id); + } else if (null == (generatedId = escape(name != null ? findString(name) : null))) { + if (LOG.isDebugEnabled()) { + LOG.debug("Cannot determine id attribute for [#0], consider defining id, name or key attribute!", this); + } + tryId = null; } else if (form != null) { - tryId = form.getParameters().get("id") + "_" - + escape(name != null ? findString(name) : null); + tryId = form.getParameters().get("id") + "_" + generatedId; } else { - tryId = escape(name != null ? findString(name) : null); + tryId = generatedId; } addParameter("id", tryId); addParameter("escapedId", escape(tryId)); diff --git a/core/src/test/java/org/apache/struts2/components/UIBeanTest.java b/core/src/test/java/org/apache/struts2/components/UIBeanTest.java index 0f00690f5..9a4ae17f7 100644 --- a/core/src/test/java/org/apache/struts2/components/UIBeanTest.java +++ b/core/src/test/java/org/apache/struts2/components/UIBeanTest.java @@ -21,15 +21,14 @@ package org.apache.struts2.components; -import java.util.Collections; -import java.util.Map; - +import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.util.ValueStack; import org.apache.struts2.StrutsTestCase; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; -import com.opensymphony.xwork2.ActionContext; -import com.opensymphony.xwork2.util.ValueStack; +import java.util.Collections; +import java.util.Map; /** * @@ -85,6 +84,21 @@ public class UIBeanTest extends StrutsTestCase { assertEquals("formId_txtFldName", txtFld.getParameters().get("id")); } + public void testPopulateComponentHtmlWithoutNameAndId() throws Exception { + ValueStack stack = ActionContext.getContext().getValueStack(); + MockHttpServletRequest req = new MockHttpServletRequest(); + MockHttpServletResponse res = new MockHttpServletResponse(); + + Form form = new Form(stack, req, res); + form.getParameters().put("id", "formId"); + + TextField txtFld = new TextField(stack, req, res); + + txtFld.populateComponentHtmlId(form); + + assertEquals(null, txtFld.getParameters().get("id")); + } + public void testEscape() throws Exception { ValueStack stack = ActionContext.getContext().getValueStack(); MockHttpServletRequest req = new MockHttpServletRequest(); @@ -143,7 +157,7 @@ public class UIBeanTest extends StrutsTestCase { ValueStack stack = ActionContext.getContext().getValueStack(); MockHttpServletRequest req = new MockHttpServletRequest(); MockHttpServletResponse res = new MockHttpServletResponse(); - Map context = Collections.singletonMap("theme", new Integer(12)); + Map context = Collections.singletonMap("theme", 12); ActionContext.getContext().put("attr", context); TextField txtFld = new TextField(stack, req, res);