- 浏览: 676263 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
Hippyqq:
谢谢很有用,
java中遍历MAP的几种方法 -
XSoftlab:
超详细。。。Java map 详解 - 用法、遍历、排序、常用 ...
java中遍历MAP的几种方法 -
bobo22:
importnet.sf.fmj.ui.application ...
java来调用电脑视频摄像头拍照进行截图 -
qq981378640:
#include <stdio.h>
int ...
c语言中unsigned类型和普通类型间的转换 -
qq981378640:
楼主你这样有点复杂了,直接这样写更好更方便
#include ...
c语言中unsigned类型和普通类型间的转换
Velocity作为一款成熟的基于java的模板引擎,能够帮我们实现页面静态化。它允许使用模板语言(template language)来引用由java代码定义的对象。最重要的是,它简单易用,乃至于看一眼它的示例就可上手使用。
------------------------------------------
“Velocity将Java代码从Web 页面中分离出来,使用Web站点从长远看更容易维护,并且提供了一种可行的JavaServer Pages替代解决方案。”——Velocity用户手册
Velocity下载地址:http://velocity.apache.org/
在开发中,我们必须导入\lib\log4j-1.2.12.jar和velocity-1.6-dep.jar两个包。如果使用velocity-1.6.jar会稍微麻烦一点,需要把\lib下的commons-collections-3.2.1.jar\commons-lang-2.4.jar和oro-2.0.8.jar放入类路径下。 velocity-1.6-dep.jar文件内部已经包含前面三个jar文件的类。
导入开发包以后,我们需要在类路径中加入velocity.properties文档:
指定日志文件存放位置
runtime.log = E:\\spring\\velocity\\velocity_example.log
指定模版文件加载位置
file.resource.loader.path=E:\\spring\\velocity
指定输入编码格式
input.encoding=UTF-8
指定velocity的servlet向浏览器输出内容的编码
default.contentType=text/html; charset\=UTF-8
指定输出编码格式
output.encoding=UTF-8
接下来是老黎讲的10个例子:
一:
try{
Velocity.init("src/velocity.properties");
VelocityContext context = new VelocityContext();
context.put("hello", "HelloWorld");
context.put("who", "黎明");
Template template = Velocity.getTemplate("mytemplate.vm");
StringWriter sw = new StringWriter();
template.merge(context, sw);
sw.flush();
System.out.println(sw.toString());
}catch( Exception e ){
e.printStackTrace();
}
mytemplate.vm内容如下:
${who}说:${hello}
二:访问对象
try{
Velocity.init("src/velocity.properties");
VelocityContext context = new VelocityContext();
context.put("person", new Person(2,"小张"));
Template template = Velocity.getTemplate("mytemplate.vm");
StringWriter sw = new StringWriter();
template.merge(context, sw);
sw.flush();
System.out.println(sw.toString());
}catch( Exception e ){
e.printStackTrace();
}
mytemplate.vm内容如下:
${person.id}=${person.name}
三:历遍集合/数组
try{
Velocity.init("src/velocity.properties");
VelocityContext context = new VelocityContext();
context.put("list", Arrays.asList("第一个","第二个","第三个"));
Template template = Velocity.getTemplate("mytemplate.vm");
StringWriter sw = new StringWriter();
template.merge(context, sw);
sw.flush();
System.out.println(sw.toString());
}catch( Exception e ){
e.printStackTrace();
}
mytemplate.vm内容如下:
#foreach($element in $list)
$element
#end
四:历遍Map集合
try{
Velocity.init("src/velocity.properties");
VelocityContext context = new VelocityContext();
Map<String,String> map = new HashMap<String, String>();
map.put("key1", "value1");
map.put("key2", "value2");
context.put("map", map);
Template template = Velocity.getTemplate("mytemplate.vm");
StringWriter sw = new StringWriter();
template.merge(context, sw);
sw.flush();
System.out.println(sw.toString());
}catch( Exception e ){
e.printStackTrace();
}
mytemplate.vm内容如下:
#foreach( $key in $map.keySet() )
$key=$map.get($key)
#end
五:获得当前迭代的索引
try{
Velocity.init("src/velocity.properties");
VelocityContext context = new VelocityContext();
Map<String,String> map = new HashMap<String, String>();
map.put("key1", "value1");
map.put("key2", "value2");
context.put("map", map);
Template template = Velocity.getTemplate("mytemplate.vm");
StringWriter sw = new StringWriter();
template.merge(context, sw);
sw.flush();
System.out.println(sw.toString());
}catch( Exception e ){
e.printStackTrace();
}
mytemplate.vm内容如下:
#foreach( $key in $map.keySet() )
$velocityCount > $key=$map.get($key)
#end
velocityCount 变量名可以通过directive.foreach.counter.name属性修改,如: directive.foreach.counter.name =index,以后可以通过$index进行访问。迭代的索引默认从1开始,我们可以通过directive.foreach.counter.initial.value=0进行修改。
六:在模版中进行赋值
#set( $name = "老张" )
$name
#set( $condition = true )
数组赋值:
#set( $ints = [1..10])
#foreach( $entry in $ints)
$entry
#end
#set( $ints = ["第一个","第二个"])
#foreach( $entry in $ints)
$entry
#end
七:#if#else#end
try{
Velocity.init("src/velocity.properties");
VelocityContext context = new VelocityContext();
context.put("condition", true);
Template template = Velocity.getTemplate("mytemplate.vm");
StringWriter sw = new StringWriter();
template.merge(context, sw);
sw.flush();
System.out.println(sw.toString());
}catch( Exception e ){
e.printStackTrace();
}
mytemplate.vm内容如下:
#if($condition)
成立
#else
不成立
#end
八:格式化日期
格式化日期必须将velocity-tools-generic-1.4.jar包加入到类路径。
try{
Velocity.init("src/velocity.properties");
VelocityContext context = new VelocityContext();
context.put("now", new Date());
context.put("dateformat", new DateTool());
Template template = Velocity.getTemplate("mytemplate.vm");
StringWriter sw = new StringWriter();
template.merge(context, sw);
sw.flush();
System.out.println(sw.toString());
}catch( Exception e ){
e.printStackTrace();
}
mytemplate.vm内容如下:
$dateformat.format("yyyy-MM-dd", $now)
九:输出到文件
try{
Velocity.init("src/velocity.properties");
VelocityContext context = new VelocityContext();
context.put("title", "巴巴运行网");
context.put("body", "这是内容");
Template template = Velocity.getTemplate("mytemplate.vm");
File savefile = new File("E:\\spring\\velocity\\WebRoot\\page\\test.html");
if(!savefile.getParentFile().exists()) savefile.getParentFile().mkdirs();
FileOutputStream outstream = new FileOutputStream(savefile);
OutputStreamWriter writer = new OutputStreamWriter(outstream,"UTF-8");
BufferedWriter bufferWriter = new BufferedWriter(writer);
template.merge(context, bufferWriter);
bufferWriter.flush();
outstream.close();
bufferWriter.close();
}catch( Exception e ){
e.printStackTrace();
}
十:null处理
try{
Velocity.init("src/velocity.properties");
VelocityContext context = new VelocityContext();
context.put("title", null);
Template template = Velocity.getTemplate("mytemplate.vm");
StringWriter writer = new StringWriter();
BufferedWriter bufferWriter = new BufferedWriter(writer);
template.merge(context, bufferWriter);
bufferWriter.flush();
System.out.println(writer.toString());
}catch( Exception e ){
e.printStackTrace();
}
mytemplate.vm内容如下:
$title
------------------------------------------
“Velocity将Java代码从Web 页面中分离出来,使用Web站点从长远看更容易维护,并且提供了一种可行的JavaServer Pages替代解决方案。”——Velocity用户手册
Velocity下载地址:http://velocity.apache.org/
在开发中,我们必须导入\lib\log4j-1.2.12.jar和velocity-1.6-dep.jar两个包。如果使用velocity-1.6.jar会稍微麻烦一点,需要把\lib下的commons-collections-3.2.1.jar\commons-lang-2.4.jar和oro-2.0.8.jar放入类路径下。 velocity-1.6-dep.jar文件内部已经包含前面三个jar文件的类。
导入开发包以后,我们需要在类路径中加入velocity.properties文档:
指定日志文件存放位置
runtime.log = E:\\spring\\velocity\\velocity_example.log
指定模版文件加载位置
file.resource.loader.path=E:\\spring\\velocity
指定输入编码格式
input.encoding=UTF-8
指定velocity的servlet向浏览器输出内容的编码
default.contentType=text/html; charset\=UTF-8
指定输出编码格式
output.encoding=UTF-8
接下来是老黎讲的10个例子:
一:
try{
Velocity.init("src/velocity.properties");
VelocityContext context = new VelocityContext();
context.put("hello", "HelloWorld");
context.put("who", "黎明");
Template template = Velocity.getTemplate("mytemplate.vm");
StringWriter sw = new StringWriter();
template.merge(context, sw);
sw.flush();
System.out.println(sw.toString());
}catch( Exception e ){
e.printStackTrace();
}
mytemplate.vm内容如下:
${who}说:${hello}
二:访问对象
try{
Velocity.init("src/velocity.properties");
VelocityContext context = new VelocityContext();
context.put("person", new Person(2,"小张"));
Template template = Velocity.getTemplate("mytemplate.vm");
StringWriter sw = new StringWriter();
template.merge(context, sw);
sw.flush();
System.out.println(sw.toString());
}catch( Exception e ){
e.printStackTrace();
}
mytemplate.vm内容如下:
${person.id}=${person.name}
三:历遍集合/数组
try{
Velocity.init("src/velocity.properties");
VelocityContext context = new VelocityContext();
context.put("list", Arrays.asList("第一个","第二个","第三个"));
Template template = Velocity.getTemplate("mytemplate.vm");
StringWriter sw = new StringWriter();
template.merge(context, sw);
sw.flush();
System.out.println(sw.toString());
}catch( Exception e ){
e.printStackTrace();
}
mytemplate.vm内容如下:
#foreach($element in $list)
$element
#end
四:历遍Map集合
try{
Velocity.init("src/velocity.properties");
VelocityContext context = new VelocityContext();
Map<String,String> map = new HashMap<String, String>();
map.put("key1", "value1");
map.put("key2", "value2");
context.put("map", map);
Template template = Velocity.getTemplate("mytemplate.vm");
StringWriter sw = new StringWriter();
template.merge(context, sw);
sw.flush();
System.out.println(sw.toString());
}catch( Exception e ){
e.printStackTrace();
}
mytemplate.vm内容如下:
#foreach( $key in $map.keySet() )
$key=$map.get($key)
#end
五:获得当前迭代的索引
try{
Velocity.init("src/velocity.properties");
VelocityContext context = new VelocityContext();
Map<String,String> map = new HashMap<String, String>();
map.put("key1", "value1");
map.put("key2", "value2");
context.put("map", map);
Template template = Velocity.getTemplate("mytemplate.vm");
StringWriter sw = new StringWriter();
template.merge(context, sw);
sw.flush();
System.out.println(sw.toString());
}catch( Exception e ){
e.printStackTrace();
}
mytemplate.vm内容如下:
#foreach( $key in $map.keySet() )
$velocityCount > $key=$map.get($key)
#end
velocityCount 变量名可以通过directive.foreach.counter.name属性修改,如: directive.foreach.counter.name =index,以后可以通过$index进行访问。迭代的索引默认从1开始,我们可以通过directive.foreach.counter.initial.value=0进行修改。
六:在模版中进行赋值
#set( $name = "老张" )
$name
#set( $condition = true )
数组赋值:
#set( $ints = [1..10])
#foreach( $entry in $ints)
$entry
#end
#set( $ints = ["第一个","第二个"])
#foreach( $entry in $ints)
$entry
#end
七:#if#else#end
try{
Velocity.init("src/velocity.properties");
VelocityContext context = new VelocityContext();
context.put("condition", true);
Template template = Velocity.getTemplate("mytemplate.vm");
StringWriter sw = new StringWriter();
template.merge(context, sw);
sw.flush();
System.out.println(sw.toString());
}catch( Exception e ){
e.printStackTrace();
}
mytemplate.vm内容如下:
#if($condition)
成立
#else
不成立
#end
八:格式化日期
格式化日期必须将velocity-tools-generic-1.4.jar包加入到类路径。
try{
Velocity.init("src/velocity.properties");
VelocityContext context = new VelocityContext();
context.put("now", new Date());
context.put("dateformat", new DateTool());
Template template = Velocity.getTemplate("mytemplate.vm");
StringWriter sw = new StringWriter();
template.merge(context, sw);
sw.flush();
System.out.println(sw.toString());
}catch( Exception e ){
e.printStackTrace();
}
mytemplate.vm内容如下:
$dateformat.format("yyyy-MM-dd", $now)
九:输出到文件
try{
Velocity.init("src/velocity.properties");
VelocityContext context = new VelocityContext();
context.put("title", "巴巴运行网");
context.put("body", "这是内容");
Template template = Velocity.getTemplate("mytemplate.vm");
File savefile = new File("E:\\spring\\velocity\\WebRoot\\page\\test.html");
if(!savefile.getParentFile().exists()) savefile.getParentFile().mkdirs();
FileOutputStream outstream = new FileOutputStream(savefile);
OutputStreamWriter writer = new OutputStreamWriter(outstream,"UTF-8");
BufferedWriter bufferWriter = new BufferedWriter(writer);
template.merge(context, bufferWriter);
bufferWriter.flush();
outstream.close();
bufferWriter.close();
}catch( Exception e ){
e.printStackTrace();
}
十:null处理
try{
Velocity.init("src/velocity.properties");
VelocityContext context = new VelocityContext();
context.put("title", null);
Template template = Velocity.getTemplate("mytemplate.vm");
StringWriter writer = new StringWriter();
BufferedWriter bufferWriter = new BufferedWriter(writer);
template.merge(context, bufferWriter);
bufferWriter.flush();
System.out.println(writer.toString());
}catch( Exception e ){
e.printStackTrace();
}
mytemplate.vm内容如下:
$title
发表评论
-
java应用CPU占用100%内存泄漏分析总结
2017-11-28 08:46 518转; http://www.blogjava.net/hank ... -
Java Mysql存取Blob数据
2014-10-21 12:54 1494当数据库字段为blob类型时 ,我们如果使用PreparedS ... -
关于日志的等级 的配置
2014-07-02 16:49 612#这里定义能显示到的最低级别,若定义到INFO级别,则看不到D ... -
Extjs中如何在一行textfield后面加几个字
2014-04-14 16:38 1489{ fieldLabel : '输入框', xtype : ' ... -
数据库中图片的操作(字节流的操作)
2013-12-09 17:26 798package com.fww.util; import j ... -
java 获得本地ip
2013-11-28 18:04 859Enumeration allNetInterfaces = ... -
java多线程下载的例子代码
2013-09-09 16:16 0java多线程下载的例子代码 import java.io.I ... -
java来调用电脑视频摄像头拍照进行截图
2013-09-09 15:34 10348packagecom.pengo.capture; i ... -
java中lucene解析word工具类
2013-09-09 15:32 1021java中lucene解析word工具类(读取word文档并查 ... -
Java 向oracle插入百万数据
2013-09-09 15:30 1524这里的表是没加索引的 ... -
java 写 excel
2013-08-28 10:29 926import java.io.FileOutputStream ... -
list 排序
2013-06-20 20:43 1030import java.util.Collections; i ... -
java抓取网络图片放到本地
2013-05-07 10:39 4238import java.io.BufferedInputStr ... -
java代码发送JSON格式的httpPOST请求
2013-05-06 13:41 20285package com.test; import java. ... -
java操作excel(HSSF样式) .
2013-04-15 15:06 1916java操作excel(HSSF样式一) import ja ... -
JAVA+Ftpclient操作
2013-03-07 16:35 6528最近再做个ftp上传、下载的项目,用到的包是sun.net.f ... -
自动消除文本框中的空格;js清除空格问题,去除空格问题
2013-03-01 11:22 6278自动消除文本框中的空格;js清除空格问题,去除空格问题 要完成 ... -
java执行Linux命令 .
2012-12-26 09:29 1231/** * 修改Linux目录权限 * @param ... -
UUID生成类
2012-12-16 18:18 2420import java.net.InetAddress; im ... -
java反射获得类的字段和值
2012-12-10 17:26 1602import java.util.HashMap; impo ...
相关推荐
使用velocity freemarke 模版实现页面静态化,有具体代码实现例子
Struts2集成FreeMarker和Velocity,写成了工具类,快速实现页面静态化,以后直接调用即可,无需修改任何源代码,改压缩文件包括 1、工具类; 2、源码(含jar包); 3、mysql数据库可执行文件; 4、struts2集成...
包括使用freemarker和velocity两种技术来实现静态化。压缩包里面的文件有说明文档,说得很仔细,特别适合入门级别的人参考。还有freemarker和velocity开发所需要的jar包和插件。
【资源说明】 1、该资源包括项目的全部源码,下载可以直接使用! 2、本项目适合作为计算机、数学、电子信息等...基于Spring+JPA+Velocity+Ehcache开发的商城系统源码+数据库(结合URL重写技术静态化商城前台页面).zip
Thymeleaf 是一个跟 Velocity、FreeMarker 类似的模板引擎,它可以完全替代 JSP .1.Thymeleaf 在有网络和无网络的环境下皆可运行,即它可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的...
简单说, Thymeleaf 是一个跟 Velocity、FreeMarker 类似的模板引擎,它可以完全替代 ... Thymeleaf 提供spring标准方言和一个与 SpringMVC 完美集成的可选模块,可以快速的实现表单绑定、属性编辑器、国际化等功能。
简单说, Thymeleaf 是一个跟 Velocity、FreeMarker 类似的模板引擎,它可以完全替代 ...3.Thymeleaf 提供spring标准方言和一个与 SpringMVC 完美集成的可选模块,可以快速的实现表单绑定、属性编辑器、国际化等功能。
以亲身经历举例, 2011 年左右进入前端领域,当时工作的主要内容是,将设计稿切图转成静态页面,然后用 jQuery 插件实现一些页面的轮播图、跑马灯等交互效果。 最后使用后端的模板语言如 Smart、Velocity 等将静态...
6.4.1国际化实现原理 131 6.4.2准备国际化资源文件 131 6.4.3调用国际化资源文件 134 6.5上传和下载 135 6.5.1文件上传的实现原理 135 6.5.2struts2文件上传实现方式 136 6.5.3struts2文件下载实现方式 141 ...
1) 功能全面:内置稳定高效的MVC基础架构和DAO框架,支持Action拦截、Form Bean / Dao Bean / Spring Bean装配和声明式事务,提供国际化、文件上传下载、缓存和页面静态化等常用Web组件,能满足绝大部分Web应用的...
它能帮助我们在由大量页面构成的项目中创建一致的页面布局和外观,如一致的导航条,一致的 banner,一致的版权,等等。它不仅仅能处理动态的内容,如jsp,php,asp等产生的内容,它也能处理静态的内容,使得它的内容...
3.3.3 使用Spring的静态切入点 3.3.4 使用动态切入点 3.3.5 切入点实施 3.4 创建引入 3.4.1 实现IntroductionInterceptor 3.4.2 创建一个引入Advisor 3.4.3 谨慎使用引入通知 3.5 ...
8.使用ForwardAction实现页面屏蔽。 13 8.使用default-Action配置统一访问 14 小结Action 14 9.使用通配符 14 10.使用0配置:ZERO Annotation 15 11.Result配置详解 15 探讨type类型: 16 Type类型值 16 作用说明 16...
内容简介 Struts 2 是Java Web 应用首选的MVC 框架。《深入浅出Struts2》对Struts 2 的工作机理进行了...如果某个应用程序具备国际化支持,我们就可以快速方便地改变它的各种屏显文字。Java已经内建了国际化支持功能。
FreeMarker模板引擎与动态页面静态化 模板引擎可以让程序实现界面与数据分离,业务代码与逻辑代码的分离,这就提升了开发效率,良好的设计也使得代码复用变得更加容易。一般的模板引擎都包含一个模板解析器和一套...
3.3 把静态参数传递给一个动作 41 3.4 ActionSupport类 41 3.5 结果 42 3.5.1 Chain 43 3.5.2 Dispatcher 44 3.5.3 FreeMarker 44 3.5.4 HttpHeader 44 3.5.5 Redirect 45 3.5.6 Redirect Action 46 3.5.7 ...
3.8.1. 利用MessageSource实现国际化 3.8.2. 事件 3.8.3. 底层资源的访问 3.8.4. ApplicationContext在WEB应用中的实例化 3.9. 粘合代码和可怕的singleton 3.9.1. 使用Singleton-helper类 4. 资源 4.1. 简介 4.2. ...