`
canky
  • 浏览: 2679 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类

Velocity学习笔记(2) - 搭建最简单的学习环境

阅读更多
昨天还不了解velocity,稍微抱怨了下,嘿
经过一番探索之后,发现其实velocity还是很简单,很好入门的哈.

搭建最简单的学习环境

1.jar包是少不了的
Commons-Collections.jar
Commons-Common.jar
Commons-Lang.jar
velocity-1.6.3.jar
velocity-tools-1.4.jar
2.随便建个类:
	public static void main(String[] args) {
		try {
		     Velocity.init();//
                     basicUsage();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}	
	private static void basicUsage() throws IOException {
	
		String template;
		Writer writer;
		VelocityContext context;
		writer= new StringWriter();
		context=new VelocityContext();
		//需要什么参数这里加.
		context.put("toolName", new String("Velocity"));
		
		//访问常量 ${runtime.COUNTER_NAME}.
		context.put("runtime", new FieldMethodizer( "org.apache.velocity.runtime.Runtime" ));
		
		//使用velocity的tools也很简单:
		//不知道为什么velocity的文档非要我们通过servlet来使用它的tools.
		//还搞了个toolbox.xml来迷惑我.
		context.put("date", new org.apache.velocity.tools.generic.DateTool());
		
		//其他class同样可以加到context,只要你有需要.
		
		template="We are using $toolName now.";
		//模板写这里,想怎样写就怎样写,
		//学习而已嘛,不需要使用模板文件,多麻烦呀.
		writer.write("Template : ");
		writer.write(template);
		writer.write("\nOutput   : ");
		
		Velocity.evaluate( context, writer, "logTagName", template );
		System.out.println( writer );
		writer.close();
	}

3.剩下的就是学习vtl的使用了,
  模仿上面的basicUsage()来测试,学习就是了.
4.基本掌握VTL之后,再看下velocity.properties怎样配置,然后就可以正式投产了.
5.struts2已经整合好veleocity,直接用就是了
0
0
分享到:
评论
1 楼 anchao 2011-11-24  
,不懂~!

相关推荐

Global site tag (gtag.js) - Google Analytics