Additions to freemarker test controller/template

This commit is contained in:
rjy7 2010-06-24 19:40:39 +00:00
parent 624edfe4a3
commit c7f4c2d961
2 changed files with 38 additions and 2 deletions

View file

@ -37,7 +37,8 @@ public class TestController extends FreeMarkerHttpServlet {
body.put("now", now); body.put("now", now);
// In template: ${now?date}, ${now?datetime}, ${now?time} // In template: ${now?date}, ${now?datetime}, ${now?time}
// You can add to a collection AFTER putting it in the template data model // You can add to a collection AFTER putting it in the template data model.
// The data model contains a reference to the collection, not a copy.
List<String> fruit = new ArrayList<String>(); List<String> fruit = new ArrayList<String>();
fruit.add("apples"); fruit.add("apples");
fruit.add("bananas"); fruit.add("bananas");
@ -49,12 +50,31 @@ public class TestController extends FreeMarkerHttpServlet {
String animal = "elephant"; String animal = "elephant";
body.put("animal", animal); body.put("animal", animal);
animal = "camel"; animal = "camel";
// Because the data model contains a reference to the collection, changing
// one also changes the other.
List<String> animals = new ArrayList<String>();
animals.add("elephant");
animals.add("tiger");
Map<String, List> zoo1 = new HashMap<String, List>();
Map<String, List> zoo2 = new HashMap<String, List>();
zoo1.put("animals", animals);
zoo2.put("animals", animals);
zoo1.get("animals").add("monkey");
body.put("zoo1", zoo1);
body.put("zoo2", zoo2);
getBerries(body);
// Create the template to see the examples live. // Create the template to see the examples live.
String bodyTemplate = "test.ftl"; String bodyTemplate = "test.ftl";
return mergeBodyToTemplate(bodyTemplate, body); return mergeBodyToTemplate(bodyTemplate, body);
} }
private void getBerries(Map<String, Object> body) {
body.put("berries", "strawberries, raspberries, blueberries");
}
} }

View file

@ -25,6 +25,22 @@
<p><strong>Animal:</strong> ${animal}</p> <p><strong>Animal:</strong> ${animal}</p>
<h2>Zoo 1</h2>
<ul>
<#list zoo1.animals as animal>
<li>${animal}</li>
</#list>
</ul>
<h2>Zoo 2</h2>
<ul>
<#list zoo2.animals as animal>
<li>${animal}</li>
</#list>
</ul>
<p><strong>Berries: </strong>${berries}</p>
<@dump var="now" /> <@dump var="now" />
<@dump var="urls" /> <@dump var="urls" />
<@dump var="fruit" /> <@dump var="fruit" />