2012.01.07 大连 晴
这些天真是很忙,也没时间写文章了,放假了,可以细细的品味一些最近的收获和感想,还有很多。。。
慢慢的到了2012,这个注定不平凡的一年,大家都有自己的事业和发展,都有自己对这一年的期望,希望能梦想成真,发展顺利
废话就不多说了,马上开始我们的说明
Velocity 是jsp等的模版语言,真是强大,但都是写好vm文件,让后解析的,如果是要在jsp等中应用,需要其他的扩展tool,Velocity官网也提供下载
我这里也会说明,让我们慢慢道来
1.官网:
http://velocity.apache.org/engine/devel/index.html
2.下载Velocity
现在的版本是:1.7
http://velocity.apache.org/download.cgi
里面包含了2个jar
1.velocity-1.7.jar
2.velocity-1.7-dep.jar
分别说明一下:
1.velocity-1.7.jar基本jar包,里面包含了开发需要的一切包
2.velocity-1.7-dep.jar包含了上面的包,但还包含了、其他的比如:oro.*,commons-lang.*,org.apache.log.*等
我用的是第一个,我不喜欢别人给封装好的东西
Velocity plugin for the Eclipse
Velocity - http://veloeclipse.googlecode.com/svn/trunk/update/
下面我们可以写一个main函数调用vm了
jar:
avalon-logkit-2.1.jar
commons-collections-3.2.1.jar
commons-lang-2.4.jar
log4j-1.2.12.jar
velocity-1.7.jar
package com.chenhailong.velocity;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Date;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
/**
*
* @author chenhailong
* pm 12:02:38
* com.chenhailong.velocity
*/
public class HelloWorld {
public static void main(String[] args) throws IOException {
VelocityEngine ve = new VelocityEngine();
ve.init();
Template template = ve.getTemplate("hello.vm");
VelocityContext context = new VelocityContext();
context.put("name", "chenhailong");
context.put("date", new Date().toString());
StringWriter writer = new StringWriter();
template.merge(context, writer);
System.out.println(writer.toString());
writer.close();
}
}
Welcome $name to Javayou.com!
today is $date.
好了 这样我们就能运行了
其他例子
package com.chen.example;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.log.Log4JLogChute;
public class Example {
public static String LOGGER_NAME = "velexample";
public static VelocityEngine ve = null;
public Example(String templateFile) {
try {
ve = new VelocityEngine();
//ve.init("bin/velocity.properties");
ve.init();
VelocityContext context = new VelocityContext();
context.put("list", getNames());
Template template = null;
try {
template = Velocity.getTemplate(templateFile);
} catch (ResourceNotFoundException rnfe) {
System.out.println("Example : error : cannot find template " + templateFile);
} catch (ParseErrorException pee) {
System.out.println("Example : Syntax error in template " + templateFile + ":" + pee);
}
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
if (template != null)
template.merge(context, writer);
writer.flush();
writer.close();
} catch (Exception e) {
System.out.println(e);
}
}
public ArrayList getNames() {
ArrayList list = new ArrayList();
list.add("ArrayList element 1");
list.add("ArrayList element 2");
list.add("ArrayList element 3");
list.add("ArrayList element 4");
return list;
}
public static void main(String[] args) {
BasicConfigurator.configure();
Logger log = Logger.getLogger(LOGGER_NAME);
log.info("Hello from Log4jLoggerExample - ready to start velocity");
Example t = new Example("example.vm");
ve.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, "org.apache.velocity.runtime.log.Log4JLogChute");
ve.setProperty(Log4JLogChute.RUNTIME_LOG_LOG4J_LOGGER, LOGGER_NAME);
log.info("this should follow the initialization output from velocity");
}
}
#set( $this = "Velocity")
$this is great!
#foreach( $name in $list )
$name is great!
#end
#set( $condition = true)
#if ($condition)
The condition is true!
#else
The condition is false!
#end
runtime.log = velocity_example.log
基本的velocity的语法和常用的都在上面了,希望对大家有用
这里我介绍一下velocity tool 的应用
举一个小例子
<?xml version="1.0" ?>
<test>
<chen a="1">
chenhailong
</chen>
</test>
commons-beanutils-1.7.0.jar
commons-collections-3.2.jar
commons-digester-1.8.jar
commons-lang-2.2.jar
commons-logging-1.1.jar
dom4j-1.1.jar
oro-2.0.8.jar
velocity-tools-view-2.0.jar
velocity-1.6.2.jar
they are togetder
tools.xml
<?xml version="1.0"?>
<tools>
<data key="author">chenhailong</data>
<data key="enter">chenh</data>
<toolbox scope="application">
<tool class="org.apache.velocity.tools.generic.XmlTool" key="test"
file="test.xml" />
</toolbox>
</tools>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd"> <web-app> <servlet> <servlet-name>velocity</servlet-name> <servlet-class>org.apache.velocity.tools.view.VelocityLayoutServlet</servlet-class> <init-param> <param-name>org.apache.velocity.tools.deprecationSupportMode</param-name> <param-value>false</param-value> </init-param> <init-param> <param-name>org.apache.velocity.tools.cleanConfiguration</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>org.apache.velocity.tools.userCanOverwriteTools</param-name> <param-value>false</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>velocity</servlet-name> <url-pattern>*.vm</url-pattern> </servlet-mapping> </web-app>
xml.vm
$test.chen.text
$test.find('@a')
$test.chen.a
$test.children()
$test.first+$test.last
$enter
$author
其他工具基本都是一样的。。。
有什么问题发站内信