diff --git a/core/src/main/resources/template/simple/doubleselect.ftl b/core/src/main/resources/template/simple/doubleselect.ftl index 67aafeec1..039e1420a 100644 --- a/core/src/main/resources/template/simple/doubleselect.ftl +++ b/core/src/main/resources/template/simple/doubleselect.ftl @@ -18,6 +18,7 @@ * under the License. */ --> +<#global dynamic_attributes_ignore = "second-"/> <#include "/${parameters.templateDir}/simple/select.ftl" /> <#assign startCount = 0/> <#if parameters.headerKey?? && parameters.headerValue??> @@ -60,6 +61,8 @@ <#if parameters.doubleDisabled!false> disabled="disabled"<#rt/> +<#include "/${parameters.templateDir}/${parameters.expandTheme}/prefixed-dynamic-attributes.ftl" /> +<@prefixedDynamicAttributes prefix="second-"/> > <#if parameters.doubleMultiple!false> @@ -190,4 +193,4 @@ ${parameters.id}Temp.options[0].selected = true; } } - \ No newline at end of file + diff --git a/core/src/main/resources/template/simple/dynamic-attributes.ftl b/core/src/main/resources/template/simple/dynamic-attributes.ftl index 78231597a..47a91139f 100644 --- a/core/src/main/resources/template/simple/dynamic-attributes.ftl +++ b/core/src/main/resources/template/simple/dynamic-attributes.ftl @@ -18,15 +18,22 @@ * under the License. */ --> +<#function acceptKey(key)> + <#if dynamic_attributes_ignore??> + <#return !key?starts_with(dynamic_attributes_ignore) > + <#else> + <#return true> + + <#if (parameters.dynamicAttributes?? && parameters.dynamicAttributes?size > 0)><#rt/> <#assign aKeys = parameters.dynamicAttributes.keySet()><#rt/> -<#list aKeys as aKey><#rt/> - <#assign keyValue = parameters.dynamicAttributes.get(aKey)/> - <#if keyValue?is_string> - <#assign value = struts.translateVariables(keyValue)!keyValue/> - <#else> - <#assign value = keyValue?string/> - +<#list aKeys?filter(acceptKey) as aKey><#rt/> +<#assign keyValue = parameters.dynamicAttributes.get(aKey)/> +<#if keyValue?is_string> + <#assign value = struts.translateVariables(keyValue)!keyValue/> +<#else> + <#assign value = keyValue?string/> + ${aKey}="${value}"<#rt/> <#rt/> <#rt/> diff --git a/core/src/main/resources/template/simple/inputtransferselect.ftl b/core/src/main/resources/template/simple/inputtransferselect.ftl index a9258dd6a..7684a097d 100644 --- a/core/src/main/resources/template/simple/inputtransferselect.ftl +++ b/core/src/main/resources/template/simple/inputtransferselect.ftl @@ -29,7 +29,6 @@
<#t/> - name="${parameters.name!""}_input"<#rt/> <#if parameters.disabled!false> @@ -55,6 +54,8 @@ <#include "/${parameters.templateDir}/${parameters.expandTheme}/scripting-events.ftl" /> <#include "/${parameters.templateDir}/${parameters.expandTheme}/common-attributes.ftl" /> +<#include "/${parameters.templateDir}/${parameters.expandTheme}/prefixed-dynamic-attributes.ftl" /> +<@prefixedDynamicAttributes prefix="input-"/> /> @@ -94,9 +95,10 @@ <#if parameters.rightTitle?has_content><#t/>
<#t/> +<#global dynamic_attributes_ignore = "input-"/> <#include "/${parameters.templateDir}/simple/select.ftl" /> <#if parameters.allowUpDown!true> - onclick="moveOptionDown(document.getElementById('${parameters.id}'), 'key', '${parameters.headerKey}');" <#else> @@ -106,7 +108,7 @@ value="${parameters.downLabel}" /> - onclick="moveOptionUp(document.getElementById('${parameters.id}'), 'key', '${parameters.headerKey}');" <#else> diff --git a/core/src/main/resources/template/simple/optiontransferselect.ftl b/core/src/main/resources/template/simple/optiontransferselect.ftl index e0d00eef0..36d4ca302 100644 --- a/core/src/main/resources/template/simple/optiontransferselect.ftl +++ b/core/src/main/resources/template/simple/optiontransferselect.ftl @@ -28,6 +28,7 @@ <#if parameters.leftTitle??><#t/>
<#t/> +<#global dynamic_attributes_ignore = "right-"/> <#include "/${parameters.templateDir}/simple/select.ftl" /> <#if parameters.allowUpDownOnLeft!true> <#t/> accesskey="<#outputformat 'JavaScript'>${parameters.doubleAccesskey}" + <#include "/${parameters.templateDir}/${parameters.expandTheme}/prefixed-dynamic-attributes.ftl" /> + <@prefixedDynamicAttributes prefix="right-"/> > <#if parameters.doubleHeaderKey?? && parameters.doubleHeaderValue??><#t/> diff --git a/core/src/main/resources/template/simple/prefixed-dynamic-attributes.ftl b/core/src/main/resources/template/simple/prefixed-dynamic-attributes.ftl new file mode 100644 index 000000000..3b6c71b21 --- /dev/null +++ b/core/src/main/resources/template/simple/prefixed-dynamic-attributes.ftl @@ -0,0 +1,36 @@ +<#-- +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +--> +<#macro prefixedDynamicAttributes prefix> +<#if (parameters.dynamicAttributes?? && parameters.dynamicAttributes?size > 0)><#rt/> +<#assign aKeys = parameters.dynamicAttributes.keySet()><#rt/> +<#list aKeys as aKey><#rt/> +<#if aKey?starts_with(prefix)> +<#assign keyValue = parameters.dynamicAttributes.get(aKey)/> +<#if keyValue?is_string> + <#assign value = struts.translateVariables(keyValue)!keyValue/> +<#else> + <#assign value = keyValue?string/> + + ${aKey?keep_after(prefix)}="${value}"<#rt/> + +<#rt/> +<#rt/> + diff --git a/core/src/main/resources/template/simple/select.ftl b/core/src/main/resources/template/simple/select.ftl index e096cb789..7e90c8045 100644 --- a/core/src/main/resources/template/simple/select.ftl +++ b/core/src/main/resources/template/simple/select.ftl @@ -68,7 +68,7 @@ <#assign itemKeyStr = stack.findString('top')> <#if parameters.listValueKey??> - <#-- checks the valueStack for the 'valueKey.' The valueKey is then looked-up in the locale file for it's + <#-- checks the valueStack for the 'valueKey.' The valueKey is then looked-up in the locale file for it's localized value. This is then used as a label --> <#assign valueKey = stack.findString(parameters.listValueKey)!'' /> <#if valueKey?has_content> @@ -139,7 +139,7 @@ <#if ( !parameters.id?? && !parameters.name??)> - + <#if parameters.disabled!false> disabled="disabled"<#rt/> diff --git a/core/src/test/java/org/apache/struts2/views/freemarker/FreemarkerResultMockedTest.java b/core/src/test/java/org/apache/struts2/views/freemarker/FreemarkerResultMockedTest.java index a78bcf1a9..99cd4842a 100644 --- a/core/src/test/java/org/apache/struts2/views/freemarker/FreemarkerResultMockedTest.java +++ b/core/src/test/java/org/apache/struts2/views/freemarker/FreemarkerResultMockedTest.java @@ -113,12 +113,7 @@ public class FreemarkerResultMockedTest extends StrutsInternalTestCase { ActionMapping mapping = container.getInstance(ActionMapper.class).getMapping(request, configurationManager); dispatcher.serviceAction(request, response, mapping); - String expectedJDK17 = - "" - + "" - + "" - + ""; - String expectedJDK18 = + String expected = "" + "" + "" @@ -126,11 +121,7 @@ public class FreemarkerResultMockedTest extends StrutsInternalTestCase { String result = stringWriter.toString(); - if (result.contains("id=\"test\" foo=\"bar\"")) { - assertEquals(expectedJDK18, result); - } else { - assertEquals(expectedJDK17, result); - } + assertEquals(expected, result); } public void testManualListInTemplate() throws Exception { diff --git a/core/src/test/java/org/apache/struts2/views/jsp/ui/DoubleSelectTest.java b/core/src/test/java/org/apache/struts2/views/jsp/ui/DoubleSelectTest.java index 536a5d996..15e0a17cd 100644 --- a/core/src/test/java/org/apache/struts2/views/jsp/ui/DoubleSelectTest.java +++ b/core/src/test/java/org/apache/struts2/views/jsp/ui/DoubleSelectTest.java @@ -287,6 +287,15 @@ public class DoubleSelectTest extends AbstractUITagTest { verifyGenericProperties(tag, "simple", new String[]{"value"}); } + public void testGenericSimpleWithDynamicAttributes() throws Exception { + DoubleSelectTag tag = new DoubleSelectTag(); + tag.setDynamicAttribute(null, "select-name", "firstName"); + tag.setDynamicAttribute(null, "second-select-name", "secondName"); + prepareTagGeneric(tag); + verifyGenericProperties(tag, "simple", new String[]{"value"}); + verify(SelectTag.class.getResource("DoubleSelect-5.txt")); + } + public void testGenericXhtml() throws Exception { DoubleSelectTag tag = new DoubleSelectTag(); prepareTagGeneric(tag); @@ -379,4 +388,4 @@ public class DoubleSelectTest extends AbstractUITagTest { return key; } } -} \ No newline at end of file +} diff --git a/core/src/test/java/org/apache/struts2/views/jsp/ui/InputTransferSelectTagTest.java b/core/src/test/java/org/apache/struts2/views/jsp/ui/InputTransferSelectTagTest.java index ee60cd80e..8a6017239 100644 --- a/core/src/test/java/org/apache/struts2/views/jsp/ui/InputTransferSelectTagTest.java +++ b/core/src/test/java/org/apache/struts2/views/jsp/ui/InputTransferSelectTagTest.java @@ -18,19 +18,16 @@ */ package org.apache.struts2.views.jsp.ui; -import org.apache.struts2.views.jsp.AbstractUITagTest; import org.apache.struts2.TestAction; +import org.apache.struts2.views.jsp.AbstractUITagTest; -import java.util.List; import java.util.ArrayList; +import java.util.List; -/** - * - */ public class InputTransferSelectTagTest extends AbstractUITagTest { public void testWithRequired() throws Exception { - List list = new ArrayList(); + List list = new ArrayList<>(); list.add("Item One"); list.add("Item Two"); @@ -47,7 +44,28 @@ public class InputTransferSelectTagTest extends AbstractUITagTest { tag.doStartTag(); tag.doEndTag(); - //System.out.println(writer.toString()); verify(InputTransferSelectTagTest.class.getResource("inputtransferselect-1.txt")); } + + public void testDynamicAttributes() throws Exception { + List list = new ArrayList<>(); + list.add("Item One"); + list.add("Item Two"); + + TestAction testaction = (TestAction) action; + testaction.setCollection(list); + + InputTransferSelectTag tag = new InputTransferSelectTag(); + tag.setPageContext(pageContext); + tag.setDynamicAttribute(null, "input-collection-name", "inputName"); + tag.setDynamicAttribute(null, "collection-name", "collectionName"); + + tag.setName("collection"); + tag.setList("collection"); + + tag.doStartTag(); + tag.doEndTag(); + + verify(InputTransferSelectTagTest.class.getResource("inputtransferselect-2.txt")); + } } diff --git a/core/src/test/java/org/apache/struts2/views/jsp/ui/OptionTransferSelectTagTest.java b/core/src/test/java/org/apache/struts2/views/jsp/ui/OptionTransferSelectTagTest.java index d7e8bb1aa..9742effe1 100644 --- a/core/src/test/java/org/apache/struts2/views/jsp/ui/OptionTransferSelectTagTest.java +++ b/core/src/test/java/org/apache/struts2/views/jsp/ui/OptionTransferSelectTagTest.java @@ -525,4 +525,36 @@ public class OptionTransferSelectTagTest extends AbstractUITagTest { //System.out.println(writer.toString()); verify(OptionTransferSelectTagTest.class.getResource("optiontransferselect-7.txt")); } + + public void testDynamicAttributes() throws Exception { + List left = new ArrayList(); + left.add("Left1"); + left.add("Left2"); + + List right = new ArrayList(); + right.add("Right1"); + right.add("Right2"); + + TestAction testaction = (TestAction) action; + testaction.setCollection(left); + testaction.setList2(right); + + OptionTransferSelectTag tag = new OptionTransferSelectTag(); + tag.setPageContext(pageContext); + + tag.setName("collection"); + tag.setList("collection"); + + tag.setDoubleName("list2"); + tag.setDoubleList("list2"); + + tag.setDynamicAttribute(null, "collection", "leftName"); + tag.setDynamicAttribute(null, "right-collection", "rightName"); + + tag.doStartTag(); + tag.doEndTag(); + + //System.out.println(writer.toString()); + verify(OptionTransferSelectTagTest.class.getResource("optiontransferselect-8.txt")); + } } diff --git a/core/src/test/resources/org/apache/struts2/views/jsp/ui/DoubleSelect-5.txt b/core/src/test/resources/org/apache/struts2/views/jsp/ui/DoubleSelect-5.txt new file mode 100644 index 000000000..247293619 --- /dev/null +++ b/core/src/test/resources/org/apache/struts2/views/jsp/ui/DoubleSelect-5.txt @@ -0,0 +1,39 @@ + +
+ + diff --git a/core/src/test/resources/org/apache/struts2/views/jsp/ui/inputtransferselect-2.txt b/core/src/test/resources/org/apache/struts2/views/jsp/ui/inputtransferselect-2.txt new file mode 100644 index 000000000..9c11a87b4 --- /dev/null +++ b/core/src/test/resources/org/apache/struts2/views/jsp/ui/inputtransferselect-2.txt @@ -0,0 +1,30 @@ + + + + + + + + + + +
+ +

+ +

+ +
+
+
+ + + + +
+ + diff --git a/core/src/test/resources/org/apache/struts2/views/jsp/ui/optiontransferselect-8.txt b/core/src/test/resources/org/apache/struts2/views/jsp/ui/optiontransferselect-8.txt new file mode 100644 index 000000000..4f6274914 --- /dev/null +++ b/core/src/test/resources/org/apache/struts2/views/jsp/ui/optiontransferselect-8.txt @@ -0,0 +1,50 @@ + + + + + + + + + + +
+ + + + + + +
+
+ +
+
+ +
+
+ +
+
+ +
+
+
+ + + + +
+ +