`
yzhong_sa
  • 浏览: 88737 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

velocity 学习 3 其实现原理

阅读更多
import java.io.StringWriter;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;


public class HelloWorld{
public static void main( String[] args )throws Exception{
/* first, get and initialize an engine */
VelocityEngine ve = new VelocityEngine();
ve.init();
/* next, get the Template */
Template t = ve.getTemplate( "hellosite.vm" );
/* create a context and add data */
VelocityContext context = new VelocityContext();
context.put("name", "sun");
context.put("site", "http://i.cn.yahoo.com/littlenewus/blog/p_3/");
/* now render the template into a StringWriter */
StringWriter writer = new StringWriter();
t.merge( context, writer );
/* show the World */
System.out.println( writer.toString() );
}
}




把velocity的包加到classpath 。。然后再写一个hellosite.vm  在里面有$name 和$site 就行了  不管了写什么叼东西。。

上面的代码也是从工作日志直接复制的  也应该能看懂吧  呵呵。。 这是velocity怎么叼模板的过程。

恩 ,当然在实际用的时候这些事情不用我们来做了  我们只需要怎么用velocity里面规定的访问机制就行了。
分享到:
评论
3 楼 czcyx007 2012-12-21  
[u][1/u]
2 楼 czcyx007 2012-12-21  
引用
111
1 楼 czcyx007 2012-12-21  
引用

相关推荐

Global site tag (gtag.js) - Google Analytics