Skip to content

Commit ba03d5b

Browse files
sslavicrstoyanchev
authored andcommitted
Fix FreeMarker form checbox macro generated names
In Spring 3.2 a fix was implemented for all Spring Freemarker form macros so that generated form input fields have valid bracketless IDs. In the fix a regression was introduced manifesting in formCheckbox macro no longer generating appropriate names for checkbox and hidden input field. This change fixes mentioned regression issue. Issue: SPR-8732
1 parent fbac428 commit ba03d5b

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/view/freemarker/spring.ftl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,8 @@
309309
<@bind path />
310310
<#assign id="${status.expression?replace('[','')?replace(']','')}">
311311
<#assign isSelected = status.value?? && status.value?string=="true">
312-
<input type="hidden" name="_${id}" value="on"/>
313-
<input type="checkbox" id="${id}" name="${id}"<#if isSelected> checked="checked"</#if> ${attributes}/>
312+
<input type="hidden" name="_${status.expression}" value="on"/>
313+
<input type="checkbox" id="${id}" name="${status.expression}"<#if isSelected> checked="checked"</#if> ${attributes}/>
314314
</#macro>
315315

316316
<#--

spring-webmvc/src/test/java/org/springframework/web/servlet/view/freemarker/FreeMarkerMacroTests.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ protected void processTemplate(Template template, SimpleHash fmModel, HttpServle
107107
fv.setApplicationContext(wac);
108108
fv.setExposeSpringMacroHelpers(true);
109109

110-
Map model = new HashMap();
110+
Map<String, Object> model = new HashMap<String, Object>();
111111
model.put("tb", new TestBean("juergen", 99));
112112
fv.render(model, request, response);
113113
}
@@ -126,7 +126,7 @@ protected void processTemplate(Template template, SimpleHash model, HttpServletR
126126
fv.setApplicationContext(wac);
127127
fv.setExposeSpringMacroHelpers(true);
128128

129-
Map model = new HashMap();
129+
Map<String, Object> model = new HashMap<String, Object>();
130130
model.put(FreeMarkerView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE, helperTool);
131131

132132
try {
@@ -265,6 +265,13 @@ public void testForm17() throws Exception {
265265
assertEquals("<input type=\"text\" id=\"spouses0.name\" name=\"spouses[0].name\" value=\"Fred\" >", getMacroOutput("FORM17"));
266266
}
267267

268+
@Test
269+
public void testForm18() throws Exception {
270+
String output = getMacroOutput("FORM18");
271+
assertTrue("Wrong output: " + output, output.startsWith("<input type=\"hidden\" name=\"_spouses[0].jedi\" value=\"on\"/>"));
272+
assertTrue("Wrong output: " + output, output.contains("<input type=\"checkbox\" id=\"spouses0.jedi\" name=\"spouses[0].jedi\" checked=\"checked\" />"));
273+
}
274+
268275
private String getMacroOutput(String name) throws Exception {
269276

270277
String macro = fetchMacro(name);
@@ -274,30 +281,32 @@ private String getMacroOutput(String name) throws Exception {
274281
FileCopyUtils.copy("<#import \"spring.ftl\" as spring />\n" + macro, new FileWriter(resource.getPath()));
275282

276283
DummyMacroRequestContext rc = new DummyMacroRequestContext(request);
277-
Map msgMap = new HashMap();
284+
Map<String, String> msgMap = new HashMap<String, String>();
278285
msgMap.put("hello", "Howdy");
279286
msgMap.put("world", "Mundo");
280287
rc.setMessageMap(msgMap);
281-
Map themeMsgMap = new HashMap();
288+
Map<String, String> themeMsgMap = new HashMap<String, String>();
282289
themeMsgMap.put("hello", "Howdy!");
283290
themeMsgMap.put("world", "Mundo!");
284291
rc.setThemeMessageMap(themeMsgMap);
285292
rc.setContextPath("/springtest");
286293

287-
TestBean tb = new TestBean("Darren", 99);
288-
tb.setSpouse(new TestBean("Fred"));
289-
tb.setJedi(true);
290-
request.setAttribute("command", tb);
294+
TestBean darren = new TestBean("Darren", 99);
295+
TestBean fred = new TestBean("Fred");
296+
fred.setJedi(true);
297+
darren.setSpouse(fred);
298+
darren.setJedi(true);
299+
request.setAttribute("command", darren);
291300

292-
HashMap names = new HashMap();
301+
Map<String, String> names = new HashMap<String, String>();
293302
names.put("Darren", "Darren Davison");
294303
names.put("John", "John Doe");
295304
names.put("Fred", "Fred Bloggs");
296305
names.put("Rob&Harrop", "Rob Harrop");
297306

298307
Configuration config = fc.getConfiguration();
299-
Map model = new HashMap();
300-
model.put("command", tb);
308+
Map<String, Object> model = new HashMap<String, Object>();
309+
model.put("command", darren);
301310
model.put("springMacroRequestContext", rc);
302311
model.put("msgArgs", new Object[] { "World" });
303312
model.put("nameOptionMap", names);

spring-webmvc/src/test/java/org/springframework/web/servlet/view/freemarker/test.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,6 @@ FORM16
8989

9090
FORM17
9191
<@spring.formInput "command.spouses[0].name", ""/>
92+
93+
FORM18
94+
<@spring.formCheckbox "command.spouses[0].jedi" />

0 commit comments

Comments
 (0)