1、下载Velocity Tools2.0,给出下载链接:http://velocity.apache.org/download.cgi
2、新建web工程,将velocityTools2.0的jar包添加进工程的lib中。
3、修改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">
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<web-app>
<servlet>
<servlet-name>velocity</servlet-name>
<servlet-class>org.apache.velocity.tools.view.VelocityViewServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>velocity</servlet-name>
<url-pattern>*.vm</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.vm</welcome-file>
</welcome-file-list>
</web-app>
4、新建一个java包com.stone.velocity。
5、新建一个javaBean ,类似这样的:
package com.stone.velocity;
public class ToyTool
{
private String message = "Hello from ToyTool!";
public String getMessage()
{
return message;
}
public void setMessage(String m)
{
message = m;
}
/** To test exception handling in templates. */
public boolean whine() {
throw new IllegalArgumentException();
}
}
6、编写tools.xml。
<?xml version="1.0"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<tools>
<data type="boolean" key="xhtml" value="true"/>
<data type="boolean" key="isSimple" value="true"/>
<data type="number" key="version" value="2.0"/>
<data key="foo">this is foo</data>
<data key="bar">this is bar.</data>
<toolbox scope="request">
<tool key="toytool" class="com.stone.velocity.ToyTool" restrictTo="index*"/>
</toolbox>
<toolbox scope="session">
<tool key="map" class="java.util.HashMap"/>
</toolbox>
</tools>
7、创建index.vm和index.jsp,注意这两个文件放在WEBROOT下,如果放在别的地方,注意修改web.xml中的相关设置。
8、index.vm
<html>
<body>
I'm a velocity template processed using the VelocityViewServlet.
#if( $XHTML )
#set( $br = "<br />" )
#else
#set( $br = "<br>" )
#end
$br
$br
Here we use a custom tool: $toytool.message
$br
$br
Lets count : #foreach($i in [1..5])$i #end
$br
$br
Let's play with a hashmap:$br
first add foo: $map.put("foo",$foo)$br
then add bar: $map.put("bar",$bar)$br
$br
and that gives us $map
$br
$br
Here we get the date from the DateTool: $date.medium
$br
$br
#if( $isSimple )
This is simple#if( $XHTML ) xhtml#end app version ${version}.
#end
$br
$br
Click <a href="index.jsp">here</a> to see the VelocityViewTag handle the same VTL markup.
</body>
</html>
9、index.jsp.
<html>
<body>
I'm a JSP file that uses the VelocityViewTag.
<velocity:view>
#if( $XHTML )
#set( $br = "<br />" )
#else
#set( $br = "<br>" )
#end
$br
$br
Here we use a custom tool: $toytool.message
$br
$br
Lets count : #foreach($i in [1..5])$i #end
$br
$br
Let's play with a hashmap:$br
first add foo: $map.put("foo",$foo)$br
then add bar: $map.put("bar",$bar)$br
$br
and that gives us $map
$br
$br
Here we get the date from the DateTool: $date.medium
$br
$br
#if( $isSimple )
This is simple#if( $XHTML ) xhtml#end app version ${version}.
#end
$br
$br
Click <a href="index.vm">here</a> to see this VTL markup as a normal template.
</velocity:view>
</body>
</html>
10、发布工程并访问,是不是很简单!这里介绍的知识简单的集成方式,还有很多的高级功能,可参考相关文档!
分享到:
相关推荐
velocity-tools-2.0.jar velocity-tools-2.0.jar velocity-tools-2.0.jar velocity-tools-2.0.jar velocity-tools-2.0.jar
velocity-tools2.0 velocity
velocity-tools.jar对应的源代码
velocity-tools-view-2.0.jar
velocity-tools-view-2.0-beta1.jar, 供大家使用
velocity-tools-generic-2.0.jar
我在用jetspeed做portal时用到了portals-bridges-common-2.0-source-release.zip,portals-bridges-velocity-2.0-source-release.zip两个资源包,传上来,和大家共享。
赠送jar包:velocity-tools-generic-3.1.jar; 赠送原API文档:velocity-tools-generic-3.1-javadoc.jar; 赠送源代码:velocity-tools-generic-3.1-sources.jar; 赠送Maven依赖信息文件:velocity-tools-generic-...
赠送jar包:velocity-tools-generic-3.1.jar 赠送原API文档:velocity-tools-generic-3.1-javadoc.jar 赠送源代码:velocity-tools-generic-3.1-sources.jar 包含翻译后的API文档:velocity-tools-generic-3.1-...
velocity 2.0 包需要的 jar 包 官方下载的,测试过了,可以运行 Demo 网上都有,关键几行代码: // ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "file"); // 按文件加载 // ve.init(); // Template t = ...
velocity-tools-1.4.jar
velocity-tools-2.0.jar 2.web.xml配置文件 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=...
velocity-tools-view-1.4.jar
梯形、余弦、多项式、7段S型、7段修正S型、15段S型、31段S型速度规划演示软件。采用统一架构实现了以上7种速度规划算法,所有核心算法均用c语言实现,不调用第三方算法库,采用QT实现图形用户界面。
本课程从velocity engine也就是velocity引擎开始, 先讲解velocity的基本使用以及基础语法 , 然后再讲解velocity 的进阶内容velocity Tools , 以及velocity作为web项目的视图改如何使用 , 每一部分都会有一个综合案例...
Velocity+Spring 2[1].0+Hibernate的Web应用开发框架,讲解如何使用velocity生成spring+hibernate的框架代码。
velocity-tools-view-1.1.jar
velocity核心jar包 包含: velocity-1.5.jar velocity-1.6.2.jar velocity-dep-1.4.jar velocity-tools-2.0.jar velocity-tools-generic-2.0.jar velocity-tools-view-2.0.jar
velocity-tools-view-1.2.jar