VelocityParseFactory { public static String parse(final String templateName, final String body, final Map<String, ?> params) throws UtilException { VelocityContext context = new VelocityContext(); if (MapUtils.isNotEmpty(params)) { for (Entry<String, ?> entry : params.entrySet()) { context.put(entry.getKey(), entry.getValue()); } } StringResourceRepository repository = StringResourceLoader.getRepository(); repository.putStringResource(templateName, body); ByteArrayOutputStream out = null; try { Template template = Velocity.getTemplate(templateName, GlobalConstants.DEFAULT_CHARSET); out = new ByteArrayOutputStream(); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, GlobalConstants.DEFAULT_CHARSET)); template.merge(context, writer); writer.flush(); return new String(out.toByteArray(), GlobalConstants.DEFAULT_CHARSET); } catch (Exception e) { throw new UtilException(ErrorCodeDef.PARSE_TEPLATE_ERROR_10004, e); } finally { if (out != null) { try { out.close(); } catch (IOException e) { throw new UtilException(ErrorCodeDef.PARSE_TEPLATE_ERROR_10004, e); } } } }  static String parse(final String templateName, final String body, final Map<String, ?> params);  }
@Test public void parse() { Bean bean = new Bean("张三", NUM_18); Map<String, Object> params = new HashMap<>(); params.put("b", bean); String template = "你好，我的名字叫${b.name}"; String str = VelocityParseFactory.parse("template01", template, params); Assert.equals(str, "你好，我的名字叫张三", ErrorCodeDef.SYSTEM_ERROR_10001); }