When Using Velocity in an application or servlet, you need to do the following:
- Initialize Velocity. Velocity provides two usage modes: single instance mode and single instance mode. The initialization process is only performed once.
- Create context object
- Add data objects to the context
- Select Template
- 'Merge' template and execute data output
In the code, by using the org. apache. velocity. app. Velocity class single instance mode
VelocityContext context = context. put ("name", String ("Velocity" Template template = Velocity. getTemplate ("mytemplate. vm "StringWriter sw = template. merge (context, sw );
This is a basic form, which is very simple, right? This is what you do when using Velocity to present a template. You may not need to write code like this-we provide some tools to make it easier to use. In any case, no matter how Velocity is used, the above execution sequence will actually happen in the background.