1 16 17 import java.io.StringWriter ; 18 import java.util.Properties ; 19 import org.apache.velocity.app.Velocity; 20 import org.apache.velocity.VelocityContext; 21 22 import org.apache.velocity.exception.ParseErrorException; 23 import org.apache.velocity.exception.MethodInvocationException; 24 25 36 37 public class Example2 38 { 39 public static void main( String args[] ) 40 { 41 42 43 try 44 { 45 Velocity.init(); 46 } 47 catch(Exception e) 48 { 49 System.out.println("Problem initializing Velocity : " + e ); 50 return; 51 } 52 53 54 55 VelocityContext context = new VelocityContext(); 56 57 context.put("name", "Velocity"); 58 context.put("project", "Jakarta"); 59 60 61 62 StringWriter w = new StringWriter (); 63 64 try 65 { 66 Velocity.mergeTemplate("example2.vm", context, w ); 67 } 68 catch (Exception e ) 69 { 70 System.out.println("Problem merging template : " + e ); 71 } 72 73 System.out.println(" template : " + w ); 74 75 79 80 String s = "We are using $project $name to render this."; 81 w = new StringWriter (); 82 83 try 84 { 85 Velocity.evaluate( context, w, "mystring", s ); 86 } 87 catch( ParseErrorException pee ) 88 { 89 93 System.out.println("ParseErrorException : " + pee ); 94 } 95 catch( MethodInvocationException mee ) 96 { 97 104 System.out.println("MethodInvocationException : " + mee ); 105 } 106 catch( Exception e ) 107 { 108 System.out.println("Exception : " + e ); 109 } 110 111 System.out.println(" string : " + w ); 112 } 113 } 114
| Popular Tags
|