模板技术是一项非常强大的技术,较之于传统Struts等以推JSP为主的框架,模板技术更加灵活,应用范围更加广泛,你不仅仅可以用来生成Web页面,凡是文本性质的东西,都可以通过模板机制来处理,比如生成Java源代码、配置文件等。模板技术在在其它一些动态语言如PHP、Ruby等得到大量应用,而近年来备受热棒的Rails等框架的页面处理也是基于模板机制,另外我们每天使用的Eclipse工具中,也大量使用到了模板技术,比如自定义代码块等功能。EasyJF开源的Web MVC框架EasyJWeb中的页面输出正是采用模板机制。
模板机制的原理其实比较简单,其实就是准备好一个模板,在模板中添加一些用于可替换的特殊标志,然后在用户使用通过模板引擎,把准备好的数据与模板进行合并处理,就能生成得到我们所期望的内容。
比如,我们有一个html模板内容如下,其中黑体部分是模板的特殊标志,用来作数据替换的:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>html模板</title>
</head>
<body>
<h1>${title}</h1>
<p>${date}</p>
</body>
</html>
<head>
<meta http-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()
则就会输出类似下面的内容:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>html模板</title>
</head>
<body>
<h1>新闻标题</h1>
<p>Thu Nov 29 20:45:38 CST 2007</p>
</body>
</html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>html模板</title>
</head>
<body>
<h1>新闻标题</h1>
<p>Thu Nov 29 20:45:38 CST 2007</p>
</body>
</html>
通过使用模板技术,你可以用EasyJWeb来生成java代码,比如在EasyJWeb代码生成引擎中的一个生成业务接口的模板内容如下:
public class $!...{domainName}ServiceImpl implements I$!...{domainName}Service...{
private I$!...{domainName}DAO $!...{domain}Dao;
public void set#upperCase($!...{domain})Dao(I$!...{domainName}DAO $!...{domain}Dao)...{
this.$!...{domain}Dao=$!...{domain}Dao;
}
public Long add$!...{domainName}($!...{domainName} $!...{domain}) ...{
this.$!...{domain}Dao.save($!...{domain});
if ($!...{domain} != null && $!...{domain}.get$!...{id}() != null) ...{
return $!...{domain}.get$!...{id}();
}
return null;
}
public $!...{domainName} get$!...{domainName}(Long id) ...{
$!...{domainName} $!...{domain} = this.$!...{domain}Dao.get(id);
return $!...{domain};
}
public boolean del$!...{domainName}(Long id) ...{
$!...{domainName} $!...{domain} = this.get$!...{domainName}(id);
if ($!...{domain} != null) ...{
this.$!...{domain}Dao.remove(id);
return true;
}
return false;
}
public boolean batchDel$!...{domainName}s(List<Serializable> $!...{domain}Ids) ...{
for (Serializable id : $!...{domain}Ids) ...{
del$!...{domainName}((Long) id);
}
return true;
}
public IPageList get$!...{domainName}By(IQueryObject queryObject) ...{
return QueryUtil.query(queryObject, $!...{domainName}.class,this.$!...{domain}Dao);
}
public boolean update$!...{domainName}(Long id, $!...{domainName} $!...{domain}) ...{
if (id != null) ...{
$!...{domain}.set$!...{id}(id);
} else ...{
return false;
}
this.$!...{domain}Dao.update($!...{domain});
return true;
}
}
private I$!...{domainName}DAO $!...{domain}Dao;
public void set#upperCase($!...{domain})Dao(I$!...{domainName}DAO $!...{domain}Dao)...{
this.$!...{domain}Dao=$!...{domain}Dao;
}
public Long add$!...{domainName}($!...{domainName} $!...{domain}) ...{
this.$!...{domain}Dao.save($!...{domain});
if ($!...{domain} != null && $!...{domain}.get$!...{id}() != null) ...{
return $!...{domain}.get$!...{id}();
}
return null;
}
public $!...{domainName} get$!...{domainName}(Long id) ...{
$!...{domainName} $!...{domain} = this.$!...{domain}Dao.get(id);
return $!...{domain};
}
public boolean del$!...{domainName}(Long id) ...{
$!...{domainName} $!...{domain} = this.get$!...{domainName}(id);
if ($!...{domain} != null) ...{
this.$!...{domain}Dao.remove(id);
return true;
}
return false;
}
public boolean batchDel$!...{domainName}s(List<Serializable> $!...{domain}Ids) ...{
for (Serializable id : $!...{domain}Ids) ...{
del$!...{domainName}((Long) id);
}
return true;
}
public IPageList get$!...{domainName}By(IQueryObject queryObject) ...{
return QueryUtil.query(queryObject, $!...{domainName}.class,this.$!...{domain}Dao);
}
public boolean update$!...{domainName}(Long id, $!...{domainName} $!...{domain}) ...{
if (id != null) ...{
$!...{domain}.set$!...{id}(id);
} else ...{
return false;
}
this.$!...{domain}Dao.update($!...{domain});
return true;
}
}
你只需传送一个具有id的域对象作为参数,添加到结果集Result中,比如我们传送一个名为Orders的域模型类,就会得到如下的输出:
public class OrdersServiceImpl implements IOrdersService ...{
private IOrdersDAO ordersDao;
public void setOrdersDao(IOrdersDAO ordersDao) ...{
this.ordersDao = ordersDao;
}
private IOrdersDAO ordersDao;
public void setOrdersDao(IOrdersDAO ordersDao) ...{
this.ordersDao = ordersDao;
}