📄 velocitymenudisplayer.java
字号:
/* * VelocityMenuDisplayer.java * * Created on December 7, 2002, 12:35 AM */package net.sf.navigator.displayer;import java.io.IOException;import java.io.StringWriter;import java.util.*;import javax.servlet.ServletContext;import javax.servlet.http.HttpServletRequest;import javax.servlet.jsp.JspException;import javax.servlet.jsp.PageContext;import net.sf.navigator.menu.MenuComponent;import org.apache.commons.lang.StringUtils;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.struts.util.RequestUtils;import org.apache.struts.util.ResponseUtils;import org.apache.velocity.Template;import org.apache.velocity.VelocityContext;import org.apache.velocity.app.Velocity;import org.apache.velocity.app.tools.VelocityFormatter;import org.apache.velocity.runtime.RuntimeConstants;import org.apache.velocity.tools.view.servlet.ServletLogger;import org.apache.velocity.tools.view.servlet.WebappLoader;/** * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a> * @version 1.0 */public class VelocityMenuDisplayer extends MessageResourcesMenuDisplayer { //~ Instance fields ======================================================== private Log log = LogFactory.getLog(VelocityMenuDisplayer.class); private PageContext pageContext = null; /** * Key used to access the ServletContext in * the Velocity application attributes. */ public static final String SERVLET_CONTEXT_KEY = ServletContext.class.getName(); //~ Methods ================================================================ public void init(PageContext pageContext, MenuDisplayerMapping mapping) { super.init(pageContext, mapping); this.pageContext = pageContext; // MR: Copied from VelocityViewServlet to initialize WebappLoader Velocity.setApplicationAttribute(SERVLET_CONTEXT_KEY, pageContext.getServletContext()); // default to servletlogger, which logs to the servlet engines log Velocity.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, ServletLogger.class.getName()); // by default, load resources with webapp resource loader Velocity.setProperty(RuntimeConstants.RESOURCE_LOADER, "webapp"); Velocity.setProperty("webapp.resource.loader.class", WebappLoader.class.getName()); // now all is ready - init Velocity try { // look to see if the user has overridden velocity.properties by // placing velocity.properties in WEB-INF/classes ResourceBundle rb = null; try { rb = ResourceBundle.getBundle("velocity"); } catch (MissingResourceException mre) { // use default rb = ResourceBundle.getBundle("net.sf.navigator.displayer.velocity"); } Properties props = new Properties(); for (Enumeration keys = rb.getKeys(); keys.hasMoreElements();) { String key = (String) keys.nextElement(); props.put(key, rb.getString(key)); } // only initialized the first time it's called, from: // http://jakarta.apache.org/velocity/developer-guide.html // it's ignored for subsequent calls Velocity.init(props); } catch (Exception e) { log.error("Error initializing Velocity: " + e.getMessage()); e.printStackTrace(); } } public void display(MenuComponent menu) throws JspException, IOException { if (isAllowed(menu)) { displayComponents(menu); } } protected void displayComponents(MenuComponent menu) throws JspException, IOException { HttpServletRequest request = (HttpServletRequest) pageContext.getRequest(); Template t = null; boolean debug = Boolean.valueOf(request.getParameter("debug")).booleanValue(); if (debug) { // Print this element to our output writer ResponseUtils.write(pageContext, " [ " + getConfig() + " ] "); } else { try { String template = getConfig(); if (template == null) { throw new JspException("You must specify a template using the 'config' attribute."); } else { log.debug("using template: " + template); } t = Velocity.getTemplate(template); } catch (Exception e) { String msg = "Error initializing Velocity: " + e.toString(); log.error(msg, e); throw new JspException(msg, e); } StringWriter sw = new StringWriter(); VelocityContext context = new VelocityContext(); context.put("formatter", new VelocityFormatter(context)); context.put("now", Calendar.getInstance().getTime()); context.put("ctxPath", request.getContextPath()); // add a helper class for string manipulation context.put("stringUtils", new StringUtils()); // add a Map for use by the Explandable List Menu context.put("map", new HashMap()); // see if a name and property were passed in if (!StringUtils.isEmpty(name)) { Object val1 = RequestUtils.lookup(pageContext, name, null, null); if (val1 != null) { context.put(name, val1); } } // request-scope attributes Enumeration enum = request.getAttributeNames(); while (enum.hasMoreElements()) { String name = (String) enum.nextElement(); Object value = request.getAttribute(name); context.put(name, value); } context.put("request", request); context.put("session", request.getSession()); context.put("menu", menu); context.put("displayer", this); try { t.merge(context, sw); } catch (Exception e) { e.printStackTrace(); throw new JspException(e); } String result = sw.getBuffer().toString(); // Print this element to our output writer ResponseUtils.write(pageContext, result); } } public void end(PageContext context) { this.pageContext = null; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -