欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

在独立的应用程序中使用 Velocity 模板引擎,velocity模板,example2.vmH

来源: javaer 分享于  点击 26743 次 点评:283

在独立的应用程序中使用 Velocity 模板引擎,velocity模板,example2.vmH


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 );    }}
相关栏目:

用户点评