I need a light weigh solution for a template engine..


General J2EE: I need a light weigh solution for a template engine..

  1. I have a requirement for auditing where I have an Object and a String like so:


    "${person.name} is ${person.age} years old."

    Is there a simple lighweight solution to this? I hav ewritten my own in the past using BeanUtils...
  2. Have you looked at Velocity?

  3. Yes, that it a bit heavy for just audit logging I think. Maybe not.
  4. Velocity is probably the lightest one out there that does bean-style translation (templates). It works well as a heavy lifter (e.g.: jsp alternative) but also works well as a simple string formatter:

    VelocityEngine engine = new VelocityEngine();
    VelocityContext context = new VelocityContext();
    context.put("person", person);
        new OutputStreamWriter(System.out),
        "$person.name is $person.age yearend old.");

    Contexts are lightweight enough create as needed (except in situations where you'd think twice about creating a String :-) The engine should be pre-allocated.

    The only part that proves a bit "heavy" for this usage pattern is that it throws unfashionable checked exceptions. A little wrapper classes fixes that in a jiffy.

    Michael Abato