Use FreeMarker templates to generate page identity, menu, search, and footer in JSP pages. This will allow theme authors to work just with the ftls rather than having to maintain both jsp and ftl versions of these templates, during the transition from JSP to FreeMarker.
This commit is contained in:
parent
73172f4f40
commit
3105e29bca
7 changed files with 89 additions and 10 deletions
|
@ -0,0 +1,50 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package edu.cornell.mannlib.vitro.webapp.controller;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
/**
|
||||
* TEMPORARY for transition from JSP to FreeMarker. Once transition
|
||||
* is complete and no more pages are generated in JSP, this can be removed.
|
||||
*
|
||||
* @author rjy7
|
||||
*
|
||||
*/
|
||||
public class FreeMarkerComponentGenerator extends FreeMarkerHttpServlet {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private static final Log log = LogFactory.getLog(FreeMarkerHttpServlet.class.getName());
|
||||
|
||||
FreeMarkerComponentGenerator(HttpServletRequest request, HttpServletResponse response) {
|
||||
doSetup(request, response);
|
||||
}
|
||||
|
||||
public String getIdentity() {
|
||||
return get("identity");
|
||||
}
|
||||
|
||||
public String getMenu() {
|
||||
return get("menu");
|
||||
}
|
||||
|
||||
public String getSearch() {
|
||||
return get("search");
|
||||
}
|
||||
|
||||
public String getFooter() {
|
||||
return get("footer");
|
||||
}
|
||||
|
||||
private String get(String templateName) {
|
||||
String template = "components/" + templateName + ".ftl";
|
||||
return mergeToTemplate(template, root).toString();
|
||||
}
|
||||
|
||||
}
|
|
@ -47,6 +47,7 @@ public class FreeMarkerHttpServlet extends VitroHttpServlet {
|
|||
|
||||
public static Configuration config = null;
|
||||
public static String contextPath = null;
|
||||
public static ServletContext context = null;
|
||||
|
||||
protected VitroRequest vreq;
|
||||
protected HttpServletResponse response;
|
||||
|
@ -60,6 +61,7 @@ public class FreeMarkerHttpServlet extends VitroHttpServlet {
|
|||
throws IOException, ServletException {
|
||||
|
||||
try {
|
||||
callSuperGet(request, response); // RY Yuck...redo
|
||||
doSetup(request, response);
|
||||
setTitle();
|
||||
setBody();
|
||||
|
@ -167,8 +169,7 @@ public class FreeMarkerHttpServlet extends VitroHttpServlet {
|
|||
}
|
||||
}
|
||||
|
||||
protected void doSetup(HttpServletRequest request, HttpServletResponse response) {
|
||||
|
||||
protected void callSuperGet(HttpServletRequest request, HttpServletResponse response) {
|
||||
try {
|
||||
super.doGet(request,response);
|
||||
} catch (ServletException e) {
|
||||
|
@ -178,6 +179,11 @@ public class FreeMarkerHttpServlet extends VitroHttpServlet {
|
|||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// RY This needs to be broken out as is for FreeMarkerComponentGenerator, which should not
|
||||
// include callSuperGet(). So it's only temporary.
|
||||
protected void doSetup(HttpServletRequest request, HttpServletResponse response) {
|
||||
|
||||
vreq = new VitroRequest(request);
|
||||
this.response = response;
|
||||
|
@ -294,10 +300,8 @@ public class FreeMarkerHttpServlet extends VitroHttpServlet {
|
|||
// and we have multi-portal installations, and (b) we need to support theme-switching on the fly.
|
||||
// To make more efficient, we could do this once, and then have a listener that does it again
|
||||
// when theme is switched.BUT this doesn't support (a), only (b), so we have to do it on every request.
|
||||
private final void setTemplateLoader() {
|
||||
protected final void setTemplateLoader() {
|
||||
|
||||
// RY If this is needed in other methods, put in instance var
|
||||
ServletContext context = getServletContext();
|
||||
String themeTemplateDir = context.getRealPath(portal.getThemeDir()) + "/ftl";
|
||||
String vitroTemplateDir = context.getRealPath("/templates/freemarker");
|
||||
|
||||
|
@ -325,4 +329,17 @@ public class FreeMarkerHttpServlet extends VitroHttpServlet {
|
|||
return new TabMenu(vreq, portalId);
|
||||
}
|
||||
|
||||
// TEMPORARY for transition from JSP to FreeMarker. Once transition
|
||||
// is complete and no more pages are generated in JSP, this can be removed.
|
||||
// Do this if FreeMarker is configured (i.e., not Datastar) and if we are not in
|
||||
// a FreeMarkerHttpServlet, which will generate identity, menu, and footer from the page template.
|
||||
// It's a static method because it needs to be called from JSPs that don't go through a servlet.
|
||||
public static void getFreeMarkerComponentsForJsp(HttpServletRequest request, HttpServletResponse response) {
|
||||
FreeMarkerComponentGenerator fcg = new FreeMarkerComponentGenerator(request, response);
|
||||
request.setAttribute("ftl_identity", fcg.getIdentity());
|
||||
request.setAttribute("ftl_menu", fcg.getMenu());
|
||||
request.setAttribute("ftl_search", fcg.getSearch());
|
||||
request.setAttribute("ftl_footer", fcg.getFooter());
|
||||
}
|
||||
|
||||
}
|
|
@ -63,7 +63,16 @@ public class VitroHttpServlet extends HttpServlet
|
|||
request.getAttribute("webappDaoFactory") == null ){
|
||||
log.warn("request scope was not prepared by VitroRequestPrep");
|
||||
}
|
||||
|
||||
// TEMPORARY for transition from JSP to FreeMarker. Once transition
|
||||
// is complete and no more pages are generated in JSP, this can be removed.
|
||||
// Do this if FreeMarker is configured (i.e., not Datastar) and if we are not in
|
||||
// a FreeMarkerHttpServlet, which will generate identity, menu, and footer from the page template.
|
||||
if ( FreeMarkerHttpServlet.config != null && !(this instanceof FreeMarkerHttpServlet) ) {
|
||||
FreeMarkerHttpServlet.getFreeMarkerComponentsForJsp(request, response);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* doPost does the same thing as the doGet method
|
||||
|
|
|
@ -59,8 +59,11 @@ public class FreeMarkerSetup implements ServletContextListener {
|
|||
|
||||
String contextPath = sc.getContextPath();
|
||||
FreeMarkerHttpServlet.contextPath = contextPath;
|
||||
FreeMarkerHttpServlet.context = sc;
|
||||
ViewObject.contextPath = contextPath;
|
||||
|
||||
// For JSP => FreeMarker transition.
|
||||
|
||||
}
|
||||
|
||||
public void contextDestroyed(ServletContextEvent event) {
|
||||
|
|
|
@ -76,9 +76,8 @@ public class StringUtils {
|
|||
return quotedList(Arrays.asList(stringArray), glue);
|
||||
}
|
||||
|
||||
// Because we can't use Java 1.6 String.isEmpty()
|
||||
public static boolean isEmpty(String s) {
|
||||
return s == null || s.length() <= 0;
|
||||
return s == null || s.isEmpty();
|
||||
}
|
||||
|
||||
public static boolean equalsOneOf(String s, String... strings) {
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
|
||||
<#include "/components/identity.ftl">
|
||||
|
||||
<#-- Note to UI team: do not change this div without also making the change in menu.jsp -->
|
||||
<div id="navAndSearch" class="block">
|
||||
<#include "/components/menu.ftl">
|
||||
<#include "/components/search.ftl">
|
||||
|
|
|
@ -24,7 +24,7 @@
|
|||
securityLevel = Integer.parseInt(loginHandler.getLoginRole());
|
||||
loginName = loginHandler.getLoginName();
|
||||
}
|
||||
|
||||
// VITRO FILE
|
||||
final Log log = LogFactory.getLog("edu.cornell.mannlib.vitro.web.menu.jsp");
|
||||
|
||||
VitroRequest vreq = new VitroRequest(request);
|
||||
|
|
Loading…
Add table
Reference in a new issue