Reputation: 4213
I need to generate a Java file using Velocity Template concept. Could you please guide through this Please.
The java code should contains some methods, imports and variables...
Thanks, Easwar
Upvotes: 0
Views: 1505
Reputation: 23903
With the velocity / java part, this is more or less you need to do:
// factory and an engine instance
VelocityEngineFactory velocityEngineFactory = new VelocityEngineFactory();
VelocityEngine engine = velocityEngineFactory.createVelocityEngine();
// now you need to give the variables you wanna have access from velocity script
VelocityContext context = new VelocityContext(properties);
ByteArrayOutputStream temp = new ByteArrayOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(temp));
// generate the result, where scriptString is the velocity script
engine.evaluate(context, bufferedWriter, null, scriptString);
bufferedWriter.flush();
textResultant = temp.toString();
So you can create an script, load it and process it programmatically.
Upvotes: 4