From a9f42061510e5015965a6a680d866db519b5356d Mon Sep 17 00:00:00 2001 From: Stevo Slavic Date: Sun, 4 Mar 2012 22:02:38 +0100 Subject: [PATCH] Generate bracketless tag id in FreeMarker forms Before this change if FreeMarker Spring form macro was bound to a path which contains square brackets, those brackets would also appear in id of generated tag, making the id invalid. As of this fix all FreeMarker Spring form macros generate tag with id that does not contain square brackets. Issue: SPR-8732 --- .../web/servlet/view/freemarker/spring.ftl | 14 +++++++------- .../view/freemarker/FreeMarkerMacroTests.java | 5 +++++ .../web/servlet/view/freemarker/test.ftl | 3 +++ 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/freemarker/spring.ftl b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/freemarker/spring.ftl index 502f99f2e61e..43b29af322eb 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/freemarker/spring.ftl +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/freemarker/spring.ftl @@ -157,7 +157,7 @@ --> <#macro formInput path attributes="" fieldType="text"> <@bind path/> - ${stringStatusValue}" ${attributes}<@closeTag/> + ${stringStatusValue}" ${attributes}<@closeTag/> <#-- @@ -202,7 +202,7 @@ --> <#macro formTextarea path attributes=""> <@bind path/> - + <#-- @@ -218,7 +218,7 @@ --> <#macro formSingleSelect path options attributes=""> <@bind path/> - <#if options?is_hash> <#list options?keys as value> @@ -244,7 +244,7 @@ --> <#macro formMultiSelect path options attributes=""> <@bind path/> - <#list options?keys as value> <#assign isSelected = contains(status.actualValue?default([""]), value)> @@ -267,7 +267,7 @@ <#macro formRadioButtons path options separator attributes=""> <@bind path/> <#list options?keys as value> - <#assign id="${status.expression}${value_index}"> + <#assign id="${status.expression?replace('[','')?replace(']','')}${value_index}"> checked="checked" ${attributes}<@closeTag/> ${separator} @@ -288,7 +288,7 @@ <#macro formCheckboxes path options separator attributes=""> <@bind path/> <#list options?keys as value> - <#assign id="${status.expression}${value_index}"> + <#assign id="${status.expression?replace('[','')?replace(']','')}${value_index}"> <#assign isSelected = contains(status.actualValue?default([""]), value)> checked="checked" ${attributes}<@closeTag/> ${separator} @@ -307,7 +307,7 @@ --> <#macro formCheckbox path attributes=""> <@bind path /> - <#assign id="${status.expression}"> + <#assign id="${status.expression?replace('[','')?replace(']','')}"> <#assign isSelected = status.value?? && status.value?string=="true"> checked="checked" ${attributes}/> diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/view/freemarker/FreeMarkerMacroTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/view/freemarker/FreeMarkerMacroTests.java index 84a7854a1aed..f85a4e011aa0 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/view/freemarker/FreeMarkerMacroTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/view/freemarker/FreeMarkerMacroTests.java @@ -260,6 +260,11 @@ public void testForm16() throws Exception { assertTrue("Wrong output: " + output, output.contains("")); } + @Test + public void testForm17() throws Exception { + assertEquals("", getMacroOutput("FORM17")); + } + private String getMacroOutput(String name) throws Exception { String macro = fetchMacro(name); diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/view/freemarker/test.ftl b/spring-webmvc/src/test/java/org/springframework/web/servlet/view/freemarker/test.ftl index 415506877505..8440b4f945f4 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/view/freemarker/test.ftl +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/view/freemarker/test.ftl @@ -86,3 +86,6 @@ FORM15 FORM16 <@spring.formCheckbox "command.jedi"/> + +FORM17 +<@spring.formInput "command.spouses[0].name", ""/>