Velocity 使用介绍-在java中的代码

1.首先初始化对象
       
VelocityContext context = new VelocityContext();<o:p></o:p>

StringWriter writer = new StringWriter();<o:p></o:p>

String encoding2 = "GBK";<o:p></o:p>

<o:p> </o:p>

2PROPERTY_PATH  系统properties文件中的TEMPLATE_INIT_PATH (指定velocity所需的property文件路径。)<o:p></o:p>

<o:p> </o:p>

3Properties中的内容<o:p></o:p>

file.resource.loader.path = D:/resin/webapps/mip/files/templateupload/(模板所在地方)<o:p></o:p>

<o:p> </o:p>

4.然后要初始化Velocity.init(PROPERTY_PATH);<o:p></o:p>

<o:p> </o:p>

5.将velocity中的标签跟java对应context.put(key, value);<o:p></o:p>

<o:p> </o:p>

6.然后加载文件Velocity.mergeTemplate(templateName, encoding2, context, writer);<o:p></o:p>

<o:p> </o:p>

7.最后调用Generator.writeToFile()生成文件。<o:p></o:p>

<o:p> </o:p>

8writeToFile 函数如下:<o:p></o:p>

FileOutputStream of = new FileOutputStream(theFile);<o:p></o:p>

         of.write(writer.toString().getBytes("GBK"));<o:p></o:p>

         // String tt = writer.toString();<o:p></o:p>

         of.flush();<o:p></o:p>

         of.close();

你可能感兴趣的:(java,velocity)