这一篇文章我将讲述关于velocity内容,首先还是从最简单的HelloWorld做起。
第一步:建立web工程命名为velocitytest
第二步:导入velocity需要的jar包这里我用的是velocity-1.7版本的,提供velocity jar包的下载
地址:http://velocity.apache.org/download.cgi 需要下载velocity-tools-2.0.zip 和 velocity-1.7.zip,
下面的包缺一不可的
commons-collections-3.2.jar
commons-digester-1.8.jar
commons-beanutils-1.7.0.jar
commons-lang-2.2.jar
commons-logging-1.1.jar
velocity-1.7.jar
velocity-tools-2.0.jar
velocity-tools-view-2.0.jar
第三步:在项目中的web.xml文件中加入如下代码:
<servlet>
<servlet-name>velocity</servlet-name>
<servlet-class>org.apache.velocity.tools.view.servlet.VelocityViewServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>velocity</servlet-name>
<url-pattern>*.vm</url-pattern>
</servlet-mapping>
第四 步 :在项目的WebRoot下创建一个以.vm(velocity的页面都是以vm结尾的)结尾的index.vm文件,代码如下
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>velocity Hello world</title>
</head>
<body>
##这就是velocity的语法
#set($a="hello world!")
#*
这是一个velocity hello world 的多行注释
*#
## 这是一个velocity 的hello world 的多行注释
velocity $a <br>
</body>
</html>
第五 步 :启动服务器,通过访问您本地的如下地址来查看效果: http://localhost/velocity/ index.vm