- 浏览: 198563 次
- 性别:
- 来自: 南京
文章分类
最新评论
-
yixinhu:
你好 能加下你的QQ么 我怎么打出来的exe点击都没反应啊.. ...
java 生成exe 文件 -
chenxiang105:
如果不需要flash 只需要做图片的翻页效果 这个是否也合适类 ...
jQuery插件page peel AD实现动态卷页、翻页或卷角flash广告效果 -
tuoxie007:
幸苦,好文章!
jetspeed门户项目组介绍 -
bobo:
需要考虑不同浏览器的兼容性
通过网页访问本地资源程序 -
tag13346:
说得这么玄,看下
时空趋势理论 --- 超越时空的均线技术(转载 )
关键字: 模板技术
/**
*作者:张荣华(ahuaxuan)
*2007-04-16
*转载请注明出处及作者
*/
模板技术在现代的软件开发中有着重要的地位,而目前最流行的两种模板技术恐怕要算freemarker和velocity
velocity
作为view,模板技术作为view的好处是很多,尤其和jsp
比较起来优点更大,众所周知jsp
需要在第一次被执行的时候编译成servlet,那么这个过程是很慢的,当然很多应用服务器都提供预编译的功能,但是在开发的时候仍然给我们程序员带来了很多痛苦,每次修改都要多几秒钟,那在一天的开发中就有很多时间浪费在jsp
的编译上了。用webwork in action的作者的话来说:“每次修改之后重新运行都要等等几秒是令人失望的,而频繁地修改jsp
更是会令你的失望情绪变本加厉“。我们把模板技术引入到view中去可以带来更好的开发效率,而且模板的速度要比jsp
快(虽然编译过后的jsp
在速度上已经满足我的需求了,呵呵)。 当然模板技术可以用在很多领域,可不只在view那里。我们可以通过模板技术来生成xml,生成jsp
,生成java文件等等,说到这里,大家通常会使用模板技术用在公司的框架里,这样就可以很快速的生成添删改查的代码,需要的只是模板,其他比如还有邮件模板等等。
以上是模板的作用,当然模板还有其他领域的应用,希望能和大家多讨论,提高我们的生产效率。
那么现在开源的模板技术有好几种,多了之后就有一个选择的问题了,如何选择一个满足自己需要的模板的呢,我大概了看了一下两种模板技术,写了一个例子,我使用了几种设计模式来完成了这个例子,这个例子中,我同时使用了freemarker和velocity
,这样同学们可以通过代码很直观的比较两种模板技术,通过这个例子,我认识到freemarker在功能上要比velocity
强大
1在view层的时候,它提供了format日期和数字的功能,我想大家都有在页面上format日期或数字的经验,用jsp
的同学可能对jstl的fmt标签很有感情,使用了freemarker之后也可以使用freemarker提供的功能来formmat日期和数据,这个功能我想是很贴心的
2通过我的使用我发现freemaker的eclipseplugin要比velocity
的eclipseplugin好,如果你是用idea那很遗憾,我没有找到类似的插件。好在很多地方呢,我看到的是freemarker的插件除了支持freemarker语法也支持html语句,而velocity
的插件貌似只支持velocity
的语法,html就只是用普通的文本来显示了,在这一点上freemarker占上风了(不要和我说高手都是用windows记事本之类的话,这本来就违背了模板技术的初衷)
3freemarker对jsptag的支持很好,算了,不到迫不得已还是不要这样做吧。
还有就是两者的语法格式,这一点上不同的人有不同倾向
下面就介绍一下这个例子吧
了,webwork2.2对两者都有不错的支持,也就是说在webwork2中你可以随意选择使用freemarker或
- /**
- *
- * @author 张荣华
- * 转载请注明出处
- */
- public class TemplateTest {
- /**
- * @param args
- */
- public static void main(String[] args) throws Exception{
- /* 准备数据 */
- Map latest = new HashMap();
- latest.put( "url" , "products/greenmouse.html" );
- latest.put( "name" , "green mouse" );
- Map root = new HashMap();
- root.put( "user" , "Big Joe" );
- root.put( "latestProduct" , latest);
- root.put( "number" , new Long( 2222 ));
- root.put( "date" , new Date());
- List listTest = new ArrayList();
- listTest.add( "1" );
- listTest.add( "2" );
- root.put( "list" ,listTest);
- TemplateEngine freemarkerEngine = (TemplateEngine)TemplateFactory.getInstance().getBean( "freemarker" );
- freemarkerEngine.run(root); //使用freemarker模板技术
- TemplateEngine velocityEngine = (TemplateEngine)TemplateFactory.getInstance().getBean( " velocity " );
- velocityEngine.run(root); //使用 velocity 模板技术
- }
- }
/**
*
* @author 张荣华
* 转载请注明出处
*/
public class TemplateTest {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
/* 准备数据 */
Map latest = new HashMap();
latest.put("url", "products/greenmouse.html");
latest.put("name", "green mouse");
Map root = new HashMap();
root.put("user", "Big Joe");
root.put("latestProduct", latest);
root.put("number", new Long(2222));
root.put("date",new Date());
List listTest = new ArrayList();
listTest.add("1");
listTest.add("2");
root.put("list",listTest);
TemplateEngine freemarkerEngine = (TemplateEngine)TemplateFactory.getInstance().getBean("freemarker");
freemarkerEngine.run(root);//使用freemarker模板技术
TemplateEngine velocityEngine = (TemplateEngine)TemplateFactory.getInstance().getBean("velocity
velocity
");
velocityEngine.run(root);//使用
模板技术
}
}
工厂类,用来得到模板引擎
- /**
- *
- * @author 张荣华
- * 转载请注明出处
- */
- public class TemplateFactory {
- private static TemplateFactory instance;
- private Map objectMap;
- static {
- instance = new TemplateFactory();
- }
- public TemplateFactory() {
- super ();
- this .objectMap = new HashMap();
- synchronized ( this ) {
- objectMap.put( "freemarker" , new FreemarkerTemplateEngine(){
- public String getTemplatePath() {
- return "template" ;
- }
- });
- objectMap.put( " velocity " , new VelocityTemplateEngine());
- }
- }
- public static TemplateFactory getInstance(){
- return instance;
- }
- /**
- * 模仿spring的工厂
- * @param beanName
- * @return
- */
- public Object getBean(String beanName){
- return objectMap.get(beanName);
- }
- }
/**
*
* @author 张荣华
* 转载请注明出处
*/
public class TemplateFactory {
private static TemplateFactory instance;
private Map objectMap;
static{
instance = new TemplateFactory();
}
public TemplateFactory() {
super();
this.objectMap = new HashMap();
synchronized (this) {
objectMap.put("freemarker", new FreemarkerTemplateEngine(){
public String getTemplatePath() {
return "template";
}
});
objectMap.put("velocity
", new VelocityTemplateEngine());
}
}
public static TemplateFactory getInstance(){
return instance;
}
/**
* 模仿spring的工厂
* @param beanName
* @return
*/
public Object getBean(String beanName){
return objectMap.get(beanName);
}
}
引擎接口
- /**
- *
- * @author 张荣华
- * 转载请注明出处
- */
- public interface TemplateEngine {
- void run(Map context) throws Exception;
- }
/**
*
* @author 张荣华
* 转载请注明出处
*/
public interface TemplateEngine {
void run(Map context)throws Exception;
}
模板引擎的实现使用method template模式,因为有两个实现,这两个实现又存在公共的逻辑,所以选择了这个模式
- /**
- *
- * @author 张荣华
- * 转载请注明出处
- */
- public abstract class AbstractTemplateEngine implements TemplateEngine{
- public abstract String getTemplatePath();
- public abstract String getTemplate();
- public abstract String getEngineType();
- public void run(Map context) throws Exception{
- if (Constants.ENGINE_TYPE_FREEMARKER.equals(getEngineType()))
- executeFreemarker(context);
- else
- executeVelocity(context);
- }
- private void executeFreemarker(Map context) throws Exception{
- Configuration cfg = new Configuration();
- cfg.setDirectoryForTemplateLoading(
- new File(getTemplatePath()));
- cfg.setObjectWrapper( new DefaultObjectWrapper());
- cfg.setCacheStorage( new freemarker.cache.MruCacheStorage( 20 , 250 ));
- Template temp = cfg.getTemplate(getTemplate());
- Writer out = new OutputStreamWriter(System.out);
- temp.process(context, out);
- out.flush();
- }
- private void executeVelocity(Map root) throws Exception{
- Velocity .init();
- VelocityContext context = new VelocityContext(root);
- org.apache.velocity .Template template = null ;
- template = Velocity .getTemplate(getTemplatePath()+getTemplate());
- StringWriter sw = new StringWriter();
- template.merge( context, sw );
- System.out.print(sw.toString());
- }
- }
/**
*
* @author 张荣华
* 转载请注明出处
*/
public abstract class AbstractTemplateEngine implements TemplateEngine{
public abstract String getTemplatePath();
public abstract String getTemplate();
public abstract String getEngineType();
public void run(Map context)throws Exception{
if(Constants.ENGINE_TYPE_FREEMARKER.equals(getEngineType()))
executeFreemarker(context);
else
executeVelocity(context);
}
private void executeFreemarker(Map context)throws Exception{
Configuration cfg = new Configuration();
cfg.setDirectoryForTemplateLoading(
new File(getTemplatePath()));
cfg.setObjectWrapper(new DefaultObjectWrapper());
cfg.setCacheStorage(new freemarker.cache.MruCacheStorage(20, 250));
Template temp = cfg.getTemplate(getTemplate());
Writer out = new OutputStreamWriter(System.out);
temp.process(context, out);
out.flush();
}
private void executeVelocity(Map root)throws Exception{
Velocity
velocity
Velocity
.init();
VelocityContext context = new VelocityContext(root);
org.apache.
.Template template = null;
template =
.getTemplate(getTemplatePath()+getTemplate());
StringWriter sw = new StringWriter();
template.merge( context, sw );
System.out.print(sw.toString());
}
}
这个是freemarker实现
- /**
- *
- * @author 张荣华
- * 转载请注明出处
- */
- public class FreemarkerTemplateEngine extends AbstractTemplateEngine{
- private static final String DEFAULT_TEMPLATE = "FreemarkerExample.ftl" ;
- /**
- * 这个方法应该实现的是读取配置文件
- */
- public String getTemplatePath() {
- return null ;
- }
- public void run(Map root) throws Exception{
- super .run(root);
- }
- public String getTemplate() {
- // TODO Auto-generated method stub
- return DEFAULT_TEMPLATE;
- }
- public String getEngineType() {
- return Constants.ENGINE_TYPE_FREEMARKER;
- }
- }
/**
*
* @author 张荣华
* 转载请注明出处
*/
public class FreemarkerTemplateEngine extends AbstractTemplateEngine{
private static final String DEFAULT_TEMPLATE = "FreemarkerExample.ftl";
/**
* 这个方法应该实现的是读取配置文件
*/
public String getTemplatePath() {
return null;
}
public void run(Map root) throws Exception{
super.run(root);
}
public String getTemplate() {
// TODO Auto-generated method stub
return DEFAULT_TEMPLATE;
}
public String getEngineType() {
return Constants.ENGINE_TYPE_FREEMARKER;
}
}
这个是velocity 实现
- /**
- *
- * @author 张荣华
- * 转载请注明出处
- */
- public class VelocityTemplateEngine extends AbstractTemplateEngine{
- private static final String DEFAULT_TEMPLATE = "VelocityExample.vm" ;
- public String getTemplatePath() {
- return "/template/" ;
- }
- public void run(Map map) throws Exception{
- super .run(map);
- }
- public String getTemplate() {
- // TODO Auto-generated method stub
- return DEFAULT_TEMPLATE;
- }
- public String getEngineType() {
- // TODO Auto-generated method stub
- return Constants.ENGINE_TYPE_VELOCITY;
- }
- }
/**
*
* @author 张荣华
* 转载请注明出处
*/
public class VelocityTemplateEngine extends AbstractTemplateEngine{
private static final String DEFAULT_TEMPLATE = "VelocityExample.vm";
public String getTemplatePath() {
return "/template/";
}
public void run(Map map) throws Exception{
super.run(map);
}
public String getTemplate() {
// TODO Auto-generated method stub
return DEFAULT_TEMPLATE;
}
public String getEngineType() {
// TODO Auto-generated method stub
return Constants.ENGINE_TYPE_VELOCITY;
}
}
以下是模板
1,freemarker模板
- freemarker template test:
- string test-----------${user}-----------${number}-----------${latestProduct.url}-----------${latestProduct.name}
- condition test-----------
- <# if user == "Big Joe" >
- list iterator-----------
- <#list list as aa>
- ${aa}
- </#list>
- </# if >
- date test-----------${date?string( "MMM/dd/yyyy" )}
freemarker template test:
string test-----------${user}-----------${number}-----------${latestProduct.url}-----------${latestProduct.name}
condition test-----------
<#if user == "Big Joe">
list iterator-----------
<#list list as aa>
${aa}
</#list>
</#if>
date test-----------${date?string("MMM/dd/yyyy")}
2,velocity
模板
- ******************************************************************************************************************
- velocity template test:
- string test-----------${user}-----------${number}-----------${latestProduct.url}-----------${latestProduct.name}
- condition test-----------
- # if ($user == "Big Joe" )
- list iterator-----------
- #foreach( $aa in $list )
- $aa
- #end
- #end
- date test-----------${date}
******************************************************************************************************************
velocity
template test:
string test-----------${user}-----------${number}-----------${latestProduct.url}-----------${latestProduct.name}
condition test-----------
#if ($user == "Big Joe")
list iterator-----------
#foreach( $aa in $list )
$aa
#end
#end
date test-----------${date}
至此整个例子就结束了,以上只是最简单的介绍,当然这两种技术还有待我们的深入研究。这个例子只不过是比较直观的表现两种技术的使用而已
而且如果想学习方法模板模式和工厂模式的同学可以下载代码看看
作者:张荣华,未经作者同意不得随意转载!
发表评论
-
配置 weblogic JMS 连接
2011-05-11 19:25 4562jms 的优点 它可以把不影响用户执行结果又比较耗时的任务 ... -
应用服务器设为服务的方法
2011-01-05 11:03 9261.将Tomcat 5以上版本设置成服务 (假设我们缺省的T ... -
java 生成exe 文件
2010-03-15 11:25 6850对于作Java桌面应 ... -
linux 下jdk ,jboss的安装和jboss的自启动
2009-09-15 13:09 2503A。 linux下 JDK的安装 ... -
如何用java远程访问Domino邮箱数据2
2009-08-14 16:54 1537遍历邮箱数据库的所有 Document : ... -
Java访问Domino(Java,Domino,diiop,远程访问)
2009-08-14 16:17 11734一、概述 Java 对 Domino Obje ... -
如何用java远程访问Domino邮箱数据1
2009-08-14 16:16 3073应用场景 我们需 ... -
实战Concurrent
2009-04-23 12:41 832DigitalSonic 写道 编写 ... -
httpclient 模拟登陆网站 获取网站内容程序
2009-03-22 08:24 3268package org.apache.http.example ... -
JAVA-用HttpClient来模拟浏览器GET,POST2
2009-03-20 00:23 28885. 提交XML格式参数提交XML格式的参数很简单,仅仅是一 ... -
JAVA-用HttpClient来模拟浏览器GET,POST1
2009-03-20 00:21 1157一般的情况下我们都是使用IE或者Navigator浏览器来访问 ...
相关推荐
模板:velocity和freemarker的比较模板:velocity和freemarker的比较
jsp、freemarker、velocity简介和对比。---如果你已经开发Java Web应用程序一段时间,那么对于 JVM 的 PermGen 问题可能并不陌生。由于 FreeMarker 模板不编译成类,它们不占用 PermGen 空间,并不需要一个新的类...
NULL 博文链接:https://dada-fangfang.iteye.com/blog/1622934
二、FreeMarker与JSP、Velocity的对比 三、一个简单的FreeMarker Demo 四、FreeMarker的数据模型 五、模板的常用指令 六.常用的FTL标记 七、内建函数: 八、FreeMarker macro(宏)的使用 九、通过Struts2设置type来...
而目前开源的组件中,Itext的确是一个First Choice,如果各位单纯是做把图片转成PDF或者自己写了Velocity或者FreeMarker模板生成了HTML是非常推荐直接用Itext来进行的。而如果,大家像我这样已经有前人写好了HTML...
velocity使用文档,系统了解velocity模板技术,系统讲述velocity的使用,可以结合freemarker的思想学习这种视图模板技术
SpringMVC模板, SpringMVC与Velocity,Freemarker
像是velocity freemarker 这样的工具,还是重了一些,在消息通知、短信这类特定场景中,可能需要更简单的方式。这是个人写的一个模板工具,方便快捷
UAP基于FIS3,封装以JAVA为后端,velocity、JSP、FreeMarker为模板的UAP前端解决方案安装npm install fis3 uap -g使用开发 uap release生产 uap release prod
1.freemarker是一个用Java开发的模板...常用的java模板引擎还有 jsp、Freemarker、Thymeleaf、Velocity等 2.模板+数据模型=输出 freemarker并不关心数据的来源,只是根据模板的内容,将数据模型在模板中显示并输出文件
松哥的书里边,其实有涉及到 Vue,但是并没有详细说过,原因很简单,Vue ... 后端模板:Jsp、FreeMarker、Velocity 前端模板:Thymeleaf 前后端不分,Jsp 是一个非常典型写法,Jsp 将 HTML 和 Java 代码结合在一起,刚
大家常用的基于模板的生成工具,可能包括FreeMarker ,velocity等,语法结构都类似且简单,上手非常快,一个下午足矣。 相比较 FreeMarker 而言,Velocity 更加简单、轻量级,但它的功能却没有 FreeMarker 那么...
1.4 合并模板和数据模型 .................................................................................. 64 1.5将代码放在一起 ............................................................................
Freemaker是一个强大的模板引擎,相比velocity而言,其强大的过程调用、递归和闭包回调功能让freemaker可以完成几乎所有我们所想的功能。从个人看法而言,freemaker完全有能力作为MDA的代码辅助生成工具。 <br>
Struts2默认支持的模板Freemarker. FreeMarker允许Java servlet保持图形设计同应用程序逻辑的分离,这是通过在模板中密封HTML完成的。模板用servlet提供的数据动态地生成 HTML。模板语言是强大的直观的,编译器速度...
hymeleaf提供了一个用于整合Spring MVC的可选模块,在应用开发中,你可以使用Thymeleaf来完全代替JSP,或其他模板引擎,如Velocity、FreeMarker等。Thymeleaf的主要目标在于提供一种可被浏览器正确显示的、格式良好...
Spring In Action(第二版)中文版_笔记 by uuwoxin ...该模块提供了很多企业及服务,如电子邮件服务、JNDI访问、EJB集成、远程调用以及定时服务,并且支持与模板框架(如Velocity和FreeMarker)的集成。
Struts2标签库提供了主题、模板支持,极大地简化了视图页面的编写,而且,struts2的主题、模板都提供了很好的扩展性。实现了更好的代码复用。...包括最常用的jsp页面,也可以说Velocity和FreeMarker等模板技术中的使用
2、模板语法类似于:FreeMarker,Velocity,Smarty 3、采用简易的语言来描述大段的字串以及Dom/DHTML操作 4、可以很方便的解析XML文件格式的数据到指定模板。 采用该引擎,可以让它来完全处理View方面的事情,服务端...
FreeMarker 进行了比较深入的阐述。有助于读者很好的了解其运作机制,以及去理解其他 模板引擎(如Velocity)的工作机理。 注:由于原文档部分内容直译可能难于被读者理解,所以有些地方采用意译为主,因此在翻译...