velocity介绍一下,这是一个jsp中常用的模板引擎,用于解决bean对象的展示,字符串处理,浏览器内置对象的获取request,rep等;可以自动读取bean,可以自动添加转换规则,可以添加任何对像,可以添加方法。
官网示例:
简单示例:
// 初始化引擎
VelocityEngine vEngine = new VelocityEngine();
vEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
vEngine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
vEngine.init();
//读取模板转换为String
Template beantemplate = vEngine.getTemplate("hello.vm", "UTF-8");//读取bean模板
StringWriter writer = new StringWriter();
VelocityContext context = new VelocityContext();
context.put("key","value");
beantemplate.merge(context , writer);
System.out.println(writer.toString());
//读取String转换为String
StringWriter writer2 = new StringWriter();
VelocityContext context2 = new VelocityContext();
context2.put("key","value");
String string = "key是${key}";
Velocity.evaluate(context2 , writer2 , "mystring", string);