`
ilusm
  • 浏览: 106492 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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/

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

分享到:
评论
1 楼 chenhailong 2012-01-07  
服,官网例子

相关推荐

Global site tag (gtag.js) - Google Analytics