VelocityServlet:
-
package
org.liufei.velocity;
-
-
import
java.io.FileNotFoundException;
-
import
java.io.IOException;
-
import
java.util.Properties;
-
-
import
javax.servlet.ServletConfig;
-
import
javax.servlet.http.HttpServletRequest;
-
import
javax.servlet.http.HttpServletResponse;
-
-
import
org.apache.velocity.Template;
-
import
org.apache.velocity.app.Velocity;
-
import
org.apache.velocity.context.Context;
-
import
org.apache.velocity.servlet.VelocityServlet;
-
-
@SuppressWarnings
(
"deprecation"
)
-
public
class
AddServletVelocity
extends
VelocityServlet {
-
-
private
static
final
long
serialVersionUID = -5343161778899944087L;
-
-
@Override
-
protected
Template handleRequest(HttpServletRequest request, HttpServletResponse response, Context ctx)
throws
Exception {
-
Template template = null
;
-
try
{
-
int
a =
11
;
-
int
b =
22
;
-
int
c = a + b ;
-
ctx.put("a"
,
new
Integer(a)) ;
-
ctx.put("b"
,
new
Integer(b)) ;
-
ctx.put("c"
,
new
Integer(c)) ;
-
template = getTemplate("add.vm"
) ;
-
}catch
(Exception e) {
-
System.out.println(e.getLocalizedMessage());
-
}
-
return
template;
-
}
-
-
@Override
-
protected
Properties loadConfiguration(ServletConfig servletConfig)
throws
IOException, FileNotFoundException {
-
Properties properties = new
Properties() ;
-
String path = servletConfig.getServletContext().getRealPath("/"
) ;
-
if
(path ==
null
){
-
System.out.println("Error !"
);
-
path = "/"
;
-
}
-
properties.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path) ;
-
properties.setProperty("runtime.log"
, path +
"velocity.log"
) ;
-
properties.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8"
);
-
properties.setProperty(Velocity.INPUT_ENCODING, "UTF-8"
);
-
properties.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8"
);
-
return
properties ;
-
}
-
-
}
vm模板:
-
<html>
-
<head>
-
<title>Velocity Test</title>
-
</head>
-
<body>
-
<h1>Velcity Excemple</h1>
-
<p>$a + $b = $c</p>
-
</body>
-
</html>
web.xml配置:
-
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
-
<web-app version="2.5"
xmlns=
"http://java.sun.com/xml/ns/javaee"
-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-
xsi:schemaLocation="http:
-
http:
-
-
<servlet>
-
<servlet-name>add</servlet-name>
-
<servlet-class
>org.liufei.velocity.AddServletVelocity</servlet-
class
>
-
</servlet>
-
<servlet-mapping>
-
<servlet-name>add</servlet-name>
-
<url-pattern>/add</url-pattern>
-
</servlet-mapping>
-
-
<!--
-
<welcome-file-list>
-
<welcome-file>index.jsp</welcome-file>
-
</welcome-file-list>
-
-->
-
</web-app>
运行结果:
Velcity Excemple
11 + 22 = 33
分享到:
相关推荐
Java的模板引擎Velocity初体验.pdf
JAVA的Velocity语法学习 内涵《Velocity_java开发指南》《Java的模板引擎Velocity初体验》 以及所必须的jar ,和大家一块学习,很好哦
Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎Velocity模板引擎
Velocity Velocity Velocity Velocity Velocity Velocity Velocity Velocity Velocity Velocity
Velocity 和 FreeMarker区别 对于大部分的应用来说,使用 FreeMarker 比 Velocity 更简单,因为 Velocity 还必须编写一些自定义的
Velocity教程
打开网址:http://www.web-tag.net/all_17.htm 就是velocity标签大全 、教程 或下载CHM格式帮助文档
在使用velocity作为视图层时候,经常会碰到为空的判断,这里整理了velocity为空判断
本课程从velocity engine也就是velocity引擎开始, 先讲解velocity的基本使用以及基础语法 , 然后再讲解velocity 的进阶内容velocity Tools , 以及velocity作为web项目的视图改如何使用 , 每一部分都会有一个综合案例...
Velocity入门教程,语法,Velocity布局,Spring框架集成Velocity
velocity 电子书velocity 电子书velocity 电子书
赠送jar包:velocity-engine-core-2.3.jar; 赠送原API文档:velocity-engine-core-2.3-javadoc.jar; 赠送源代码:velocity-engine-core-2.3-sources.jar; 赠送Maven依赖信息文件:velocity-engine-core-2.3.pom;...
关于velocity的学习资料,velocity教程和velocity用户手册
velocity中文文档 教程 velocity中文文档 教程 velocity中文文档 教程
开发velocity所需的jar包
vim velocity插件 velocity.vim vim异常强大哦
赠送jar包:velocity-engine-core-2.3.jar 赠送原API文档:velocity-engine-core-2.3-javadoc.jar 赠送源代码:velocity-engine-core-2.3-sources.jar 包含翻译后的API文档:velocity-engine-core-2.3-javadoc-...
赠送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-...
模板:velocity和freemarker的比较模板:velocity和freemarker的比较
velocityTemplate java 模块 引擎\Velocity教程