Velocity 搭建

网上关于Velocity的资料确实挺多的.我看了好多,就是让velocity跑不起来.感觉就怪了.于是我回头看了下velocity自带的例子.simple. 是war包.于是部署了下,还真跑起来了.
 
下面把velocity简单说下,希望能节省摸索velocity的时间.
 
我是在Eclipse3.2+myEclipse 下部署的.
 
首先 建一个web 项目. 名字 velocityTest.
 
然后建类,是为了测试elocity如何取类属性值.
 
 
 
 

package com.test;


/*
 * 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.
 */


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();
    }

}

 

二 , 修改web.xml 文件

 

 

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
>
    <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>
  <welcome-file-list>
    <welcome-file>index.vm</welcome-file>
  </welcome-file-list>
</web-app>

 

三, 创建toolbox.xml 文件, 这个文件相当重要.千万不要忘记了.*.vm 文件里面的动态变量就在此文件里面配置.

 

 

 

 

 

 

<?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.
-->

<toolbox>
  <xhtml>true</xhtml>
  <tool>
     <key>toytool</key>
     <scope>request</scope>
     <request-path>index.vm</request-path>
     <class>com.test.ToyTool</class>
  </tool>
  <data type="number">
    <key>version</key>
    <value>1.1</value>
  </data>
  <data type="boolean">
    <key>isSimple</key>
    <value>true</value>
  </data>
  <data type="string">
    <key>foo</key>
    <value>this is foo.</value>
  </data>
  <data type="string">
    <key>bar</key>
    <value>this is bar.</value>
  </data>
  <data type="string">
    <key>name</key>
    <value>jim.</value>
  </data>
  <tool>
    <key>map</key>
    <scope>session</scope>
    <class>java.util.HashMap</class>
  </tool>
  <tool>
    <key>date</key>
    <scope>application</scope>
    <class>org.apache.velocity.tools.generic.DateTool</class>
  </tool>
</toolbox>

 

然后就在web项目的根目录下创建index.vm文件.

 

 

 

<html>
<body>
hello...


I'm a velocity template.

#if($XHTML )
  #set( $br = "<br />" )
#else
  #set( $br = "<br>" )
#end


$br
$br


name is: $name

$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

</body>
</html>

 

 

然后,重启tomcat. 在浏览器里输入 http://localhost:8008/velocityTest/

就可以看到你想要的结果了.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值