velocity介绍一下,这是一个jsp中常用的模板引擎,用于解决bean对象的展示,字符串处理,浏览器内置对象的获取request,rep等;可以自动读取bean,可以自动添加转换规则,可以添加任何对像,可以添加方法。

    官网示例:

    velocity 最全实例.rar

    简单示例:

    1. // 初始化引擎
    2. VelocityEngine vEngine = new VelocityEngine();
    3. vEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
    4. vEngine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
    5. vEngine.init();
    6. //读取模板转换为String
    7. Template beantemplate = vEngine.getTemplate("hello.vm", "UTF-8");//读取bean模板
    8. StringWriter writer = new StringWriter();
    9. VelocityContext context = new VelocityContext();
    10. context.put("key","value");
    11. beantemplate.merge(context , writer);
    12. System.out.println(writer.toString());
    13. //读取String转换为String
    14. StringWriter writer2 = new StringWriter();
    15. VelocityContext context2 = new VelocityContext();
    16. context2.put("key","value");
    17. String string = "key是${key}";
    18. Velocity.evaluate(context2 , writer2 , "mystring", string);