From 3527b1c94822b7c626a16a5b764bb030f563a0ea Mon Sep 17 00:00:00 2001 From: rjy7 Date: Fri, 25 Jun 2010 15:30:31 +0000 Subject: [PATCH] NIHVIVO-658 Handle body changing page title in FreeMarker controllers. Modifications to dump directives. --- .../freemarker/FreeMarkerHttpServlet.java | 9 +++- .../controller/freemarker/TestController.java | 1 + .../directives/DumpDataModelDirective.java | 45 ++++++++----------- .../webapp/web/directives/DumpDirective.java | 2 +- webapp/web/templates/freemarker/body/test.ftl | 13 +++--- 5 files changed, 35 insertions(+), 35 deletions(-) diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/FreeMarkerHttpServlet.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/FreeMarkerHttpServlet.java index 61e7f080b..d8239211b 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/FreeMarkerHttpServlet.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/FreeMarkerHttpServlet.java @@ -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 body, Configuration config) { return ""; } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/TestController.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/TestController.java index f3dffabd9..1bf4d6590 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/TestController.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/TestController.java @@ -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"; diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/DumpDataModelDirective.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/DumpDataModelDirective.java index 88352e561..0f8bde190 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/DumpDataModelDirective.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/DumpDataModelDirective.java @@ -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 models = new ArrayList(); + List directives = new ArrayList(); Map dm = (Map) 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 = "

Data model: " + StringUtils.join(models, ", ") + ".

"; + String directiveNames = "

Directives: " + StringUtils.join(directives, ", ") + ".

"; + + String output = modelNames + directiveNames; Writer out = env.getOut(); - out.write(output + "
"); + out.write(output); } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/DumpDirective.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/DumpDirective.java index 07b7353d1..67220005c 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/DumpDirective.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/DumpDirective.java @@ -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(); diff --git a/webapp/web/templates/freemarker/body/test.ftl b/webapp/web/templates/freemarker/body/test.ftl index 8e2c8e54d..eeb6a30fb 100644 --- a/webapp/web/templates/freemarker/body/test.ftl +++ b/webapp/web/templates/freemarker/body/test.ftl @@ -2,21 +2,23 @@ <#-- FreeMarker test cases --> -

Dates

+

${title}

+ +

Dates

  • ${now?datetime}
  • ${now?date}
  • ${now?time}
-

Apples

+

Apples

    <#list apples as apple>
  • ${apple}
-

Fruit

+

Fruit

    <#list fruit as f>
  • ${f}
  • @@ -28,14 +30,14 @@

    Book Title: ${bookTitle}

    -

    Zoo 1

    +

    Zoo 1

      <#list zoo1.animals as animal>
    • ${animal}
    -

    Zoo 2

    +

    Zoo 2

      <#list zoo2.animals as animal>
    • ${animal}
    • @@ -47,3 +49,4 @@ <@dump var="now" /> <@dump var="urls" /> <@dump var="fruit" /> +<@dumpDataModel />