@Test public void test_render() throws Exception { String templateVariable = "Hello Velocity!"; Map<String, Object> model = new HashMap<>(); model.put("message", templateVariable); String expected = String.format("<h1>%s</h1>", templateVariable); String actual = new VelocityTemplateEngine("UTF-8").render(new ModelAndView(model, "hello.vm")); assertEquals(expected, actual); }
@Override public String render(ModelAndView modelAndView) { String templateEncoding = Optional.ofNullable(this.encoding).orElse(StandardCharsets.UTF_8.name()); Template template = velocityEngine.getTemplate(modelAndView.getViewName(), templateEncoding); Object model = modelAndView.getModel(); if (model instanceof Map) { Map<?, ?> modelMap = (Map<?, ?>) model; VelocityContext context = new VelocityContext(modelMap); StringWriter writer = new StringWriter(); template.merge(context, writer); return writer.toString(); } else { throw new IllegalArgumentException("modelAndView must be of type java.util.Map"); } }
VelocityTemplateEngine extends TemplateEngine { @Override public String render(ModelAndView modelAndView) { String templateEncoding = Optional.ofNullable(this.encoding).orElse(StandardCharsets.UTF_8.name()); Template template = velocityEngine.getTemplate(modelAndView.getViewName(), templateEncoding); Object model = modelAndView.getModel(); if (model instanceof Map) { Map<?, ?> modelMap = (Map<?, ?>) model; VelocityContext context = new VelocityContext(modelMap); StringWriter writer = new StringWriter(); template.merge(context, writer); return writer.toString(); } else { throw new IllegalArgumentException("modelAndView must be of type java.util.Map"); } } }
VelocityTemplateEngine extends TemplateEngine { @Override public String render(ModelAndView modelAndView) { String templateEncoding = Optional.ofNullable(this.encoding).orElse(StandardCharsets.UTF_8.name()); Template template = velocityEngine.getTemplate(modelAndView.getViewName(), templateEncoding); Object model = modelAndView.getModel(); if (model instanceof Map) { Map<?, ?> modelMap = (Map<?, ?>) model; VelocityContext context = new VelocityContext(modelMap); StringWriter writer = new StringWriter(); template.merge(context, writer); return writer.toString(); } else { throw new IllegalArgumentException("modelAndView must be of type java.util.Map"); } } VelocityTemplateEngine();  VelocityTemplateEngine(String encoding);  VelocityTemplateEngine(VelocityEngine velocityEngine); }
VelocityTemplateEngine extends TemplateEngine { @Override public String render(ModelAndView modelAndView) { String templateEncoding = Optional.ofNullable(this.encoding).orElse(StandardCharsets.UTF_8.name()); Template template = velocityEngine.getTemplate(modelAndView.getViewName(), templateEncoding); Object model = modelAndView.getModel(); if (model instanceof Map) { Map<?, ?> modelMap = (Map<?, ?>) model; VelocityContext context = new VelocityContext(modelMap); StringWriter writer = new StringWriter(); template.merge(context, writer); return writer.toString(); } else { throw new IllegalArgumentException("modelAndView must be of type java.util.Map"); } } VelocityTemplateEngine();  VelocityTemplateEngine(String encoding);  VelocityTemplateEngine(VelocityEngine velocityEngine); @Override String render(ModelAndView modelAndView); }
VelocityTemplateEngine extends TemplateEngine { @Override public String render(ModelAndView modelAndView) { String templateEncoding = Optional.ofNullable(this.encoding).orElse(StandardCharsets.UTF_8.name()); Template template = velocityEngine.getTemplate(modelAndView.getViewName(), templateEncoding); Object model = modelAndView.getModel(); if (model instanceof Map) { Map<?, ?> modelMap = (Map<?, ?>) model; VelocityContext context = new VelocityContext(modelMap); StringWriter writer = new StringWriter(); template.merge(context, writer); return writer.toString(); } else { throw new IllegalArgumentException("modelAndView must be of type java.util.Map"); } } VelocityTemplateEngine();  VelocityTemplateEngine(String encoding);  VelocityTemplateEngine(VelocityEngine velocityEngine); @Override String render(ModelAndView modelAndView);  }