利用Velocity Tools2.0 轻松构建Web工程_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 利用Velocity Tools2.0 轻松构建Web工程

利用Velocity Tools2.0 轻松构建Web工程

 2011/9/6 8:13:01  huangyunzeng  http://huangyunzeng.iteye.com  我要评论(0)
  • 摘要:1、下载VelocityTools2.0,给出下载链接:http://velocity.apache.org/download.cgi2、新建web工程,将velocityTools2.0的jar包添加进工程的lib中。3、修改web.xml文件,修改后的文件是这样的:<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEweb-appPUBLIC"-//SunMicrosystems,Inc.//DTDWebApplication2
  • 标签:Web 利用

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、发布工程并访问,是不是很简单!这里介绍的知识简单的集成方式,还有很多的高级功能,可参考相关文档!

发表评论
用户名: 匿名