<iframe align="top" marginwidth="0" marginheight="0" src="http://www.zealware.com/csdnblog01.html" frameborder="0" width="728" scrolling="no" height="90"></iframe>
模板技术是一项非常强大的技术,较之于传统Struts等以推JSP为主的框架,模板技术更加灵活,应用范围更加广泛,你不仅仅可以用来生成Web页面,凡是文本性质的东西,都可以通过模板机制来处理,比如生成Java源代码、配置文件等。模板技术在在其它一些动态语言如PHP、Ruby等得到大量应用,而近年来备受热棒的Rails等框架的页面处理也是基于模板机制,另外我们每天使用的Eclipse工具中,也大量使用到了模板技术,比如自定义代码块等功能。EasyJF开源的Web MVC框架EasyJWeb中的页面输出正是采用模板机制。
模板机制的原理其实比较简单,其实就是准备好一个模板,在模板中添加一些用于可替换的特殊标志,然后在用户使用通过模板引擎,把准备好的数据与模板进行合并处理,就能生成得到我们所期望的内容。
比如,我们有一个html模板内容如下,其中黑体部分是模板的特殊标志,用来作数据替换的:
htmlxmlns="http://www.w3.org/1999/xhtml">
head>
metahttp-equiv="Content-Type"content="text/html;charset=gb2312"/>
title>html模板title>
head>
body>
h1>${title}h1>
p>${date}p>
body>
html>
head>
metahttp-equiv="Content-Type"content="text/html;charset=gb2312"/>
title>html模板title>
head>
body>
h1>${title}h1>
p>${date}p>
body>
html>
当模板引擎在处理的时候,他就会把特殊标志的部分换成具体的数据内容,比如我们如果给title及date分别如下的值:
title="
新闻标题
"
Date=
new
Date()
则就会输出类似下面的内容:
htmlxmlns="http://www.w3.org/1999/xhtml">
head>
metahttp-equiv="Content-Type"content="text/html;charset=UTF-8"/>
title>html模板title>
head>
body>
h1>新闻标题h1>
p>ThuNov2920:45:38CST2007p>
body>
html>
head>
metahttp-equiv="Content-Type"content="text/html;charset=UTF-8"/>
title>html模板title>
head>
body>
h1>新闻标题h1>
p>ThuNov2920:45:38CST2007p>
body>
html>
通过使用模板技术,你可以用EasyJWeb来生成java代码,比如在EasyJWeb代码生成引擎中的一个生成业务接口的模板内容如下:
public
class
$
!
...
{domainName}
ServiceImpl
implements
I$
!
...
{domainName}
Service
...
{
privateI$!...{domainName}DAO$!...{domain}Dao;
publicvoidset#upperCase($!...{domain})Dao(I$!...{domainName}DAO$!...{domain}Dao)...{
this.$!...{domain}Dao=$!...{domain}Dao;
}
publicLongadd$!...{domainName}($!...{domainName}$!...{domain})...{
this.$!...{domain}Dao.save($!...{domain});
if($!...{domain}!=null&&$!...{domain}.get$!...{id}()!=null)...{
return$!...{domain}.get$!...{id}();
}
returnnull;
}
public$!...{domainName}get$!...{domainName}(Longid)...{
$!...{domainName}$!...{domain}=this.$!...{domain}Dao.get(id);
return$!...{domain};
}
publicbooleandel$!...{domainName}(Longid)...{
$!...{domainName}$!...{domain}=this.get$!...{domainName}(id);
if($!...{domain}!=null)...{
this.$!...{domain}Dao.remove(id);
returntrue;
}
returnfalse;
}
publicbooleanbatchDel$!...{domainName}s(ListSerializable>$!...{domain}Ids)...{
for(Serializableid:$!...{domain}Ids)...{
del$!...{domainName}((Long)id);
}
returntrue;
}
publicIPageListget$!...{domainName}By(IQueryObjectqueryObject)...{
returnQueryUtil.query(queryObject,$!...{domainName}.class,this.$!...{domain}Dao);
}
publicbooleanupdate$!...{domainName}(Longid,$!...{domainName}$!...{domain})...{
if(id!=null)...{
$!...{domain}.set$!...{id}(id);
}else...{
returnfalse;
}
this.$!...{domain}Dao.update($!...{domain});
returntrue;
}
}
privateI$!...{domainName}DAO$!...{domain}Dao;
publicvoidset#upperCase($!...{domain})Dao(I$!...{domainName}DAO$!...{domain}Dao)...{
this.$!...{domain}Dao=$!...{domain}Dao;
}
publicLongadd$!...{domainName}($!...{domainName}$!...{domain})...{
this.$!...{domain}Dao.save($!...{domain});
if($!...{domain}!=null&&$!...{domain}.get$!...{id}()!=null)...{
return$!...{domain}.get$!...{id}();
}
returnnull;
}
public$!...{domainName}get$!...{domainName}(Longid)...{
$!...{domainName}$!...{domain}=this.$!...{domain}Dao.get(id);
return$!...{domain};
}
publicbooleandel$!...{domainName}(Longid)...{
$!...{domainName}$!...{domain}=this.get$!...{domainName}(id);
if($!...{domain}!=null)...{
this.$!...{domain}Dao.remove(id);
returntrue;
}
returnfalse;
}
publicbooleanbatchDel$!...{domainName}s(ListSerializable>$!...{domain}Ids)...{
for(Serializableid:$!...{domain}Ids)...{
del$!...{domainName}((Long)id);
}
returntrue;
}
publicIPageListget$!...{domainName}By(IQueryObjectqueryObject)...{
returnQueryUtil.query(queryObject,$!...{domainName}.class,this.$!...{domain}Dao);
}
publicbooleanupdate$!...{domainName}(Longid,$!...{domainName}$!...{domain})...{
if(id!=null)...{
$!...{domain}.set$!...{id}(id);
}else...{
returnfalse;
}
this.$!...{domain}Dao.update($!...{domain});
returntrue;
}
}
你只需传送一个具有id的域对象作为参数,添加到结果集Result中,比如我们传送一个名为Orders的域模型类,就会得到如下的输出:
public
class
OrdersServiceImpl
implements
IOrdersService
...
{
privateIOrdersDAOordersDao;
publicvoidsetOrdersDao(IOrdersDAOordersDao)...{
this.ordersDao=ordersDao;
}
publicLongaddOrders(Ordersorders)...{
this.ordersDao.save(orders);
if(orders!=null&&orders.getId()!=null)...{
returnorders.getId();
}
returnnull;
}
publicOrdersgetOrders(Longid)...{
Ordersorders=this.ordersDao.get(id);
if(orders!=null)...{
returnorders;
}
returnnull;
}
publicbooleandelOrders(Longid)...{
Ordersorders=this.getOrders(id);
if(orders!=null)...{
this.ordersDao.remove(id);
returntrue;
}
returnfalse;
}
publicbooleanbatchDelOrderss(ListSerializable>ordersIds)...{
for(Serializableid:ordersIds)...{
delOrders((Long)id);
}
returntrue;
}
publicbooleanremoveOrders(Longid)...{
Ordersorders=this.getOrders(id);
if(orders!=null)...{
this.ordersDao.remove(id);
returntrue;
}
returnfalse;
}
publicboo
privateIOrdersDAOordersDao;
publicvoidsetOrdersDao(IOrdersDAOordersDao)...{
this.ordersDao=ordersDao;
}
publicLongaddOrders(Ordersorders)...{
this.ordersDao.save(orders);
if(orders!=null&&orders.getId()!=null)...{
returnorders.getId();
}
returnnull;
}
publicOrdersgetOrders(Longid)...{
Ordersorders=this.ordersDao.get(id);
if(orders!=null)...{
returnorders;
}
returnnull;
}
publicbooleandelOrders(Longid)...{
Ordersorders=this.getOrders(id);
if(orders!=null)...{
this.ordersDao.remove(id);
returntrue;
}
returnfalse;
}
publicbooleanbatchDelOrderss(ListSerializable>ordersIds)...{
for(Serializableid:ordersIds)...{
delOrders((Long)id);
}
returntrue;
}
publicbooleanremoveOrders(Longid)...{
Ordersorders=this.getOrders(id);
if(orders!=null)...{
this.ordersDao.remove(id);
returntrue;
}
returnfalse;
}
publicboo