Use FreeMarker controller and templates for About and Index pages. Improvements to FreeMarkerHttpServlet.getUrl() method. Unit tests for FreeMarkerHttpServlet static methods.
This commit is contained in:
parent
92f9280af0
commit
046ca60439
9 changed files with 140 additions and 68 deletions
|
@ -15,8 +15,10 @@ import java.util.List;
|
|||
*
|
||||
*/
|
||||
|
||||
public class Controllers {
|
||||
//servlet urls go here:
|
||||
public class Controllers {
|
||||
|
||||
// Servlet urls
|
||||
|
||||
public static final String ENTITY = "/entity";
|
||||
public static final String ENTITY_PROP_LIST = "/entityPropList";
|
||||
public static final String ENTITY_LIST = "/EntityList";
|
||||
|
@ -31,7 +33,12 @@ public class Controllers {
|
|||
public static final String LOGIN = "/siteAdmin";
|
||||
public static final String LOGOUT = "/siteAdmin";
|
||||
|
||||
public static final String EXPORT_RDF = "/export";
|
||||
public static final String EXPORT_RDF = "/export";
|
||||
|
||||
public static final String CONTACT_URL = "/comments";
|
||||
public static final String TERMS_OF_USE_URL = "/termsOfUse";
|
||||
public static final String BROWSE = "/browse";
|
||||
public static final String INDIVIDUAL_LIST_URL = "/entitylist"; // will change to individuallist
|
||||
|
||||
// jsps go here:
|
||||
|
||||
|
@ -86,9 +93,8 @@ public class Controllers {
|
|||
|
||||
public static final String TOGGLE_SCRIPT_ELEMENT = "<script language='JavaScript' type='text/javascript' src='js/toggle.js'></script>";
|
||||
|
||||
public static final Object SEARCH_ERROR_JSP = "/search_error.jsp";
|
||||
public static final Object SEARCH_ERROR_JSP = "/search_error.jsp";
|
||||
|
||||
public static final String CONTACT_URL = "/comments";
|
||||
|
||||
//public static final String TAB_ENTITIES_LIST_JSP = "templates/tab/tabEntities.jsp";
|
||||
|
||||
|
|
|
@ -6,11 +6,15 @@ import java.io.File;
|
|||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
|
@ -242,13 +246,16 @@ public class FreeMarkerHttpServlet extends VitroHttpServlet {
|
|||
if ( ! StringUtils.isEmpty(bannerImage)) {
|
||||
root.put("bannerImage", getUrl(themeDir + "site_icons/" + bannerImage));
|
||||
}
|
||||
|
||||
Map<String, String> portalParam = new HashMap<String, String>();
|
||||
portalParam.put("home", "" + portalId);
|
||||
|
||||
urls.put("about", getUrl(Controllers.ABOUT + "?home=" + portalId));
|
||||
urls.put("about", getUrl(Controllers.ABOUT, portalParam));
|
||||
if (ContactMailServlet.getSmtpHostFromProperties() != null) {
|
||||
urls.put("contact", getUrl(Controllers.CONTACT_URL + "?home=" + portalId));
|
||||
urls.put("contact", getUrl(Controllers.CONTACT_URL, portalParam));
|
||||
}
|
||||
urls.put("search", getUrl(Controllers.SEARCH_URL));
|
||||
urls.put("termsOfUse", getUrl("/termsOfUse?home=" + portalId));
|
||||
urls.put("termsOfUse", getUrl(Controllers.TERMS_OF_USE_URL, portalParam));
|
||||
urls.put("login", getUrl(Controllers.LOGIN));
|
||||
urls.put("logout", getUrl(Controllers.LOGOUT));
|
||||
urls.put("siteAdmin", getUrl(Controllers.SITE_ADMIN));
|
||||
|
@ -318,14 +325,7 @@ public class FreeMarkerHttpServlet extends VitroHttpServlet {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
public static String getUrl(String path) {
|
||||
if ( ! path.startsWith("/") ) {
|
||||
path = "/" + path;
|
||||
}
|
||||
return contextPath + path;
|
||||
}
|
||||
|
||||
|
||||
private TabMenu getTabMenu(int portalId) {
|
||||
return new TabMenu(vreq, portalId);
|
||||
}
|
||||
|
@ -343,4 +343,41 @@ public class FreeMarkerHttpServlet extends VitroHttpServlet {
|
|||
request.setAttribute("ftl_footer", fcg.getFooter());
|
||||
}
|
||||
|
||||
|
||||
/* ******************** Utilities ******************* */
|
||||
|
||||
public static String getUrl(String path) {
|
||||
if ( ! path.startsWith("/") ) {
|
||||
path = "/" + path;
|
||||
}
|
||||
return contextPath + path;
|
||||
}
|
||||
|
||||
public static String getUrl(String path, Map<String, String> params) {
|
||||
String url = getUrl(path);
|
||||
|
||||
Iterator<String> i = params.keySet().iterator();
|
||||
String key, value;
|
||||
String glue = "?";
|
||||
while (i.hasNext()) {
|
||||
key = i.next();
|
||||
value = params.get(key);
|
||||
url += glue + key + "=" + urlEncode(value);
|
||||
glue = "&";
|
||||
}
|
||||
|
||||
return url;
|
||||
}
|
||||
|
||||
public static String urlEncode(String url) {
|
||||
String encoding = "ISO-8859-1";
|
||||
String encodedUrl = null;
|
||||
try {
|
||||
encodedUrl = URLEncoder.encode(url, encoding);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
log.error("Error encoding url " + url + " with encoding " + encoding + ": Unsupported encoding.");
|
||||
}
|
||||
return encodedUrl;
|
||||
}
|
||||
|
||||
}
|
|
@ -35,6 +35,7 @@ public class IndividualView extends ViewObject {
|
|||
|
||||
// Or maybe getProfileUrl - there might be other kinds of urls
|
||||
// e.g., getEditUrl, getDeleteUrl - these would return the computations of PropertyEditLinks
|
||||
// Just call getUrl...
|
||||
public String getProfileUrl() {
|
||||
return contextPath + URL + ""; // ADD IN the label from the individual's uri
|
||||
}
|
||||
|
|
|
@ -12,15 +12,6 @@ import org.apache.commons.logging.LogFactory;
|
|||
import edu.cornell.mannlib.vitro.webapp.beans.VClass;
|
||||
import edu.cornell.mannlib.vitro.webapp.beans.VClassGroup;
|
||||
|
||||
/*
|
||||
* A VClassGroupDisplay object is associated with a VClassGroup for display.
|
||||
* It is an object that contains a linked list, rather than a type of linked list,
|
||||
* so that JSP EL can access properties such as publicName.
|
||||
*
|
||||
* RY We may want an abstract display class as a superclass.
|
||||
* RY We may want an interface that the superclass would implement.
|
||||
* RY We may want to nest this class in the VClassGroup class.
|
||||
*/
|
||||
public class VClassGroupView extends ViewObject {
|
||||
|
||||
private static final Log log = LogFactory.getLog(VClassGroupView.class.getName());
|
||||
|
@ -54,6 +45,7 @@ public class VClassGroupView extends ViewObject {
|
|||
|
||||
public List<VClassView> getClasses() {
|
||||
// Do we need to store the classes as an instance member? Would we ever access this method more than once per template?
|
||||
// Don't do this in the constructor, since we might not need it.
|
||||
if (classes == null) {
|
||||
List<VClass> classList = vClassGroup.getVitroClassList();
|
||||
classes = new ArrayList<VClassView>();
|
||||
|
|
|
@ -2,15 +2,19 @@
|
|||
|
||||
package edu.cornell.mannlib.vitro.webapp.view;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.beans.VClass;
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.Controllers;
|
||||
|
||||
public class VClassView extends ViewObject {
|
||||
|
||||
private static final Log log = LogFactory.getLog(VClassView.class.getName());
|
||||
private static final String URL = "/individuallistFM?vclassId=";
|
||||
private static final String URL = Controllers.INDIVIDUAL_LIST_URL;
|
||||
|
||||
private VClass vclass;
|
||||
|
||||
|
@ -23,7 +27,9 @@ public class VClassView extends ViewObject {
|
|||
}
|
||||
|
||||
public String getUrl() {
|
||||
return contextPath + URL + encodeUrl(vclass.getURI());
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
params.put("vclassId", vclass.getURI());
|
||||
return getUrl(URL, params);
|
||||
}
|
||||
|
||||
public int getEntityCount() {
|
||||
|
|
|
@ -2,36 +2,33 @@
|
|||
|
||||
package edu.cornell.mannlib.vitro.webapp.view;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.FreeMarkerHttpServlet;
|
||||
import edu.cornell.mannlib.vitro.webapp.utils.StringUtils;
|
||||
|
||||
// RY We may want an interface that the superclass would implement.
|
||||
// RY Consider using FreeMarker's DisplayObjectWrapper instead, or extending it.
|
||||
|
||||
public abstract class ViewObject {
|
||||
|
||||
private static final Log log = LogFactory.getLog(ViewObject.class.getName());
|
||||
|
||||
// RY Can probably remove this, since we're using the FreeMarkerHttpServlet methods instead
|
||||
public static String contextPath;
|
||||
|
||||
protected static String getUrl(String path) {
|
||||
protected String getUrl(String path) {
|
||||
return FreeMarkerHttpServlet.getUrl(path);
|
||||
}
|
||||
|
||||
protected String encodeUrl(String url) {
|
||||
String encoding = "ISO-8859-1";
|
||||
String encodedUrl = null;
|
||||
try {
|
||||
encodedUrl = URLEncoder.encode(url, encoding);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
log.error("Error encoding url " + url + " with encoding " + encoding + ": Unsupported encoding.");
|
||||
}
|
||||
return encodedUrl;
|
||||
protected String getUrl(String path, Map<String, String> params) {
|
||||
return FreeMarkerHttpServlet.getUrl(path, params);
|
||||
}
|
||||
|
||||
protected String urlEncode(String str) {
|
||||
return FreeMarkerHttpServlet.urlEncode(str);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -44,7 +44,7 @@ public class TabMenu extends MainMenu {
|
|||
// Hard-coded tabs. It's not really a good idea to have these here, since any menu item that doesn't
|
||||
// come from the db should be accessible to the template to change the text. But we need them here
|
||||
// to apply the "active" mechanism.
|
||||
addItem("Index", "/browsecontroller");
|
||||
addItem("Index", "/browse");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue