Added default formats to Freemarker configuration. Added a test controller and template for running test cases.
This commit is contained in:
parent
340970c891
commit
30b07ebd05
5 changed files with 93 additions and 17 deletions
|
@ -299,6 +299,15 @@
|
||||||
<url-pattern>/about</url-pattern>
|
<url-pattern>/about</url-pattern>
|
||||||
</servlet-mapping>
|
</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>
|
||||||
<servlet-name>FakeSelfEditController</servlet-name>
|
<servlet-name>FakeSelfEditController</servlet-name>
|
||||||
<servlet-class>edu.cornell.mannlib.vitro.webapp.controller.FakeSelfEditController</servlet-class>
|
<servlet-class>edu.cornell.mannlib.vitro.webapp.controller.FakeSelfEditController</servlet-class>
|
||||||
|
|
|
@ -2,9 +2,7 @@
|
||||||
|
|
||||||
package edu.cornell.mannlib.vitro.webapp.controller.freemarker;
|
package edu.cornell.mannlib.vitro.webapp.controller.freemarker;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
|
@ -26,13 +24,6 @@ public class AboutController extends FreeMarkerHttpServlet {
|
||||||
body.put("aboutText", portal.getAboutText());
|
body.put("aboutText", portal.getAboutText());
|
||||||
body.put("acknowledgeText", portal.getAcknowledgeText());
|
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";
|
String bodyTemplate = "about.ftl";
|
||||||
return mergeBodyToTemplate(bodyTemplate, body);
|
return mergeBodyToTemplate(bodyTemplate, body);
|
||||||
|
|
||||||
|
|
|
@ -28,20 +28,34 @@ public class FreeMarkerSetup implements ServletContextListener {
|
||||||
// Specify the data source where the template files come from.
|
// Specify the data source where the template files come from.
|
||||||
// RY Now being done for each request, in order to support multi-portal apps
|
// RY Now being done for each request, in order to support multi-portal apps
|
||||||
// and dynamic theme-loading.
|
// and dynamic theme-loading.
|
||||||
// try {
|
// try {
|
||||||
// cfg.setDirectoryForTemplateLoading(new File(templatePath));
|
// cfg.setDirectoryForTemplateLoading(new File(templatePath));
|
||||||
// } catch (IOException e) {
|
// } catch (IOException e) {
|
||||||
// log.error("Error specifying template directory.");
|
// log.error("Error specifying template directory.");
|
||||||
// }
|
// }
|
||||||
|
|
||||||
String buildEnv = ConfigurationProperties.getProperty("Environment.build");
|
String buildEnv = ConfigurationProperties.getProperty("Environment.build");
|
||||||
if (buildEnv != null && buildEnv.equals("development")) {
|
if (buildEnv != null && buildEnv.equals("development")) {
|
||||||
cfg.setTemplateUpdateDelay(0); // no template caching in development
|
cfg.setTemplateUpdateDelay(0); // no template caching in development
|
||||||
}
|
}
|
||||||
|
|
||||||
// Specify how templates will see the data-model. This is an advanced topic...
|
// Specify how templates will see the data-model. This is an advanced topic...
|
||||||
// but just use this:
|
// but just use this:
|
||||||
cfg.setObjectWrapper(new DefaultObjectWrapper());
|
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 {
|
try {
|
||||||
cfg.setSetting("url_escaping_charset", "ISO-8859-1");
|
cfg.setSetting("url_escaping_charset", "ISO-8859-1");
|
||||||
|
|
|
@ -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);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
15
webapp/web/templates/freemarker/body/test.ftl
Normal file
15
webapp/web/templates/freemarker/body/test.ftl
Normal 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>
|
Loading…
Add table
Reference in a new issue