Browse Source

SPR-4700 - Add single checkbox input macro for Velocity and Freemarker

conversation
Arjen Poutsma 16 years ago
parent
commit
93e99556c0
  1. 17
      org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/freemarker/spring.ftl
  2. 15
      org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/velocity/spring.vm
  3. 5
      org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/freemarker/FreeMarkerMacroTests.java
  4. 6
      org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/freemarker/test.ftl
  5. 5
      org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/velocity/VelocityMacroTests.java
  6. 7
      org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/velocity/test.vm

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

@ -296,6 +296,23 @@ @@ -296,6 +296,23 @@
<input type="hidden" name="_${status.expression}" value="on"/>
</#macro>
<#--
* formCheckbox
*
* Show a single checkbox.
*
* @param path the name of the field to bind to
* @param attributes any additional attributes for the element (such as class
* or CSS styles or size
-->
<#macro formCheckbox path attributes="">
<@bind path />
<#local checked><#if status.value?? && status.value?string=="true">true<#else>false</#if></#local>
<#local id><#if status.expression??>${status.expression}<#else>${path}</#if></#local>
<input type="hidden" name="_${id}" value="on"/>
<input type="checkbox" id="${id}" name="${id}" checked="${checked}" ${attributes}/>
</#macro>
<#--
* showErrors
*

15
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/velocity/spring.vm

@ -269,6 +269,21 @@ @@ -269,6 +269,21 @@
<input type="hidden" name="_${status.expression}" value="on"/>
#end
#**
* springFormCheckbox
*
* Show a single checkbox.
*
* @param path the name of the field to bind to
* @param attributes any additional attributes for the element (such as class
* or CSS styles or size
*#
#macro( springFormCheckbox $path $attributes )
#springBind($path)
<input type="hidden" name="_${status.expression}" value="on"/>
<input type="checkbox" id="${status.expression}" name="${status.expression}" checked="#if("$!{status.value}"=="true")true#{else}false#end" ${attributes}/>
#end
#**
* springShowErrors
*

5
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/freemarker/FreeMarkerMacroTests.java

@ -140,6 +140,7 @@ public class FreeMarkerMacroTests extends TestCase { @@ -140,6 +140,7 @@ public class FreeMarkerMacroTests extends TestCase {
TestBean tb = new TestBean("Darren", 99);
tb.setSpouse(new TestBean("Fred"));
tb.setJedi(true);
request.setAttribute("command", tb);
HashMap names = new HashMap();
@ -192,6 +193,10 @@ public class FreeMarkerMacroTests extends TestCase { @@ -192,6 +193,10 @@ public class FreeMarkerMacroTests extends TestCase {
if (tokens[i].equals("FORM11")) assertEquals("<input type=\"text\" id=\"name\" name=\"name\" value=\"Darren\" >", tokens[i + 1]);
if (tokens[i].equals("FORM12")) assertEquals("<input type=\"hidden\" id=\"name\" name=\"name\" value=\"Darren\" >", tokens[i + 1]);
if (tokens[i].equals("FORM13")) assertEquals("<input type=\"password\" id=\"name\" name=\"name\" value=\"\" >", tokens[i + 1]);
if (tokens[i].equals("FORM15")) assertEquals("<input type=\"hidden\" name=\"_name\" value=\"on\"/>", tokens[i + 1]);
if (tokens[i].equals("FORM15")) assertEquals("<input type=\"checkbox\" id=\"name\" name=\"name\" checked=\"false\" />", tokens[i + 2]);
if (tokens[i].equals("FORM16")) assertEquals("<input type=\"hidden\" name=\"_jedi\" value=\"on\"/>", tokens[i + 1]);
if (tokens[i].equals("FORM16")) assertEquals("<input type=\"checkbox\" id=\"jedi\" name=\"jedi\" checked=\"true\" />", tokens[i + 2]);
}
}

6
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/freemarker/test.ftl

@ -77,3 +77,9 @@ FORM13 @@ -77,3 +77,9 @@ FORM13
FORM14
<@spring.formSingleSelect "command.name", options, ""/>
FORM15
<@spring.formCheckbox "command.name"/>
FORM16
<@spring.formCheckbox "command.jedi"/>

5
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/velocity/VelocityMacroTests.java

@ -133,6 +133,7 @@ public class VelocityMacroTests extends TestCase { @@ -133,6 +133,7 @@ public class VelocityMacroTests extends TestCase {
rc.setContextPath("/springtest");
TestBean tb = new TestBean("Darren", 99);
tb.setJedi(true);
request.setAttribute("command", tb);
HashMap names = new HashMap();
@ -178,6 +179,10 @@ public class VelocityMacroTests extends TestCase { @@ -178,6 +179,10 @@ public class VelocityMacroTests extends TestCase {
//TODO verify remaining output (fix whitespace)
if (tokens[i].equals("FORM9")) assertEquals("<input type=\"password\" id=\"name\" name=\"name\" value=\"\" >", tokens[i + 1]);
if (tokens[i].equals("FORM10")) assertEquals("<input type=\"hidden\" id=\"name\" name=\"name\" value=\"Darren\" >", tokens[i + 1]);
if (tokens[i].equals("FORM15")) assertEquals("<input type=\"hidden\" name=\"_name\" value=\"on\"/>", tokens[i + 1]);
if (tokens[i].equals("FORM15")) assertEquals("<input type=\"checkbox\" id=\"name\" name=\"name\" checked=\"false\" />", tokens[i + 2]);
if (tokens[i].equals("FORM16")) assertEquals("<input type=\"hidden\" name=\"_jedi\" value=\"on\"/>", tokens[i + 1]);
if (tokens[i].equals("FORM16")) assertEquals("<input type=\"checkbox\" id=\"jedi\" name=\"jedi\" checked=\"true\" />", tokens[i + 2]);
}
}

7
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/velocity/test.vm

@ -53,5 +53,12 @@ FORM9 @@ -53,5 +53,12 @@ FORM9
FORM10
#springFormHiddenInput("command.name" "")
FORM15
#springFormCheckbox("command.name" "")
FORM16
#springFormCheckbox("command.jedi" "")
ERRORS
#springShowErrors(" " "")

Loading…
Cancel
Save