`
rain_2372
  • 浏览: 676263 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Velocity帮我们实现页面静态化

    博客分类:
  • java
阅读更多
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 freemarke 模版 静态化 实现

    使用velocity freemarke 模版实现页面静态化,有具体代码实现例子

    Struts2集成FreeMarker和Velocity,写成了工具类,快速实现页面静态化

    Struts2集成FreeMarker和Velocity,写成了工具类,快速实现页面静态化,以后直接调用即可,无需修改任何源代码,改压缩文件包括 1、工具类; 2、源码(含jar包); 3、mysql数据库可执行文件; 4、struts2集成...

    JSP页面静态化

    包括使用freemarker和velocity两种技术来实现静态化。压缩包里面的文件有说明文档,说得很仔细,特别适合入门级别的人参考。还有freemarker和velocity开发所需要的jar包和插件。

    基于Spring+JPA+Velocity+Ehcache开发的商城系统源码+数据库.zip

    【资源说明】 1、该资源包括项目的全部源码,下载可以直接使用! 2、本项目适合作为计算机、数学、电子信息等...基于Spring+JPA+Velocity+Ehcache开发的商城系统源码+数据库(结合URL重写技术静态化商城前台页面).zip

    Thymeleaf中文参考手册_3.0.5版

    Thymeleaf 是一个跟 Velocity、FreeMarker 类似的模板引擎,它可以完全替代 JSP .1.Thymeleaf 在有网络和无网络的环境下皆可运行,即它可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的...

    thymeleaf_3.0.5_中文参考手册

    简单说, Thymeleaf 是一个跟 Velocity、FreeMarker 类似的模板引擎,它可以完全替代 ... Thymeleaf 提供spring标准方言和一个与 SpringMVC 完美集成的可选模块,可以快速的实现表单绑定、属性编辑器、国际化等功能。

    thymeleaf_中文参考手册

    简单说, Thymeleaf 是一个跟 Velocity、FreeMarker 类似的模板引擎,它可以完全替代 ...3.Thymeleaf 提供spring标准方言和一个与 SpringMVC 完美集成的可选模块,可以快速的实现表单绑定、属性编辑器、国际化等功能。

    一名合格前端工程师的进阶指南!都来认真看一下吧

    以亲身经历举例, 2011 年左右进入前端领域,当时工作的主要内容是,将设计稿切图转成静态页面,然后用 jQuery 插件实现一些页面的轮播图、跑马灯等交互效果。 最后使用后端的模板语言如 Smart、Velocity 等将静态...

    Java Web程序设计教程

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

    JessMA Java Web 应用开发框架 (v3.2.2-20130815).pdf

    1) 功能全面:内置稳定高效的MVC基础架构和DAO框架,支持Action拦截、Form Bean / Dao Bean / Spring Bean装配和声明式事务,提供国际化、文件上传下载、缓存和页面静态化等常用Web组件,能满足绝大部分Web应用的...

    java开发常用jar包

    它能帮助我们在由大量页面构成的项目中创建一致的页面布局和外观,如一致的导航条,一致的 banner,一致的版权,等等。它不仅仅能处理动态的内容,如jsp,php,asp等产生的内容,它也能处理静态的内容,使得它的内容...

    spring in action英文版

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

    Struts2入门教程(全新完整版)

    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 .pdf(原书扫描版) part 1

    内容简介 Struts 2 是Java Web 应用首选的MVC 框架。《深入浅出Struts2》对Struts 2 的工作机理进行了...如果某个应用程序具备国际化支持,我们就可以快速方便地改变它的各种屏显文字。Java已经内建了国际化支持功能。

    java版飞机大战源码-migo-freemaker:类似freemaker的Demo

    FreeMarker模板引擎与动态页面静态化 模板引擎可以让程序实现界面与数据分离,业务代码与逻辑代码的分离,这就提升了开发效率,良好的设计也使得代码复用变得更加容易。一般的模板引擎都包含一个模板解析器和一套...

    深入浅出Struts2(附源码)

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

    Spring-Reference_zh_CN(Spring中文参考手册)

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

Global site tag (gtag.js) - Google Analytics