Added default formats to Freemarker configuration. Added a test controller and template for running test cases.

This commit is contained in:
rjy7 2010-06-03 16:20:35 +00:00
parent 340970c891
commit 30b07ebd05
5 changed files with 93 additions and 17 deletions

View file

@ -299,6 +299,15 @@
<url-pattern>/about</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>FreeMarkerTestController</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.controller.freemarker.TestController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FreeMarkerTestController</servlet-name>
<url-pattern>/freemarkertest</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>FakeSelfEditController</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.controller.FakeSelfEditController</servlet-class>

View file

@ -2,9 +2,7 @@
package edu.cornell.mannlib.vitro.webapp.controller.freemarker;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
@ -26,13 +24,6 @@ public class AboutController extends FreeMarkerHttpServlet {
body.put("aboutText", portal.getAboutText());
body.put("acknowledgeText", portal.getAcknowledgeText());
// Test of #list directive in template on undefined, null, or empty values
// Basic idea: empty list okay, null or undefined value not okay
// List<String> apples = new ArrayList<String>(); // no error
// List<String> apples = null; // error
// body.put("apples", apples);
// no apples in body: error
String bodyTemplate = "about.ftl";
return mergeBodyToTemplate(bodyTemplate, body);

View file

@ -28,11 +28,11 @@ public class FreeMarkerSetup implements ServletContextListener {
// Specify the data source where the template files come from.
// RY Now being done for each request, in order to support multi-portal apps
// and dynamic theme-loading.
// try {
// cfg.setDirectoryForTemplateLoading(new File(templatePath));
// } catch (IOException e) {
// log.error("Error specifying template directory.");
// }
// try {
// cfg.setDirectoryForTemplateLoading(new File(templatePath));
// } catch (IOException e) {
// log.error("Error specifying template directory.");
// }
String buildEnv = ConfigurationProperties.getProperty("Environment.build");
if (buildEnv != null && buildEnv.equals("development")) {
@ -43,6 +43,20 @@ public class FreeMarkerSetup implements ServletContextListener {
// but just use this:
cfg.setObjectWrapper(new DefaultObjectWrapper());
// Set some formatting defaults. These can be overridden at the template
// or environment (template-processing) level, or for an individual
// instance by using built-ins.
cfg.setLocale(java.util.Locale.US);
String dateFormat = "M/d/yyyy";
cfg.setDateFormat(dateFormat);
String timeFormat = "hh:mm a";
cfg.setTimeFormat(timeFormat);
cfg.setDateTimeFormat(dateFormat + " " + timeFormat);
//cfg.setNumberFormat("#,##0.##");
try {
cfg.setSetting("url_escaping_charset", "ISO-8859-1");
} catch (TemplateException e) {

View file

@ -0,0 +1,47 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.controller.freemarker;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* A place for storing test cases.
* @author rjy7
*
*/
public class TestController extends FreeMarkerHttpServlet {
private static final long serialVersionUID = 1L;
protected String getTitle() {
return "Test";
}
protected String getBody() {
Map<String, Object> body = new HashMap<String, Object>();
// Test of #list directive in template on undefined, null, and empty values.
// Basic idea: empty list okay, null or undefined value not okay.
List<String> apples = new ArrayList<String>(); // no error
// List<String> apples = null; // error
body.put("apples", apples); // without this: error
Calendar cal = Calendar.getInstance();
Date now = cal.getTime();
body.put("now", now);
// In template: ${now?date}, ${now?datetime}, ${now?time}
// Create the template to see the examples live.
String bodyTemplate = "test.ftl";
return mergeBodyToTemplate(bodyTemplate, body);
}
}

View file

@ -0,0 +1,15 @@
<#-- $This file is distributed under the terms of the license in /doc/license.txt -->
<#-- FreeMarker test cases -->
<h2>Dates</h2>
<p>${now?datetime}</p>
<p>${now?date}</p>
<p>${now?time}</p>
<h2>Apples</h2>
<#list apples as apple>
<p>${apple}</p>
</#list>