-- Velosity Templates --





Velosity Engine:

-> Velocity is an open source templating tool developed by an international
volunteer community and hosted by the Apache Software Foundation's
Jakarta Project

-> It's free and the source code is available under the Apache Software License,
a business-friendly open source license

-> language - Velocity Template Language (VTL).


-> any Aplication using Velosity requires
-> 1.0 Template
-> 2.0 Corresponding java programme

-------------template------------------------

Hello $name! Welcome to Velosity!

-------------java programme------------------

import java.io.StringWriter;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;

public class HelloWorld{

PSVM throws Exception{

VelocityEngine ve=new VelocityEngine();
ve.init();

Template t=ve.getTemplate("src/com/neo/..../myvelocity/helloworld.vm");

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

StirngWriter writer=new StringWriter();
t.merge(context,writer);

System.out.println(writer.toString());
}
}

----------------------------------------------


-> Why use Velocity templates??

-> this useful in any application area that requires data formatting and presentation.

-> it adapts to meny application areas and application environments (servlet...etc).
-> simple & clear syntax 4 the template desiners.
-> simple programming model 4 developers.
-> develop and maintain easily coz separate the logic and presentation
-> Velocity enables templates to access any public method of data objects in the context.

i.e. Can reuse your existing classes. So, objects you want to use in your templates
don't need to be structured in a certain way, like JavaBeans, or
implement special I/O or lifecycle modes, such as JSP (JavaServer Pages) taglibs.
The only requirement is that methods are public.

-> Therefore it shows MVC architecture. Velocity is used to manipulate and present the data
Not to create data.
-> can be used in
-> Servlet Base Web applications
-> Java and SQL code genarations
-> XML processing and transaction
-> Text processing (such as RTF file genaration)


-> Velocity Template Language
-> references:
-> accessing the object in the context
-> freely mix with the template's non-VTL contents.
-> use "$" to refer context data. if not such data in context, treat as a text and render.
-> Velocity reference allows access to any object's public method

-----------------------------------------------------------------

There are $myBean.getSize() elements.

$myObject.anotherMethod( 1, "more data ")

$foo.getBar().barMethod("hello", $moredata )

$foo.myMethod( $bar.callThis() )

-----------------------------------------------------------------

-> Velocity worke as java bean (i.e. can access getter and setter as properties)
-> $pet.name for only $pet.price and $pet.get('name') for only $pet.get('price') are same.

-> directives:
-> set of statements used for controle and action
-> #set() - used to set a reference value within the template. can replace the existing object
in the context or establish a new one. And can commaon math operations on integers and booleans

#set( $startrow = 0)
#set( $count = $current + 1 )
#set( $isReady = ( $isOn && $isOpen) )


-> #if() , #else , #elseif() , #end

#if( $value > 5 )
bigger
#elseif( $value < 5 )
smaller
#else
just right
#end


-> #foreach() , #end()

#foreach( $item in $itemList )
My $item.
#end


-> #inclde() and #parse()

-> The #include() and #parse() directives are similar.
-> take a template or static resource name as an argument.
-> The difference is that #include() includes the specified resource's content
without any processing, and #parse() treats the specified resource as a template,
processing all directives and references against the current context. (in dynamically)

#include("disclaimer.txt")

#parse("header.vm")


-> #macro()

-> lets the designer create a parameterized bit of VTL code, called a Velocimacro,
that can be invoked like a directive. Similar to a subroutine, this feature lets you
create reusable pieces of VTL that contain explicit parameters for easier usage,
readability, and maintenance. Velocimacro is a rich and powerful tool,
-> Suppose you want to encapsulate the VTL that designers use to make HTML < select gt; widgets.
You would define a Velocimacro in regular VTL code:

#macro( select $name $displayname $choicelist )
<SELECT name=$name>
<option value="">$displayname</option>
#foreach( $choice in $choicelist )
<option value=$choice>$choice</option>
#end
</select>
#end


Then, anytime you need an HTML <select> widget, you could simply invoke
the Velocimacro as a directive:

Please choose: #select( "size" "--SIZE--" $sizelist )


-> other VTL def's

#set( $myint = 5 ) // assign a integer
#set( $mystring = 'Hello There') // assign a string
#set( $mybool = true ) // assign a boolean
#set( $objarr = [ "a", $myint, $mystring, false ] ) // assign arrays of object
#set( $intrangearr = [1..10] ) // assign sequencial integers

#set( $foo = 'bar')
#set( $interp = "The value is '$foo'") // to use variable inside the stirng



-> Same data deferent temolates:

-------------------html---------------------------------

<HTML>
<HEAD>
<TITLE>Pet Store Sale!</TITLE>
</HEAD>
<BODY>
<CENTER>
<B>$petList.size() Pets on Sale!</B>
<BR/>
We are proud to offer these fine pets
at these amazing prices.
<BR/>
This month only, choose from:
#set( $count = 1 )
<TABLE>
#foreach( $pet in $petList )
<TR>
<TD>$count)</TD>
<TD>$pet.name</TD>
<TD>$pet.price</TD>
</TR>
#set( $count = $count + 1 )
#end
</TABLE>
<BR/>
<I>Call Today!</I>
</CENTER>
</BODY>
</HTML>

---------------------xml-----------------------------

<?xml version="1.0"?>
<salelist>
#foreach( $pet in $petList )
<pet>
<name>$pet.name</name>
<price<$pet.price</price>
</pet>
#end
</salelist>

----------------------servlet-------------------------------

import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.velocity.Template;
import org.apache.velocity.context.Context;
import org.apache.velocity.servlet.VelocityServlet;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.exception.ParseErrorException;
public class Sample extends VelocityServlet{

public Template handleRequest( HttpServletRequest request,
HttpServletResponse response, Context context ){

ArrayList list = new ArrayList();
Map map = new HashMap();
map.put("name", "horse");
map.put("price", "00.00");
list.add( map );

map = new HashMap();
map.put("name", "dog");
map.put("price", "9.99");
list.add( map );
map = new HashMap();
map.put("name", "bear");
map.put("price", ".99");
list.add( map );

context.put("petList", list);

Template template = null;

try
{
template = getTemplate("petstoreemail.vm");
}
catch( Exception e )
{
System.out.println("Error " + e);
}
return template;
}
}

------------------------------------------------------

Comments

Popular posts from this blog

-- How to Schedule a Task --

How to Connect DataBase to Java Application -JDBC

-- Are you a Android Developer? --