网上关于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/
就可以看到你想要的结果了.
分享到:
相关推荐
分享一个spring+mybatis+velocity项目demo,该项目是之前给一个学第学习用的,主要基于springMVC、mybatis、velocity搭建的,使用maven构建,其中zai service层编写了两个简单组件,一个是email发送,一个是认证授权...
maven+springMVC+mybatis+velocity+mysql+junit项目框架搭建
使用springmvc,mybatis,velocity搭建的最小实例,velocity和jsp作用类似,用来展示数据比较方便
一个使用maven搭建spring mvc的例子(velocity+mybatis)
velocity语法使用文档。教你10分钟学会使用velocity。注意,不包含搭建velocity环境的说明。
velocity语法,Velocity+Java开发指南中文版,struts2 与velocity1.6及velocity tools1.4的整合,Velocity+Spring+Ibatis框架搭建说明文档,Velocity详解(初学者建议看)
这是我写的一个Spring + Spring MVC + MyBatis + Velocity + MySQL 框架搭建完整项目,里面包含了所需的全部JAR包已经相关源码,感兴趣的同学可以下载喽。。。 配合我的博客更好哦!
springboot maven 搭建 springboot1.5以上因不支持velocity 问题的核心在于高版本的springboot已经删除了velocity支持的类 PS: 网上解决方法: 1.降低pring-context-support版本,但是强制降级影响其他代码运行因此...
Java Velocity 项目生成器使用 Jave Velocity 模板构建前端 Web 应用程序的生成器。特征CSS 自动前缀带有 LiveReload 的内置预览服务器自动编译 CoffeeScript & Sass 自动 lint 你的脚本自动将图像内联到 css 文件...
Velocity+Spring+Ibatis框架搭建说明文档1
根据本人公司开发项目自己搭建的一个velocity小例子,就是想看看velocity如何运行加载前台页面
struts2+spring+velocity简单的扩展实例,下载下来就能运行。后续将整合各项技术。敬请期待。
基于express搭建的velocity服务器 使用方式 Command: $ git clone https://github.com/tangjinzhou/express-velocity.git $ cd express-velcity $ npm install $ npm start 自定义配置 //config.js配置文件 module....
这是一个简易的博客 由SpringMvc+Maven+Mybatis+Mysql+tomcat+velocity搭建而成 一些基本功能都已经具有。 另外需要注意的是,一些样式和静态资源需要另外启用一个Apache。 静态资源地址为:
使用maven搭建的spirng mvc+mybatis+maven+ velocity 的框架,系统只跑一个登陆的过程,代码简洁,你可以根据自己的业务需求扩展。详情请参照源码。
根据风速公式在SIMULINK环境中搭建的随机风速的模型,可运行
大数据的5V特点(IBM提出):Volume(大量)、Velocity(高速)、Variety(多样)、Value(低价值密度)、Veracity(真实性)。 “大数据”一词列出了商务印书馆推出的《汉语新词语词典(2000—2020)》中国这20年...
springboot springboot gradle 搭建 springboot1.5以上不支持velocity 此工程配置可支持velocity 1.spring-boot搭建:spring-boot和core 2.设计模式:design-pattern 各种设计模式的实现
Velocity | 模板引擎 | [http://velocity.apache.org/](http://velocity.apache.org/) FontAwesome | 图标CSS分类参考 | [http://www.yeahzan.com/fa/facss.html](http://www.yeahzan.com/fa/facss.html) H+ | 后台...
此项目基于Java配置的方式搭建,此种配置方式比XML配置方式更加强大,类型安全并且易于重构 建议在开发中优先使用基于Java去配置,如本实例中的 EmailApplicationContext类 Spring Email抽象核心接口MailSender,其...