NIHVIVO-658 Handle body changing page title in FreeMarker controllers. Modifications to dump directives.

This commit is contained in:
rjy7 2010-06-25 15:30:31 +00:00
parent f77cf70224
commit 3527b1c948
5 changed files with 35 additions and 35 deletions

View file

@ -82,6 +82,10 @@ public class FreeMarkerHttpServlet extends VitroHttpServlet {
setUpRoot(vreq, root);
root.put("body", getBody(vreq, body, config)); // need config to get and process template
// getBody() may have changed the title, so put the new value in the root map. (E.g., the title may
// include an individual's name, which is only discovered when processing the body.)
root.put("title", body.get("title"));
writePage(root, config, response);
} catch (Throwable e) {
@ -357,11 +361,12 @@ public class FreeMarkerHttpServlet extends VitroHttpServlet {
return copyright;
}
// Subclasses will override. This serves as a default.
// Subclasses may override. This serves as a default.
protected String getTitle(String siteName) {
return siteName;
}
// Most subclasses will override. Some (e.g., ajax controllers) don't need to define a page body.
protected String getBody(VitroRequest vreq, Map<String, Object> body, Configuration config) {
return "";
}

View file

@ -70,6 +70,7 @@ public class TestController extends FreeMarkerHttpServlet {
body.put("bookTitle", "Pride and Prejudice");
body.put("bookTitle", "Persuasion");
body.put("title", "VIVO Test");
// Create the template to see the examples live.
String bodyTemplate = "test.ftl";

View file

@ -4,13 +4,14 @@ package edu.cornell.mannlib.vitro.webapp.web.directives;
import java.io.IOException;
import java.io.Writer;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import edu.cornell.mannlib.vitro.webapp.utils.StringUtils;
import freemarker.core.Environment;
import freemarker.template.Configuration;
import freemarker.template.SimpleScalar;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
@ -40,42 +41,32 @@ public class DumpDataModelDirective implements TemplateDirectiveModel {
}
TemplateHashModel dataModel = env.getDataModel();
String output = "Data model: ";
List<String> models = new ArrayList<String>();
List<String> directives = new ArrayList<String>();
Map<String, Object> dm = (Map<String, Object>) DeepUnwrap.permissiveUnwrap(dataModel);
Set varNames = dm.keySet();
for (Object varName : varNames) {
output += (String) varName + ", ";
}
// Add shared variables
Configuration config = env.getConfiguration();
Set sharedVars = config.getSharedVariableNames();
Iterator i = sharedVars.iterator();
while (i.hasNext()) {
String sv = (String) i.next();
TemplateModel tm = config.getSharedVariable(sv);
if (tm instanceof TemplateDirectiveModel ||
// Legacy built-ins that are added to all configurations
tm instanceof freemarker.template.utility.CaptureOutput ||
tm instanceof freemarker.template.utility.StandardCompress ||
tm instanceof freemarker.template.utility.HtmlEscape ||
tm instanceof freemarker.template.utility.NormalizeNewlines ||
tm instanceof freemarker.template.utility.XmlEscape) {
continue;
if (dm.get(varName) instanceof TemplateDirectiveModel) {
directives.add((String) varName);
} else {
models.add((String) varName);
}
output += sv + ", ";
}
output = output.replaceAll(", $", ".");
Collections.sort(models);
Collections.sort(directives);
// RY Improve by making presentation of various types more nuanced
// Also merge to a template for formatting
// get config from environment; get a template from config
// merge as in FreeMarkerHttpServlet.mergeToTemplate()
String modelNames = "<p><strong>Data model:</strong> " + StringUtils.join(models, ", ") + ".</p>";
String directiveNames = "<p><strong>Directives:</strong> " + StringUtils.join(directives, ", ") + ".</p>";
String output = modelNames + directiveNames;
Writer out = env.getOut();
out.write(output + "<br />");
out.write(output);
}

View file

@ -47,7 +47,7 @@ public class DumpDirective implements TemplateDirectiveModel {
String output = var + ": " + val.toString();
// RY Improve by making presentation of various types more nuanced
// Also merge to a template for formatting
// Also merge to a template for formatting:
// get config from environment; get a template from config
// merge as in FreeMarkerHttpServlet.mergeToTemplate()
Writer out = env.getOut();