Code.GeekInterview.com
  I am new, Sign me up!
 
Code Samples Java
 

Velocity Framework Application


Code ResourceAuthor: srinivasaraobora  

Difficulty Level: Expert

Published: 25th Apr 2009   Read: 1857 times  

Filed in: Java
Add Comment


 


This code show how to use velocity frame work.in velocity frame work we need .vm file which taken by velocity engine.

 


Sample Code
  1. Employee.java
  2. import java.io.StringWriter;
  3.  
  4. import java.util.List;
  5. import java.util.ArrayList;
  6. import java.util.Map;
  7. import java.util.HashMap;
  8.  
  9. import org.apache.velocity.Template;
  10. import org.apache.velocity.VelocityContext;
  11. import org.apache.velocity.app.VelocityEngine;
  12.  
  13. public class Employee
  14. {
  15.     public static void main( String[] args )
  16.         throws Exception
  17.     {
  18.         /*
  19.          *   first, get and initialize an engine
  20.          */
  21.  
  22.         VelocityEngine ve = new VelocityEngine();
  23.         ve.init();
  24.  
  25.         /*
  26.          *   organize our data
  27.          */
  28.  
  29.         ArrayList list = new ArrayList();
  30.         Map map = new HashMap();
  31.  
  32.         map.put("name", "srinivas");
  33.         map.put("sal", "$10000.00");
  34.         list.add( map );
  35.  
  36.         map = new HashMap();
  37.         map.put("name", "umamahesh");
  38.         map.put("sal", "$59000.99");
  39.         list.add( map );
  40.  
  41.         map = new HashMap();
  42.         map.put("name", "ravi kumar");
  43.         map.put("sal", "$3000.99");
  44.         list.add( map );
  45.  
  46.         map = new HashMap();
  47.         map.put("name", "anil kumar");
  48.         map.put("sal", "$30000.99");
  49.         list.add( map );
  50.  
  51.         /*
  52.          *  add that list to a VelocityContext
  53.          */
  54.  
  55.         VelocityContext context = new VelocityContext();
  56.         context.put("empList", list);
  57.  
  58.         /*
  59.          *   get the Template  
  60.          */
  61.  
  62.         Template t = ve.getTemplate( "employee_html.vm" );
  63.  
  64.         /*
  65.          *  now render the template into a Writer, here
  66.          *  a StringWriter
  67.          */
  68.  
  69.         StringWriter writer = new StringWriter();
  70.  
  71.         t.merge( context, writer );
  72.  
  73.         /*
  74.          *  use the output in the body of your emails
  75.          */
  76.  
  77.         System.out.println( writer.toString() );
  78.     }
  79. }
  80.  
  81.  
  82.  
  83. employee_html.vm
  84.  
  85.  
  86.   <HTML>
  87.     <HEAD>
  88.       <TITLE>Employee Details</TITLE>
  89.     </HEAD>
  90.  
  91.  
  92.     <BODY>
  93.       <CENTER>
  94.       <B>$empList.size() Employees Details</B>
  95.      
  96.       <BR/>
  97.       We are proud to offer these fine pets
  98.       at these amazing prices.  
  99.       <BR/>
  100.       This month only, choose from :
  101.    
  102.       #set( $count = 1 )  
  103.       <TABLE>
  104.         #foreach( $emp in $empList )
  105.           <TR>
  106.             <TD>$count)</TD>
  107.             <TD>$emp.name</TD>
  108.             <TD>$emp.sal</TD>
  109.           </TR>
  110.           #set( $count = $count + 1 )
  111.         #end
  112.       </TABLE>
  113.      
  114.      <I>Visit Again</I>
  115.  
  116.       </CENTER>
  117.  
  118.     </BODY>
  119.   </HTML>
  120.  
  121.  
  122. Set the class path for velocity-dep-1.2.jar and then run the application.
Copyright GeekInterview.com


Next Article: Code For Reading Excel file data


 

Latest Code Samples

 

Popular Code Samples

 

Related Code Samples

 

Post Comment


Members Please Login

Name:  Email: (Optional. Used for Notification)

Title:
 
Comment:
Validation Code: <=>  (Enter this code in text box)





Popular Coders

# Coder NameHits
1. iamdvr34756
2. venki_madesh30775
3. srinivasaraobora21280
4. bora_srinivasarao11323
5. Kiran.jakkaraju4477
6. parmod kumar duhan4316
7. chowsys3900
8. Venkateswara Rao3628
9. Raju3175
10. Vamshidhar Matam2232

Active Coders

Refined Tags

 

Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact  

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape