From c178c8d922ec0d51f0824b17a4a9cd3a061b4165 Mon Sep 17 00:00:00 2001 From: rjy7 Date: Fri, 29 Oct 2010 16:44:29 +0000 Subject: [PATCH] NIHVIVO-1297 Undid the effects of revision 6246. This was a mistake, since including the body template from the page template rather than pregenerating the body string doesn't allow the addition of stylesheet tags to the page head. --- .../FreemarkerComponentGenerator.java | 4 +- .../freemarker/FreemarkerHttpServlet.java | 134 ++++++++++++------ .../freemarker/HomePageController.java | 6 +- .../controller/login/LoginTemplateHelper.java | 13 +- .../freemarker/body/login/login-form.ftl | 2 +- .../body/search/search-pagedResults.ftl | 2 +- .../templates/freemarker/page/page-home.ftl | 5 - webapp/web/templates/freemarker/page/page.ftl | 2 +- 8 files changed, 103 insertions(+), 65 deletions(-) delete mode 100644 webapp/web/templates/freemarker/page/page-home.ftl diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/FreemarkerComponentGenerator.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/FreemarkerComponentGenerator.java index 6dcc4815b..678728242 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/FreemarkerComponentGenerator.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/FreemarkerComponentGenerator.java @@ -32,7 +32,9 @@ public class FreemarkerComponentGenerator extends FreemarkerHttpServlet { VitroRequest vreq = new VitroRequest(request); Configuration config = getConfig(vreq); - Map root = getPageValues(vreq, new HashMap()); + // root is the map used to create the page shell - header, footer, menus, etc. + Map root = getSharedVariables(vreq, new HashMap()); + root.putAll(getRootValues(vreq)); request.setAttribute("ftl_identity", get("identity", root, config)); request.setAttribute("ftl_menu", get("menu", root, config)); 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 e85f55e8a..17fdcca6a 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 @@ -259,14 +259,32 @@ public class FreemarkerHttpServlet extends VitroHttpServlet { Configuration config = getConfig(vreq); Map bodyMap = values.getMap(); - Map map = new HashMap(); - map.putAll(getPageValues(vreq, bodyMap)); - - // Add the values from the subcontroller. - map.putAll(bodyMap); - map.put("bodyTemplate", values.getTemplateName()); + // We can't use shared variables in the Freemarker configuration to store anything + // except theme-specific data, because multiple portals or apps might share the same theme. So instead + // just put the shared variables in both root and body. + Map sharedVariables = getSharedVariables(vreq, bodyMap); - writePage(map, config, response); + // root is the map used to create the page shell - header, footer, menus, etc. + Map root = new HashMap(sharedVariables); + + // body is the map used to create the page body + Map body = new HashMap(sharedVariables); + root.putAll(getRootValues(vreq)); + + // Add the values that we got, and merge to the template. + String bodyTemplate = values.getTemplateName(); + String bodyString; + if (bodyTemplate != null) { + body.putAll(bodyMap); + bodyString = mergeMapToTemplate(bodyTemplate, body, config); + } else { + // The subcontroller has not defined a body template. All markup for the page + // is specified in the main page template. + bodyString = ""; + } + root.put("body", bodyString); + + writePage(root, config, response); } protected void doRedirect(HttpServletRequest request, HttpServletResponse response, ResponseValues values) @@ -312,6 +330,50 @@ public class FreemarkerHttpServlet extends VitroHttpServlet { doTemplate(vreq, response, trv); } + // We can't use shared variables in the Freemarker configuration to store anything + // except theme-specific data, because multiple portals or apps might share the same theme. So instead + // we'll get all the shared variables here, and put them in both root and body maps. + protected Map getSharedVariables(VitroRequest vreq, Map bodyMap) { + + Map map = new HashMap(); + + Portal portal = vreq.getPortal(); + // Ideally, templates wouldn't need portal id. Currently used as a hidden input value + // in the site search box, so needed for now. + map.put("portalId", portal.getPortalId()); + + String siteName = portal.getAppName(); + map.put("siteName", siteName); + + // In some cases the title is determined during the subclass processRequest() method; e.g., + // for an Individual profile page, the title should be the Individual's label. In that case, + // put that title in the sharedVariables map because it's needed by the page root map as well + // to generate the element. Otherwise, use the getTitle() method to generate the title. + String title = (String) bodyMap.get("title"); + if (StringUtils.isEmpty(title)) { + title = getTitle(siteName); + } + map.put("title", title); + + String themeDir = getThemeDir(portal); + UrlBuilder urlBuilder = new UrlBuilder(portal); + + map.put("urls", getUrls(themeDir, urlBuilder)); + + map.put("themeDir", themeDir); + + map.put("themeDir", themeDir); + + map.put("stylesheets", getStylesheetList(themeDir)); + map.put("scripts", getScriptList(themeDir)); + map.put("headScripts", getScriptList(themeDir)); + + map.putAll(getDirectives()); + + return map; + } + + public String getThemeDir(Portal portal) { return portal.getThemeDir().replaceAll("/$", ""); } @@ -382,65 +444,43 @@ public class FreemarkerHttpServlet extends VitroHttpServlet { return map; } - // Values needed to generate the page frame - header, footer, menus, etc. Some may also be used in the - // page body. + // Add variables that should be available only to the page's root map, not to the body. // RY This is protected instead of private so FreeMarkerComponentGenerator can access. // Once we don't need that (i.e., jsps have been eliminated) we can make it private. - protected Map<String, Object> getPageValues(VitroRequest vreq, Map<String, Object> bodyMap) { - - Map<String, Object> map = new HashMap<String, Object>(); + protected Map<String, Object> getRootValues(VitroRequest vreq) { + Map<String, Object> root = new HashMap<String, Object>(); + root.put("tabMenu", getTabMenu(vreq)); + Portal portal = vreq.getPortal(); - // Ideally, templates wouldn't need portal id. Currently used as a hidden input value - // in the site search box, so needed for now. - map.put("portalId", portal.getPortalId()); - String siteName = portal.getAppName(); - map.put("siteName", siteName); - - // In some cases the title is determined during the subclass processRequest() method; e.g., - // for an Individual profile page, the title should be the Individual's label. In that case, - // put that title in the sharedVariables map because it's needed by the page root map as well - // to generate the <title> element. Otherwise, use the getTitle() method to generate the title. - String title = (String) bodyMap.get("title"); - if (StringUtils.isEmpty(title)) { - title = getTitle(siteName); - } - map.put("title", title); - - String themeDir = getThemeDir(portal); - UrlBuilder urlBuilder = new UrlBuilder(portal); - map.put("urls", getUrls(themeDir, urlBuilder)); - map.put("themeDir", themeDir); - map.put("stylesheets", getStylesheetList(themeDir)); - map.put("scripts", getScriptList(themeDir)); - map.put("headScripts", getScriptList(themeDir)); - map.putAll(getDirectives()); - map.put("tabMenu", getTabMenu(vreq)); - ApplicationBean appBean = vreq.getAppBean(); PortalWebUtil.populateSearchOptions(portal, appBean, vreq.getWebappDaoFactory().getPortalDao()); PortalWebUtil.populateNavigationChoices(portal, vreq, appBean, vreq.getWebappDaoFactory().getPortalDao()); - map.putAll(getLoginValues(vreq)); + root.putAll(getLoginValues(vreq)); - map.put("copyright", getCopyrightInfo(portal)); - map.put("siteTagline", portal.getShortHand()); - map.put("breadcrumbs", BreadCrumbsUtil.getBreadCrumbsDiv(vreq)); + UrlBuilder urlBuilder = new UrlBuilder(portal); + root.put("version", getRevisionInfo(urlBuilder)); + + root.put("copyright", getCopyrightInfo(portal)); + root.put("siteTagline", portal.getShortHand()); + root.put("breadcrumbs", BreadCrumbsUtil.getBreadCrumbsDiv(vreq)); + + String themeDir = getThemeDir(portal); // This value is used only in stylesheets.ftl and already contains the context path. - map.put("stylesheetPath", UrlBuilder.getUrl(themeDir + "/css")); + root.put("stylesheetPath", UrlBuilder.getUrl(themeDir + "/css")); String bannerImage = portal.getBannerImage(); if ( ! StringUtils.isEmpty(bannerImage)) { - map.put("bannerImage", UrlBuilder.getUrl(themeDir + "site_icons/" + bannerImage)); + root.put("bannerImage", UrlBuilder.getUrl(themeDir + "site_icons/" + bannerImage)); } - map.put("version", getRevisionInfo(urlBuilder)); - - return map; + return root; } + private TabMenu getTabMenu(VitroRequest vreq) { int portalId = vreq.getPortal().getPortalId(); return new TabMenu(vreq, portalId); diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/HomePageController.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/HomePageController.java index 1c82db1f5..3e9c8305d 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/HomePageController.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/HomePageController.java @@ -23,10 +23,8 @@ public class HomePageController extends FreemarkerHttpServlet { @Override protected ResponseValues processRequest(VitroRequest vreq) { - Map<String, Object> body = new HashMap<String, Object>(); - - // Add home page data to body here - + Map<String, Object> body = new HashMap<String, Object>(); + // Add home page data to body here return new TemplateResponseValues(BODY_TEMPLATE, body); } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/login/LoginTemplateHelper.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/login/LoginTemplateHelper.java index 070b811bf..7af6d4c3e 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/login/LoginTemplateHelper.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/login/LoginTemplateHelper.java @@ -39,7 +39,7 @@ public class LoginTemplateHelper extends LoginTemplateHelperBase { /** Show error message */ public static final String TEMPLATE_SERVER_ERROR = Template.ERROR_MESSAGE.toString(); - public static final String BODY_LOGIN_NAME = "username"; + public static final String BODY_LOGIN_NAME = "loginName"; public static final String BODY_FORM_ACTION = "formAction"; public static final String BODY_INFO_MESSAGE = "infoMessage"; public static final String BODY_ERROR_MESSAGE = "errorMessage"; @@ -156,11 +156,14 @@ public class LoginTemplateHelper extends LoginTemplateHelperBase { private String doTemplate(VitroRequest vreq, TemplateResponseValues values) { // Set it up like FreeMarkerHttpServlet.doGet() would do. Configuration config = getConfig(vreq); - Map<String, Object> root = getPageValues(vreq, new HashMap<String, Object>()); + Map<String, Object> sharedVariables = getSharedVariables(vreq, new HashMap<String, Object>()); + Map<String, Object> root = new HashMap<String, Object>(sharedVariables); + Map<String, Object> body = new HashMap<String, Object>(sharedVariables); + root.putAll(getRootValues(vreq)); - // Add the TemplateResponseValues - root.putAll(values.getMap()); - return mergeMapToTemplate(values.getTemplateName(), root, config); + // Add the values that we got, and merge to the template. + body.putAll(values.getMap()); + return mergeMapToTemplate(values.getTemplateName(), body, config); } /** diff --git a/webapp/web/templates/freemarker/body/login/login-form.ftl b/webapp/web/templates/freemarker/body/login/login-form.ftl index 4e42b728f..f37a65503 100644 --- a/webapp/web/templates/freemarker/body/login/login-form.ftl +++ b/webapp/web/templates/freemarker/body/login/login-form.ftl @@ -27,7 +27,7 @@ <form action="${formAction}" method="post"> <label for="loginName">Email or Username</label> - <input id="loginName" class="focus" name="loginName" type="text" value="${username}" /> + <input id="loginName" class="focus" name="loginName" type="text" value="${loginName}" /> <label for="loginPassword">Password</label> <input id="loginPassword" type="password" name="loginPassword" /> <input name="loginForm" type="submit" class="submit" value="Log in"/> diff --git a/webapp/web/templates/freemarker/body/search/search-pagedResults.ftl b/webapp/web/templates/freemarker/body/search/search-pagedResults.ftl index 63184d552..a7daa6a0a 100644 --- a/webapp/web/templates/freemarker/body/search/search-pagedResults.ftl +++ b/webapp/web/templates/freemarker/body/search/search-pagedResults.ftl @@ -56,4 +56,4 @@ </div> <!-- end contentsBrowseGroup --> -${stylesheets.addFromTheme("/css/search.css")} \ No newline at end of file +${stylesheets.addFromTheme("/css/search.css")} diff --git a/webapp/web/templates/freemarker/page/page-home.ftl b/webapp/web/templates/freemarker/page/page-home.ftl deleted file mode 100644 index a401fe61f..000000000 --- a/webapp/web/templates/freemarker/page/page-home.ftl +++ /dev/null @@ -1,5 +0,0 @@ -<#-- $This file is distributed under the terms of the license in /doc/license.txt$ --> - -<#-- Page template for home page --> - -<#include "page.ftl"> \ No newline at end of file diff --git a/webapp/web/templates/freemarker/page/page.ftl b/webapp/web/templates/freemarker/page/page.ftl index 6bb4bc8f4..a0baa46b5 100644 --- a/webapp/web/templates/freemarker/page/page.ftl +++ b/webapp/web/templates/freemarker/page/page.ftl @@ -23,7 +23,7 @@ <div id="contentwrap"> <div id="content"> - <#include bodyTemplate> + ${body} </div> <!-- content --> </div> <!-- contentwrap -->