VELOCITY TUTORIAL _1
TEMPLATE
ENGINE :
It
is program or software which takes text files as input and process
then and produces one or more output files [O/p
files has content different from input text file or very few times
same as input text file]
VELOCITY
:
- Velocity is open-source template engine written in java-language .
USES
OF VELOCITY:
- It is used for presenting a view to the user as like JSP.
- It is used to create any kind of pages like HTML , XML and so on.
- It can be used in standlone application to create XML files dynamically.
- We can make the whole-text file as string which is used for generating e-mail.
- It is easy to understand compared to JSP pages which uses Scriptlets, Tags .. so non-java developers can easily understand Velocity so Presentation layer and business layer is separated.
VELOCITY
VS JSP :
- Velocity is faster compared to JSP [JSP involves conversion to servlet then compile and run].
- Easy to learn.
- No need for any web-container or server to run the velocity
- No expection occurs in velocity while in JSP we have Compile and runtime error.HELLO WORLD PROGRAM :
REQUIRED
PACKAGES :
|
|
Steps
involved in creating and running a velocity program.
- STEP 1: Intialize Velocity-Engine[NOTE: In version 1.1 we directly intialize velocity engine by calling “Velocity.init()” method but in 1.2 we can create VelocityEngine object and then intialize the velocity Engine as “VelocityEngine velocityEngine = new VelocityEngine(); velocityEngine.init();”]
- STEP 2: Create the Template object by calling getTemplate() method.[NOTE : In version 1.1 we use Velocity class to create template object as “Template template = Velocity.getTemplate("./src/directives.vm");” but in 1.2 version we can also use VelocityEngine object to create a template object as “VelocityEngine velocityEngine = new VelocityEngine(); velocityEngine.init();Template template = velocityEngine.getTemplate(/*Give the file location here*/"helloworld.txt");”]
- STEP 3: Create VelocityContext object and add values.
- STEP 4 : Process the template.
PROGRAMMING
:
JAVA-FILE:
import
java.io.StringWriter;
import
org.apache.velocity.Template;
import
org.apache.velocity.VelocityContext;
import
org.apache.velocity.app.VelocityEngine;
public
class
HelloWorld {
public
static
void
main(String[] args) throws
Exception {
/*STEP
1 :Create a VelocityTemplate Engine Object*/
VelocityEngine
velocityEngine = new
VelocityEngine();
/*STEP
2 :Initialise VelocityTemplate Engine */
velocityEngine.init();
/*
* NOTE : we can directly initialise the velocity engine by
calling "Velocity.init()"
* Creation of VelocityEngine object is available from 1.2 version
only
* */
/*STEP
3: Create the template object by calling getTemplate() method in
VE(VelocityEngine)*/
Template
template = velocityEngine.getTemplate(/*Give
the file location here*/"./src/helloworld.txt");
/*STEP
4: Create VelocityContext */
VelocityContext
context = new
VelocityContext();
StringWriter
writer = new
StringWriter();
/*STEP
5: Process the template and write the o/p to stream*/
template.merge(context,
writer);
/*STEP
6: Show the output */
System.out.println(writer);
}
}
|
helloworld.txt-FILE
Hello
java! Welcome to Velocity!
Hello
World
NOTE
: This text file is called as velocity template file
|
OUTPUT
Hello
java! Welcome to Velocity!
Hello
World
|
Note : If
you find any thing which you can not understand or wrong please
leave comments.I assure i will get back to you
NOTE : TO download the velocity tutorial in word format click here
What do you do so that instead of plain old console output, you'd be able to create a web-app screen? I've seen many tutorials but most of the classes they use are deprecated.
ReplyDeleteHi i am a total begineer to apache velocity....can you tel me the essentials..like creating a velocityservlet,vm template
ReplyDeleteme too....i also want to learn from basics
DeleteThank you so much for the tutorial vignesh
ReplyDeletevery informative article
ReplyDeletethanks for sharing!!!
dharmendra
plz tell me how to run velociry application in eclipse.. and how to test the already available application
ReplyDeleteThis comment has been removed by the author.
ReplyDelete//To read the file
ReplyDeleteve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
Use the above code to read the file, else it will show unable to load the resource error. Add it after velocity engine.
plz tell me how to display image in velocity , i have created one template to send mail which contain an image which is fixed, but when i receive mail it is not displaying the image .
ReplyDeleteplz reply as soon as possible
plz tell me how to display image in velocity , i have created one template to send mail which contain an image which is fixed, but when i receive mail it is not displaying the image .
ReplyDeleteplz reply as soon as possible bgvinay6@gmail.com
plz tell me how to display image in velocity , i have created one template to send mail which contain an image which is fixed, but when i receive mail it is not displaying the image .
ReplyDeleteplz reply as soon as possible bgvinay6@gmail.com
plz tell me how to display image in velocity , i have created one template to send mail which contain an image which is fixed, but when i receive mail it is not displaying the image .
ReplyDeleteplz reply as soon as possible
plz we want from basics bro....Anyway Thanks
ReplyDelete