基于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 () ...
随机推荐
- POJ1505&&UVa714 Copying Books(DP)
Copying Books Time Limit: 3000MS Memory Limit: 10000K Total Submissions: 7109 Accepted: 2221 Descrip ...
- [Cocos2d-x]CCSpriteFrameCache的使用
文档: http://cocos2d.cocoachina.com/document/index/class?url=dc/dda/classcocos2d_1_1_c_c_sprite_frame_ ...
- Spring MVC Hello World Example(转)
Spring 3 You may interest at this Spring 3 MVC hello world example. In Spring MVC web application, i ...
- OCP读书笔记(16) - 管理资源
使用者组 创建资源用户组OLTP_GRP,将用户HR,OE加入此组: BEGIN dbms_resource_manager.clear_pending_area(); dbms_resource_m ...
- [置顶] think in java interview-高级开发人员面试宝典(七)
上两周研发任务太紧了,所以担搁了一下,我们继续我们的面试之旅. 下面是一个基于图书系统的15道SQL问答,供大家参考 问题描述:本题用到下面三个关系表:CARD 借书卡. CNO 卡号,N ...
- SQLServer 复制和数据库镜像 具体配置部署
SQLserver 能够把镜像和复制同一时候部署,结合了两方的高可用性.能够使数据库得到更好的高可用性和容灾的保证. 关于镜像:f=255&MSPPError=-2147217396" ...
- sort功能使用
头: #include <algorithm> using namespace std; 1.默认的sort函数是按升序排序. sort(a,a+n); / ...
- Thinkphp常用标签
告:在使用下列所说的任何标签库都需要 HTML第一行加入 <tarlib name=”cx,html” /> 如果想单独引入cx标签库就直接写成<tarlib name=”cx” / ...
- GString及IntelliJIdea中调试Groovy的操作步骤
今天是学习Groovy的第一天,首先我觉得学习任何一种语言都要先弄清楚这种语言的特性,因为只有了解了特性之后学习才能达到好的效果,那么groovy的特点是什么的.我觉得groovy是一种动态语言,动态 ...
- OpenCV 编程简单介绍(矩阵/图像/视频的基本读写操作)
PS. 因为csdn博客文章长度有限制,本文有部分内容被截掉了.在OpenCV中文站点的wiki上有可读性更好.而且是完整的版本号,欢迎浏览. OpenCV Wiki :<OpenCV 编程简单 ...