diff --git a/core/src/main/java/org/apache/struts2/components/Label.java b/core/src/main/java/org/apache/struts2/components/Label.java index ba991688d..4498a7fd5 100644 --- a/core/src/main/java/org/apache/struts2/components/Label.java +++ b/core/src/main/java/org/apache/struts2/components/Label.java @@ -78,15 +78,17 @@ public class Label extends UIBean { addParameter("for", findString(forAttr)); } - // try value first, then name (this overrides the default behavior in the superclass) + // try value, then key, then name (this overrides the default behavior in the superclass) if (value != null) { addParameter("nameValue", findString(value)); + } else if (key != null) { + String expr = "%{getText('"+ key +"')}"; + addParameter("nameValue", findString(expr)); } else if (name != null) { String expr = name; if (altSyntax()) { expr = "%{" + expr + "}"; } - addParameter("nameValue", findString(expr)); } } diff --git a/core/src/test/java/org/apache/struts2/TestAction.java b/core/src/test/java/org/apache/struts2/TestAction.java index b54e1417b..dfc3f3357 100644 --- a/core/src/test/java/org/apache/struts2/TestAction.java +++ b/core/src/test/java/org/apache/struts2/TestAction.java @@ -20,10 +20,7 @@ */ package org.apache.struts2; -import java.util.Arrays; -import java.util.Collection; -import java.util.List; -import java.util.Map; +import java.util.*; import org.apache.struts2.views.jsp.ui.User; @@ -50,6 +47,19 @@ public class TestAction extends ActionSupport { private List list3; private SomeEnum status = SomeEnum.COMPLETED; + private final Map texts = new HashMap(); + + public void setText(String key, String value) { + this.texts.put(key, value); + } + + public String getText(String key) { + if (this.texts.containsKey(key)) { + return (String) this.texts.get(key); + } + return super.getText(key); + } + public Collection getCollection() { return collection; } diff --git a/core/src/test/java/org/apache/struts2/views/jsp/ui/LabelTest.java b/core/src/test/java/org/apache/struts2/views/jsp/ui/LabelTest.java index d60c129d5..96e0f47f7 100644 --- a/core/src/test/java/org/apache/struts2/views/jsp/ui/LabelTest.java +++ b/core/src/test/java/org/apache/struts2/views/jsp/ui/LabelTest.java @@ -108,4 +108,26 @@ public class LabelTest extends AbstractUITagTest { LabelTag tag = new LabelTag(); verifyGenericProperties(tag, "xhtml", null); } + + public void testWithKey() throws Exception { + TestAction testAction = (TestAction) action; + final String key = "labelKey"; + final String value = "baz"; + testAction.setText(key, value); + + testAction.setFoo("notToBeOutput"); + + LabelTag tag = new LabelTag(); + tag.setPageContext(pageContext); + tag.setLabel("mylabel"); + tag.setFor("for"); + tag.setName("foo"); + tag.setKey(key); + + tag.doStartTag(); + tag.doEndTag(); + + verify(LabelTest.class.getResource("Label-2.txt")); + } + }