Author: lukaszlenart Date: Thu May 23 06:31:48 2013 New Revision: 1485592 URL: http://svn.apache.org/r1485592 Log: WW-3623 Extends id generation logic to support case when tag doesn't have id, key nor name defined and some generics fix
Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/Component.java struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/UIBean.java struts/struts2/trunk/core/src/test/java/org/apache/struts2/components/UIBeanTest.java Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/Component.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/Component.java?rev=1485592&r1=1485591&r2=1485592&view=diff ============================================================================== --- struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/Component.java (original) +++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/Component.java Thu May 23 06:31:48 2013 @@ -53,7 +53,7 @@ public class Component { public static final String COMPONENT_STACK = "__component_stack"; protected ValueStack stack; - protected Map parameters; + protected Map<String, Object> 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<String, Object>(); 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 <tt>null</tt>. */ - public Stack getComponentStack() { - Stack componentStack = (Stack) stack.getContext().get(COMPONENT_STACK); + public Stack<Component> getComponentStack() { + Stack<Component> componentStack = (Stack<Component>) stack.getContext().get(COMPONENT_STACK); if (componentStack == null) { - componentStack = new Stack(); + componentStack = new Stack<Component>(); stack.getContext().put(COMPONENT_STACK, componentStack); } return componentStack; @@ -449,7 +449,7 @@ public class Component { * Gets the parameters. * @return the parameters. Is never <tt>null</tt>. */ - public Map getParameters() { + public Map<String, Object> 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<String, Object> 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<String, Object> params = getParameters(); if (value == null) { params.remove(key); Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/UIBean.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/UIBean.java?rev=1485592&r1=1485591&r2=1485592&view=diff ============================================================================== --- struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/UIBean.java (original) +++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/UIBean.java Thu May 23 06:31:48 2013 @@ -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 Com if ( name != null ) { // list should have been created by the form component - List tags = (List) form.getParameters().get("tagNames"); + List<String> tags = (List<String>) form.getParameters().get("tagNames"); tags.add(name); } } @@ -829,8 +828,8 @@ public abstract class UIBean extends Com 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 Com protected Map getTooltipConfig(UIBean component) { Object tooltipConfigObj = component.getParameters().get("tooltipConfig"); - Map tooltipConfig = new LinkedHashMap(); + Map<String, String> tooltipConfig = new LinkedHashMap<String, String>(); if (tooltipConfigObj instanceof Map) { // we get this if its configured using // 1] UI component's tooltipConfig attribute OR // 2] <param name="tooltip" value="" /> param tag value attribute - tooltipConfig = new LinkedHashMap((Map)tooltipConfigObj); + tooltipConfig = new LinkedHashMap<String, String>((Map)tooltipConfigObj); } else if (tooltipConfigObj instanceof String) { // we get this if its configured using @@ -945,17 +944,16 @@ public abstract class UIBean extends Com String tooltipConfigStr = (String) tooltipConfigObj; String[] tooltipConfigArray = tooltipConfigStr.split("\\|"); - for (int a=0; a<tooltipConfigArray.length; a++) { - String[] configEntry = ((String)tooltipConfigArray[a].trim()).split("="); + for (String aTooltipConfigArray : tooltipConfigArray) { + String[] configEntry = aTooltipConfigArray.trim().split("="); String key = configEntry[0].trim(); - String value = null; + String value; if (configEntry.length > 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 Com } /** - * 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 Com * <li>[this_component_name]</li> * </ol> * - * @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)); Modified: struts/struts2/trunk/core/src/test/java/org/apache/struts2/components/UIBeanTest.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/components/UIBeanTest.java?rev=1485592&r1=1485591&r2=1485592&view=diff ============================================================================== --- struts/struts2/trunk/core/src/test/java/org/apache/struts2/components/UIBeanTest.java (original) +++ struts/struts2/trunk/core/src/test/java/org/apache/struts2/components/UIBeanTest.java Thu May 23 06:31:48 2013 @@ -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 StrutsTe 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 StrutsTe 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);