- 浏览: 203636 次
- 性别:
- 来自: 南京
-
文章分类
最新评论
-
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 4659jms 的优点 它可以把不影响用户执行结果又比较耗时的任务 ... -
应用服务器设为服务的方法
2011-01-05 11:03 9771.将Tomcat 5以上版本设置成服务 (假设我们缺省的T ... -
java 生成exe 文件
2010-03-15 11:25 6913对于作Java桌面应 ... -
linux 下jdk ,jboss的安装和jboss的自启动
2009-09-15 13:09 2547A。 linux下 JDK的安装 ... -
如何用java远程访问Domino邮箱数据2
2009-08-14 16:54 1583遍历邮箱数据库的所有 Document : ... -
Java访问Domino(Java,Domino,diiop,远程访问)
2009-08-14 16:17 11883一、概述 Java 对 Domino Obje ... -
如何用java远程访问Domino邮箱数据1
2009-08-14 16:16 3123应用场景 我们需 ... -
实战Concurrent
2009-04-23 12:41 868DigitalSonic 写道 编写 ... -
httpclient 模拟登陆网站 获取网站内容程序
2009-03-22 08:24 3312package org.apache.http.example ... -
JAVA-用HttpClient来模拟浏览器GET,POST2
2009-03-20 00:23 29395. 提交XML格式参数提交XML格式的参数很简单,仅仅是一 ... -
JAVA-用HttpClient来模拟浏览器GET,POST1
2009-03-20 00:21 1196一般的情况下我们都是使用IE或者Navigator浏览器来访问 ...
相关推荐
项目已获导师指导并通过的高分毕业设计项目,可作为课程设计和期末大作业,下载即用无需修改,项目完整确保可以运行。 包含:项目源码、数据库脚本、软件工具等,该项目可以作为毕设、课程设计使用,前后端代码都在里面。 该系统功能完善、界面美观、操作简单、功能齐全、管理便捷,具有很高的实际应用价值。 项目都经过严格调试,确保可以运行!可以放心下载 技术组成 语言:java 开发环境:idea 数据库:MySql5.7以上 部署环境:maven 数据库工具:navicat
YunSDR通信小课堂(第15讲).mhtml
【前端】是基于salvo和rbatis的rbac权限管理系统的前端项目
DeepSeek_V3技术报告译文版,翻译学习版,简单易懂;
分享一个ArcGIS 二调符号库(部标准)
【断点续传】FTP断点续传搭建_pgj
木块识别数据集,正确识别率95.7%,yolov5pytorch格式标注(可用于统计木块数量)
三相LCL型并网逆变器:电容电流反馈与电网电压全前馈的优化控制策略及低次谐波抑制技术,三相LCL型并网逆变器:电容电流反馈与全前馈电网电压控制策略的优化与谐波抑制研究,三相lcl型并网逆变器控制策略 电容电流反馈和电网电压全前馈,加入5.7.11.13次谐波thd<5。 相关方面电力电气工程,电子信息工程等等都可以。 ,三相lcl型并网逆变器控制策略; 谐波thd; 电网电压全前馈; 电容电流反馈; 电力电气工程; 电子信息工程,基于谐波优化的三相LCL型并网逆变器控制策略研究
基于SSA-RF算法优化的多变量时间序列预测:交叉验证抑制过拟合的Matlab代码实现,"基于SSA-RF优化的多变量时间序列预测及过拟合抑制:MATLAB代码实践",基于麻雀搜索算法优化随机森林(SSA-RF)的多变量时间序列预测 SSA-RF多变量时间序列 采用交叉验证抑制过拟合问题 matlab代码, 注:暂无Matlab版本要求 -- 推荐 2018B 版本及以上 注:采用 RF 工具箱(无需安装,可直接运行),仅支持 Windows 64位系统 ,SSA-RF; 麻雀搜索算法; 随机森林; 多变量时间序列预测; 交叉验证; 过拟合抑制; MATLAB代码; Windows 64位系统,"基于SSA-RF优化的多变量时间序列预测的Matlab交叉验证实现"
DeepSeek本地部署AI对话网页版
【毕业设计】基于VpnService的Android抓包与防火墙的实现_pgj
DeepSeek 使用技巧,强烈建议收藏.docx
逐项优化进阶:水果图像分割
使用Visual Studio搭建C++20开发环境
三相同步旋转锁相环PSIM仿真,以及相关技术文件代码
项目已获导师指导并通过的高分毕业设计项目,可作为课程设计和期末大作业,下载即用无需修改,项目完整确保可以运行。 包含:项目源码、数据库脚本、软件工具等,该项目可以作为毕设、课程设计使用,前后端代码都在里面。 该系统功能完善、界面美观、操作简单、功能齐全、管理便捷,具有很高的实际应用价值。 项目都经过严格调试,确保可以运行!可以放心下载 技术组成 语言:java 开发环境:idea 数据库:MySql5.7以上 部署环境:maven 数据库工具:navicat
之前全套打包的,也忘记有没有教程了,反正搭建挺简单的,先替换域名再搭建就好了,,不会的可以来问我,但是代搭建需要收辛苦费。 注意:这是系统源码,授权站+主站的。完整版的,之前拿来卖的,现在拼多多我也在卖,保证可用,因为之前都是给客户一条龙搭建好的,也忘记有没有教程了,多研究研究就好了。
内容概要:本文提出了一种针对带正交加强件(纵向筋条与环形肋)的层合材料圆柱壳振动声特性的半解析方法。该模型采用一阶剪切变形理论结合Lekhnitsky分散加强筋技术和光谱边界元法进行计算,并引入了主要结构与子段的概念以应对复杂壳体建模问题。文中利用傅里叶级数与勒让德多项式作为容许函数,在不同外部流体条件、夹层层法和加固件的数量及形状变化下进行了数值验证,并展示了模型对振动声性能预测的能力。 适合人群:机械工程领域的研究人员和技术人员,特别是从事复合材料及船舶海洋工程方向的人士。 使用场景及目标:用于精确模拟和评估带有不同加强结构的复合圆柱壳体在浸没情况下的振动响应与辐射噪声行为,辅助优化设计方案并提高产品性能。 其他说明:本研究成果为理解网格强化叠层结构提供了新的视角,并强调了此类材料组合应用于实际工程项目时所需关注的关键因素。此外还对比了一些文献数据来证明所提方法的有效性和准确性。
【毕业设计】关于新浪微博API的Java编程._pgj
第三方热狗如果个胜多负少