自己写的一个工具类: VelocityUtils
velocity1.7小例子 http://www.cnblogs.com/jston/archive/2013/02/19/2916999.html
1 2 3 4 5 6 |
Xml代码 <dependency> <groupId>velocity</groupId> <artifactId>velocity</artifactId> <version>1.4</version> </dependency> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
Java代码 package com.util; import org.apache.velocity.Template; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.Velocity; import org.apache.velocity.app.VelocityEngine; import java.io.InputStream; import java.io.Reader; import java.io.StringWriter; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Properties; /** * 模板处理工具类,现在只支持类路径方式,不支持jar,绝对路径等方式。 * * @author pandy * */ @SuppressWarnings("rawtypes") public class VelocityUtils { private final static String DEFAULT_TEMPLATE_PATH = ""; private final static String DEFAULT_LOG_TAG = "mystring"; private static VelocityEngine engine; /** * 做初始化信息 */ static { Properties p = new Properties(); // 设置输入输出编码类型。和这次说的解决的问题无关 p.setProperty(Velocity.INPUT_ENCODING, "UTF-8"); p.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8"); p.setProperty("userdirective", "org.apache.velocity.tools.generic.directive.Ifnull"); // 这里加载类路径里的模板而不是文件系统路径里的模板 p.setProperty("resource.loader", "class"); p.setProperty("class.resource.loader.class","org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); engine = new VelocityEngine(); try { engine.init(p); Velocity.init(); } catch (Exception e) { e.printStackTrace(); engine = null; } } /** * 参数转换:Pap->VelocityContext * * @param paramters * @return */ private static VelocityContext parseMapToVelocityContext(Map paramters) { VelocityContext context = new VelocityContext(); if (paramters != null && !paramters.isEmpty()) { Iterator it = paramters.keySet().iterator(); while (it.hasNext()) { Object key = it.next(); Object value = paramters.get(key); context.put(key.toString(), value); } } return context; } /** * 读取模板文件 * * @param templateName * @param path * @return */ private static Template getTemplate(String templateName, String path) { Template template = null; try { template = engine.getTemplate(path + templateName); } catch (Exception e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } return template; } /** * * @param templateName * @return 模板的html字符串 */ public static String merge(String templateName) { return merge(templateName, null, null, null); } /** * * @param templateName * @param path * @return */ public static String merge(String templateName, String path) { return merge(templateName, path, null, null); } /** * 这个方法要自动转换Map类型,变成VelocityContext类型 * * @param templateName * @param paramters * @return 模板的html字符串 */ public static String merge(String templateName, Map paramters) { return merge(templateName, DEFAULT_TEMPLATE_PATH, parseMapToVelocityContext(paramters), null); } /** * * @param templateName * @param path * @param paramters * @return */ public static String merge(String templateName, String path, Map paramters) { return merge(templateName, path, parseMapToVelocityContext(paramters), null); } /** * * @param templateName * @param context * @return 模板的html字符串 */ public static String merge(String templateName, String path, VelocityContext context) { return merge(templateName, path, context, null); } /** * * @param templateName * @param context * @return */ public static String merge(String templateName, VelocityContext context) { return merge(templateName, DEFAULT_TEMPLATE_PATH, context, null); } /** * * @param templateName * @param context * @param writer * @return 模板的html字符串 */ public static String merge(String templateName, VelocityContext context, StringWriter writer) { return merge(templateName, DEFAULT_TEMPLATE_PATH, context, writer); } /** * 最终执行方法 * * @param templateName * @param path * @param context * @param writer * @return 模板的html字符串 */ public static String merge(String templateName, String path, VelocityContext context, StringWriter writer) { Template template = getTemplate(templateName, path); if (writer == null) { writer = new StringWriter(); } try { template.merge(context, writer); } catch (Exception e) { e.printStackTrace(); return null; } /* show the World */ return writer.toString(); } public static String mergeWithStr(Map paramters, String templateStr) { StringWriter writer = new StringWriter(); return mergeWithStr(parseMapToVelocityContext(paramters), writer, templateStr); } public static String mergeWithStr(Map paramters, StringWriter writer, String templateStr) { return mergeWithStr(parseMapToVelocityContext(paramters), writer, templateStr); } public static String mergeWithStr(VelocityContext context, StringWriter writer, String templateStr) { try { Velocity.evaluate(context, writer, DEFAULT_LOG_TAG, templateStr); } catch (Exception e) { e.printStackTrace(); } return writer.toString(); } public static String mergeWithStr(Map paramters, Reader reader) { StringWriter writer = new StringWriter(); return mergeWithStr(parseMapToVelocityContext(paramters), writer, reader); } public static String mergeWithStr(Map paramters, StringWriter writer, Reader reader) { return mergeWithStr(parseMapToVelocityContext(paramters), writer, reader); } public static String mergeWithStr(VelocityContext context, StringWriter writer, Reader reader) { try { Velocity.evaluate(context, writer, DEFAULT_LOG_TAG, reader); } catch (Exception e) { e.printStackTrace(); } return writer.toString(); } @Deprecated public static String mergeWithStr(Map paramters, InputStream instream) { StringWriter writer = new StringWriter(); return mergeWithStr(parseMapToVelocityContext(paramters), writer, instream); } @Deprecated public static String mergeWithStr(Map paramters, StringWriter writer, InputStream instream) { return mergeWithStr(parseMapToVelocityContext(paramters), writer, instream); } @Deprecated public static String mergeWithStr(VelocityContext context, StringWriter writer, InputStream instream) { try { Velocity.evaluate(context, writer, DEFAULT_LOG_TAG, instream); } catch (Exception e) { e.printStackTrace(); } return writer.toString(); } @SuppressWarnings("unchecked") public static void main(String[] args) { Map paramters = new HashMap(); paramters.put("name", "Pandy"); paramters.put("site", "http://zhuhaironghui.oicp.net"); String str = merge("hello.vm", "com/rh/core/menu/templates/", paramters); System.out.println(str); System.out.println(mergeWithStr(paramters, "Hello $name! Welcome to $site world!")); } } |
模板:com/templates/hello.vm
Hello $name! Welcome to $site world!
调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Java代码 import com.util.VelocityUtils; import java.util.HashMap; import java.util.Map; /** * Created by pandy on 14-8-13. */ public class ToolMain { public static void main(String[] args){ Map<String, Object> context = new HashMap<String, Object>(); context.put("name","Pandy"); context.put("site","www.pandy8.com"); String gridConfigStr = VelocityUtils.merge("com/templates/hello.vm", context); System.out.println(gridConfigStr); } } |
输出:
Hello Pandy! Welcome to www.pandy8.com world!