Wednesday, 12 December 2012

Velocity Template Tutorial_4



FORMAL REFERENCE NOTATION :
  • To understand formal-reference notation we need to understand the problem first.
PROBLEM:
In JAVA-CODE:

VelocityContext context = new VelocityContext();
context.put("name","vignesh");

IN TEMPLATE-FILE:
 
I am $nameWaran

When the above code is processed by velocity engine it thinks we are refrering to “$nameWaran” not “$name waran” so inorder to overcome this problem use formal-reference notation as

I am ${name}Waran

ESCAPE CHARACTER :(\)

USAGE 1:
#set($name=”vignesh”)
$name
\$name ## using ESCAPE CHARACTER
\\$name

OUTPUT
vignesh
$name
\vignesh
USAGE 2:
#set($name =”vignesh”)
#set($escapeName =”$name”)
#set($dontEscapeName =$name)
$name
$escapeName
$dontEscapeName
OUTPUT
vignesh
$name
vignesh

VELOCITY-CONTEXT CHAINING:
  • Used to mearge to velocity context into one.
  • Can chain any number of Context.
  • EXAMPLE :  
LINE PROGRAMMING EXPLANATION

1
2
3
4

5


6
7

8
VelocityContext context1 = new VelocityContext();
context1.put("name","Velocity");
context1.put("project", "Jakarta");
context1.put("duplicate", "I am in context1");

VelocityContext context2 = new VelocityContext( context1 );

 
context2.put("lang", "Java" );
context2.put("duplicate", "I am in context2");

template.merge( context2, writer )
Line 1:Create-context_1 object.
Line 2&3&4 : Adding values to context.

LINE 5 :Creating context_2 and add context_1 to context_2[Chaining the context]

LINE 6&7 : Adding values to context.

Line 8:Merging

POINTS TO REMEMBER :
  • If want to use replacement for JSP then we can use cache.
  • We can create event handlers like if any exception occurs like NULLPOINTEREXCEPTION we can use Event handlers to handlle that.
  • Using Properties files to giving extra info to velocity engine like using UTF-8 encoding
  • See Documentation for Full Details.

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

2 comments:

  1. Thank You, With the help of your tutorials I learnt the Velocity basics very quickly....

    ReplyDelete
  2. thank you.. its very is to understand the basics

    ReplyDelete