Velocity is a Java based template engine.It permits anyone to use a simple yet powerful template language to reference objects defined in Java code.
An alternative to JSP and PHP, and few others. Velocity framework can be used in following areas:- Web apps, dynamic HTML pages are created and processed with VelocityViewServlet or number of frameworks that support Velocity - Source code generation- Automatic e-mails- XML transformation, provides an ant task called Anakia which reads an XML file and makes it available to a Velocity template. What are the features of Velocity?Velocity framework has following features:- Velocity is MVC based- It segregates html template code from Java code- It emphasizes on role based web app development - Velocity offers better maintainable web applications.
Here is a little description of how it could be used:
Problem(2 parts):
1. I have a block of text inside a String. I need to be able to apply certain substituions and insertions based on pattern matches. Example, blah blah blah CPB blah blah - needs to be blah blah blah <color=red>CPB</color> blah blah
2. The substituion/insertion data needs to be stored externally so it can be changed.
Is velocity a good match for this, or should I just store Pattern strings in a database table?
Solutions Exploration:
Velocity is easy to use. You simply use template markers in the prototype text. For example:
blah blah blah $CPB blah blah
You populate the VelocityContext (essentially a dictionary) with replacement values. In your case, these would come from a config file.
Velocity is more interesting when you use it with objects. For example, at the key 'colorUtil' you might put an object (of your design) which builds color tags. Your template might then look something like:
blah blah blah $colorUtil.red($CPB) blah blah
-----------------------------------------------
For literal text, this should work:
blah blah blah $colorUtil.red('SUBJECT') blah blah
to Velocity or not to Velocity
I have used velocity extensively when I worked on an adapter's FCIF work. But, that's about it lol. Everything was straight pojo field mapping to same named property in the template with a dollar sign in front. The colorUtil looks way cool, but it still operates on the premise that CPB is a property not literal text, correct?
No comments:
Post a Comment