- 论坛徽章:
- 0
|
[代码] example2.vm
Hello from $name in the $project project.
[代码] java代码- import java.io.StringWriter;
- import java.util.Properties;
- import org.apache.velocity.app.Velocity;
- import org.apache.velocity.VelocityContext;
- import org.apache.velocity.exception.ParseErrorException;
- import org.apache.velocity.exception.MethodInvocationException;
- public class Example2
- {
- public static void main( String args[] )
- {
- // first, we init the runtime engine. Defaults are fine.
-
- try
- {
- Velocity.init();
- }
- catch(Exception e)
- {
- System.out.println("Problem initializing Velocity : " + e );
- return;
- }
- // lets make a Context and put data into it
- VelocityContext context = new VelocityContext();
- context.put("name", "Velocity");
- context.put("project", "Jakarta");
-
- // lets render a template
- StringWriter w = new StringWriter();
- try
- {
- Velocity.mergeTemplate("./src/example2.vm", context, w );
- }
- catch (Exception e )
- {
- System.out.println("Problem merging template : " + e );
- }
- System.out.println(" template : " + w );
- String s = "We are using $project $name to render this.";
- w = new StringWriter();
- try
- {
- Velocity.evaluate( context, w, "mystring", s );
- }
- catch( ParseErrorException pee )
- {
- System.out.println("ParseErrorException : " + pee );
- }
- catch( MethodInvocationException mee )
- {
- System.out.println("MethodInvocationException : " + mee );
- }
- catch( Exception e )
- {
- System.out.println("Exception : " + e );
- }
- System.out.println(" string : " + w );
- }
- }
复制代码 |
|