Skip to content

Commit

Permalink
Fix FreeMarker form checbox macro generated names
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sslavic authored and rstoyanchev committed Mar 1, 2013
1 parent fbac428 commit ba03d5b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,8 @@
<@bind path />
<#assign id="${status.expression?replace('[','')?replace(']','')}">
<#assign isSelected = status.value?? && status.value?string=="true">
<input type="hidden" name="_${id}" value="on"/>
<input type="checkbox" id="${id}" name="${id}"<#if isSelected> checked="checked"</#if> ${attributes}/>
<input type="hidden" name="_${status.expression}" value="on"/>
<input type="checkbox" id="${id}" name="${status.expression}"<#if isSelected> checked="checked"</#if> ${attributes}/>
</#macro>

<#--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ protected void processTemplate(Template template, SimpleHash fmModel, HttpServle
fv.setApplicationContext(wac);
fv.setExposeSpringMacroHelpers(true);

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

Map model = new HashMap();
Map<String, Object> model = new HashMap<String, Object>();
model.put(FreeMarkerView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE, helperTool);

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

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

private String getMacroOutput(String name) throws Exception {

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

DummyMacroRequestContext rc = new DummyMacroRequestContext(request);
Map msgMap = new HashMap();
Map<String, String> msgMap = new HashMap<String, String>();
msgMap.put("hello", "Howdy");
msgMap.put("world", "Mundo");
rc.setMessageMap(msgMap);
Map themeMsgMap = new HashMap();
Map<String, String> themeMsgMap = new HashMap<String, String>();
themeMsgMap.put("hello", "Howdy!");
themeMsgMap.put("world", "Mundo!");
rc.setThemeMessageMap(themeMsgMap);
rc.setContextPath("/springtest");

TestBean tb = new TestBean("Darren", 99);
tb.setSpouse(new TestBean("Fred"));
tb.setJedi(true);
request.setAttribute("command", tb);
TestBean darren = new TestBean("Darren", 99);
TestBean fred = new TestBean("Fred");
fred.setJedi(true);
darren.setSpouse(fred);
darren.setJedi(true);
request.setAttribute("command", darren);

HashMap names = new HashMap();
Map<String, String> names = new HashMap<String, String>();
names.put("Darren", "Darren Davison");
names.put("John", "John Doe");
names.put("Fred", "Fred Bloggs");
names.put("Rob&Harrop", "Rob Harrop");

Configuration config = fc.getConfiguration();
Map model = new HashMap();
model.put("command", tb);
Map<String, Object> model = new HashMap<String, Object>();
model.put("command", darren);
model.put("springMacroRequestContext", rc);
model.put("msgArgs", new Object[] { "World" });
model.put("nameOptionMap", names);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,6 @@ FORM16

FORM17
<@spring.formInput "command.spouses[0].name", ""/>

FORM18
<@spring.formCheckbox "command.spouses[0].jedi" />

0 comments on commit ba03d5b

Please sign in to comment.