📄 velocityhelper.java
字号:
package webwork.view.velocity;import java.util.Properties;import java.util.Enumeration;import java.util.Iterator;import java.io.Writer;import javax.servlet.ServletContext;import javax.servlet.ServletResponse;import javax.servlet.ServletRequest;import org.apache.velocity.app.Velocity;import org.apache.velocity.context.Context;import org.apache.velocity.VelocityContext;import org.apache.velocity.Template;import org.apache.velocity.runtime.RuntimeSingleton;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import webwork.config.Configuration;import webwork.util.ServletValueStack;/** * @author Hani Suleiman (hani@formicary.net) * Date: Nov 15 * @author 2003 * Time: 11:21:14 AM */public class VelocityHelper{ private static final Log log = LogFactory.getLog(VelocityHelper.class); /** * The HTTP request object context key. */ public static final String REQUEST = "req"; /** * The HTTP response object context key. */ public static final String RESPONSE = "res"; static final String WEBWORK_UTIL = "webwork"; private static boolean initialized = false; public static final String VELO_CONTEXT = "__webwork__velocity__context"; /** * Hook up Velocity with the WebWork configuration. */ public static synchronized void initVelocity(ServletContext context) throws Exception { // WebWork configuration provides main config final Properties conf = new Properties() { public Object get(Object key) { return Configuration.get(key.toString()); } public String getProperty(String key) { return Configuration.getString(key.toString()); } public Enumeration keys() { final Iterator list = Configuration.list(); return new Enumeration() { public Object nextElement() { return list.next(); } public boolean hasMoreElements() { return list.hasNext(); } }; } }; // Set dynamic properties here // The properties not set here are taken from the WebWork configuration Properties p = new Properties(conf) { public Enumeration keys() { return conf.keys(); } }; /* * first, normalize our velocity log file to be in the * webapp */ String log = p.getProperty(Velocity.RUNTIME_LOG); if(log != null) { log = context.getRealPath(log); if(log != null) { p.setProperty(Velocity.RUNTIME_LOG, log); } } /* * If there is a file loader resource path, treat it the * same way, but only if it doesn't start with /. In that case * we use it as-is to allow the templates to be taken from some * repository (!very useful during development!). */ String path = p.getProperty(Velocity.FILE_RESOURCE_LOADER_PATH); if(path != null && (path.equals("/") || !path.startsWith("/"))) { path = context.getRealPath(path); if(path != null) { p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path); } } Velocity.setApplicationAttribute(ServletContext.class.getName(), context); Velocity.init(p); initialized = true; } public static void merge(Context context, String templateName, Writer writer) { try { Template t = RuntimeSingleton.getTemplate(templateName); t.merge(context, writer); //java.io.PrintWriter out = new java.io.PrintWriter(System.out); //t.merge(context, out); //out.close(); } catch(Exception e) { log.error(e.getMessage(), e); } } public static Context getContext(ServletContext context, ServletRequest request, ServletResponse response) { if(!initialized) { try { initVelocity(context); } catch(Exception e) { log.error(e.getMessage(), e); return null; } } WebWorkVelocityContext ctx = (WebWorkVelocityContext)request.getAttribute(VELO_CONTEXT); if(ctx==null) { ctx = new WebWorkVelocityContext(ServletValueStack.getStack(request)); ctx.put(REQUEST, request); ctx.put(RESPONSE, response); ctx.put(WEBWORK_UTIL, new WebWorkUtil(ctx)); request.setAttribute(VELO_CONTEXT, ctx); } return ctx; } /** * WebWork specific Velocity context implementation. */ static class WebWorkVelocityContext extends VelocityContext { ServletValueStack stack; WebWorkVelocityContext(ServletValueStack aStack) { stack = aStack; } public boolean internalContainsKey(java.lang.Object key) { boolean contains = super.internalContainsKey(key); return contains ? true : stack.test(key.toString()); } public Object internalGet(String key) { return super.internalContainsKey(key) ? super.internalGet(key) : stack.findValue(key); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -