基于Velocity开发自己的模板引擎
Velocity是一个基于java的模板引擎(template engine)。它同意不论什么人只简单的使用模板语言(template language)来引用由java代码定义的对象。
当Velocity应用于web开发时,界面设计人员能够和java程序开发者同步开发一个遵循MVC架构的web网站,也就是说,页面设计人员能够仅仅 关注页面的显示效果,而由java程序开发者关注业务逻辑编码。Velocity将java代码从web页面中分离出来,这样为web网站的长期维护提 供了便利,同一时候也为我们在JSP,PHP和Freemarker之外又提供了一种可选的方案。
大多数开发者只了解上述部分,即Velocity能够作为MVC的V,所以出现了非常多Velocity和SpringMVC,Velocity和Struts集成的设计。但少有人关注,Velocity作为模板引擎的意义,既然是模板引擎,那它就不应该只局限在MVC的领域。
Velocity的能力远不止web网站开发这个领域,比如,它能够从模板(template)产生SQL和PostScript、XML,它也能够被当 作一个独立工具来产生源码和报告,或者作为其它系统的集成组件使用。
下面代码,是我对Velocity的简单封装,能够将Velocity作为单独的组件来使用,稍加丰富就能够成为我们应用的模板引擎。
核心代码:
package com.ths.platform.framework.template; import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Properties;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine; public class VelocityParser
{
//模板上下文
private VelocityContext mainContext;
//模板对象
private Template mainTemplate;
//模板引擎
private VelocityEngine velocityEngine;
//模板引擎初始化參数
private Properties properties; public static void main( String[ ] args ) {
String filepath = "template/view.jsp";
VelocityParser velocityParser = new VelocityParser( filepath );
velocityParser.addToContext( "title" , "HelloWorld" );
velocityParser.processTemplate( );
} /**
* @MethodName : addToContext
* @Description : 向模板上下文中加入參数
* @param key
* @param value
*/
public void addToContext( String key, Object value ) {
if ( mainContext == null )
{
mainContext = new VelocityContext( );
} mainContext.put( key , value );
} /**
* @MethodName : addToContext
* @Description :初始化模板上下文
* @param chainCtx
*/
public void addToContext( VelocityContext chainCtx ) {
mainContext = new VelocityContext( chainCtx );
} /**
* @MethodName : processTemplate
* @Description : 输出到控制台
*/
public void processTemplate() {
try
{
BufferedWriter writer = new BufferedWriter( new OutputStreamWriter( System.out ) );
if ( mainTemplate != null )
{
mainTemplate.merge( mainContext , writer );
} writer.flush( );
writer.close( );
}
catch ( Exception ex )
{
ex.printStackTrace( );
}
} /**
* @MethodName : processTemplate
* @Description : 输出到文件
* @param destPath
*/
public void processTemplate(String destPath) {
try
{
OutputStream os = new FileOutputStream(destPath);
OutputStreamWriter writer = new OutputStreamWriter(os, "UTF-8"); if ( mainTemplate != null )
{
mainTemplate.merge( mainContext , writer );
} writer.flush( );
writer.close( );
}
catch ( Exception ex )
{
ex.printStackTrace( );
}
} /**
* 依据模板文件初始化模板引擎
* @param templateFile
*/
public VelocityParser( String templateFile ) {
this(templateFile , null);
} /**
* 依据模板文件和模板上下文(參数)初始化模板引擎
* @param templateFile
* @param chainContext
*/
public VelocityParser( String templateFile , VelocityContext chainContext ) {
try
{
//新建模板引擎
velocityEngine = new VelocityEngine( ); //获取初始化參数
properties = initProperties( ); //初始化模板引擎
velocityEngine.init( properties ); //获取模板对象
mainTemplate = velocityEngine.getTemplate( templateFile ); //设置模板上下文
if(chainContext!=null){
//设置模板上下文
mainContext = chainContext;
} }
catch ( Exception ex )
{
System.out.println( "Error processing template file: " + templateFile );
}
} /**
* @MethodName : initProperties
* @Description : 设置初始化參数
* @return
*/
private Properties initProperties() {
Properties properties = new Properties( );
//设置从classpath中载入模板文件
properties.setProperty( Velocity.FILE_RESOURCE_LOADER_PATH , Thread.currentThread( )
.getContextClassLoader( ).getResource( "" ).getPath( ) );
//解决模板中文乱码
properties.setProperty( Velocity.INPUT_ENCODING , "utf-8" );
properties.setProperty( Velocity.OUTPUT_ENCODING , "utf-8" );
return properties;
} }
<table>
<tr><td>$title</td></tr>
</table>
基于Velocity开发自己的模板引擎的更多相关文章
- Spring Boot Web开发与thymeleaf模板引擎
简介: 使用Springboot应用,选中需要的模块, Spring已经默认将场景配置好了,只需在配置文件中少量配置就可以运行起来 自己编写业务代码 自动配置原理 这个场景Springboot帮我们配 ...
- Nunjucks:Mozilla 开发的 JavaScript 模板引擎
Nunjucks 中文网站:https://nunjucks.bootcss.com/
- Spring Boot Web开发中Thymeleaf模板引擎的使用
这里使用的是idea 1.新建Spring Boot项目 File-->New-->Project...,然后选择左边的Spring Initializr-->Next,可根据自己的 ...
- php模板引擎
http://baike.baidu.com/link?url=HmXfdJBv3zpCdnZPeaSmZmqDBHlyTBnz9Rmb5it-jf1_NLHfaku6_i8ssUYbnaTQEBD4 ...
- 一些基于jQuery开发的控件
基于jQuery开发,非常简单的水平方向折叠控件.主页:http://letmehaveblog.blogspot.com/2007/10/haccordion-simple-horizontal-a ...
- PHP模板引擎,Smarty定义
PHP模板引擎:PHP是一种HTML内嵌式的在服务器端执行的脚本语言.初始的开发模板就是混合 层的数据编程,虽然通过MVC的设计模式可以实现将程序的应用逻辑与网页的呈现逻辑强制 分离,但也只是将程序的 ...
- jquery的一个模板引擎-zt
jQuery-jTemplate.js下载:http://jtemplates.tpython.com/ 一 , 简单介绍 它是一个基于jQuery开发的javascript模板引擎.它主要的作用如下 ...
- Spring Boot 系列(五)web开发-Thymeleaf、FreeMarker模板引擎
前面几篇介绍了返回json数据提供良好的RESTful api,下面我们介绍如何把处理完的数据渲染到页面上. Spring Boot 使用模板引擎 Spring Boot 推荐使用Thymeleaf. ...
- JS模板引擎:基于字符串拼接
目的 编写一个基于字符串拼接的js模板引擎雏形,这里并不会提供任何模板与数据的绑定. 基本原理 Javascript中创建函数的方式有多种,包括: 1. var func = function () ...
随机推荐
- 基于DSP的疲劳驾驶检测系统的研究
原地址:http://www.chinaaet.com/article/index.aspx?id=114534 关键词:疲劳检测DSP亮瞳效应PERCLOS 摘 要: 针对汽车驾驶员疲劳驾驶检测的 ...
- oschina 建站系统
建站系统 分类网站程序(9) 众筹平台(2) 团购网站系统(14) 开源轻博客系统(8) 开源博客系统(279) 视频网站系统(9) 开源微博工具(93) 论坛系统BBS(129) 建站系统CMS(5 ...
- 基于Grunt的版本号构建系统新手教程
作者:zhanhailiang 日期:2014-10-12 1. 安装nodejs,npm,grunt-cli.參见<Windows环境下安装nodejs+npm+grunt-cli工具> ...
- Android wear 初体验
近期一直在研究android wear SDK,整体感受来说就是和现有的android 其它的开发SDK还是有非常多新的东西.比如手机终端与手表端的通信机制,手表端的UI规范.可是从开发本身来讲,还是 ...
- OC -- 第一个类
OC -- 第一个类 类名:Car 属性:轮胎个数.时速 行为:跑 完整写一个类:类的声明和实现 1. 类的声明 代码: // NSObject 再Foundation框架中 #import & ...
- bash学习之环境变量
1.查看系统存在的环境变量env 和 export env命令:查看环境变量 [CJP@CJP ~]$ env HOSTNAME=CJP SHELL=/bin/bash HISTSIZE=1000 U ...
- mongodb中的排序和索引快速学习
在mongodb中,排序和索引其实都是十分容易的,先来小结下排序: 1 先插入些数据 db.SortTest.insert( { name : "Denis", age : ...
- Hbase配置中出现的问题总结
在create table的时候出现例如以下问题 1. ERROR: java.io.IOException: Table Namespace Manager not ready yet, try a ...
- hdu 5282 Senior's String 两次dp
题链:http://acm.hdu.edu.cn/showproblem.php?pid=5282 Senior's String Time Limit: 2000/1000 MS (Java/Oth ...
- Spring实战笔记2---Bean的装配
创建应用对象之间协作关系的行为通常成为装配,该篇的主要内容有两个,一个Spring装配Bean的几种方式以及Spring表达式,事实上这两者是分不开的,在Spring中,对象无需自己负责查找或者创建与 ...