● xdoclet學(xué)習(xí)續(xù)
Xdoclet是一個(gè)代碼自動(dòng)生成的工具
XDoclet任務(wù)就是Ant的自定義任務(wù),除此以外,沒有其他運(yùn)行XDoclet任務(wù)的方法。
XDoclet它有兩個(gè)重要的組件:
進(jìn)行特殊標(biāo)記的 Java 源文件。
預(yù)先定義的模板。[引用]
Merge File用來處理無法在Source Code中加xdoclet tag的情況。
? XDoclet中的核心任務(wù):
<ejbdoclet>:面向EJB領(lǐng)域,生成EJB、工具類和布署描述符。
<webdoclet>:面向Web開發(fā),生成serlvet、自定義標(biāo)簽庫和web框架文件。
<hibernatedoclet>:Hibernate持續(xù),配置文件、Mbeans
<jdodoclet>:JDO,元數(shù)據(jù),vender configuration
<jmxdoclet>:JMX,MBean接口,mlets,配置文件。
<doclet>:使用用戶自定義模板來生成代碼。
<documentdoclet>:生成項(xiàng)目文件(例如todo列報(bào)表)
? webdoclet sub task
XDoclet并沒有和Ant一起發(fā)布,所以如果你想要使用XDoclet的話,就需要單獨(dú)的下載和
安裝。在使用任何一個(gè)XDoclet的任務(wù)之前,你首先需要在使用Ant的<taskdef>任務(wù)來聲
明它。
<deploymentdescriptor>:產(chǎn)生標(biāo)準(zhǔn)的web引用配置文件web.xml,destdir屬性設(shè)定
web.xml文件的存放位置。
XDoclet通過合并點(diǎn)(merge points)支持定制,合并點(diǎn)是在模板文件定義里允許運(yùn)行時(shí)插入定制代碼的地方,使用mergedir屬性設(shè)置。
<target name="webdoclet" depends="compile-web"
unless="webdoclet.unnecessary"
description="Generate web and Struts descriptors">
<taskdef name="webdoclet" classname="xdoclet.modules.web.WebDocletTask">
<classpath>
<path refid="xdoclet.classpath"/>
<path refid="web.compile.classpath"/>
</classpath>
</taskdef>
<!—mergedir完成模板文件合并的功能-->
<webdoclet destdir="${webapp.target}/WEB-INF"
force="${xdoclet.force}"
mergedir="metadata/web"
excludedtags="@version,@author"
verbose="true">
<!—找出目錄src/web 和${build.dir}/web/gen文件中的webdoclet注
釋,生成web.xml,struts-config.xml等配置信息文件-->
<fileset dir="src/web"/>
<fileset dir="${build.dir}/web/gen"/>
<!—生成的web.xml文件放在build/appName/WEB-INF/-->
<deploymentdescriptor validateXML="true"
servletspec="2.3" sessiontimeout="10"
destdir="${webapp.target}/WEB-INF" distributable="false"
displayname="${ant.project.name}">
<configParam name="httpPort" value="${http.port}"/>
<configParam name="httpsPort" value="${https.port}"/>
<configParam name="daoType" value="${dao.type}"/>
<configParam name="security" value="${security.mode}"/>
</deploymentdescriptor>
<jsptaglib validateXML="true"
description="Custom tag library for this application"
shortName="${webapp.name}" filename="${webapp.name}.tld"/>
<strutsconfigxml validateXML="true" version="1.2"/>
<strutsvalidationxml/>
</webdoclet>
</target>
圖表 1引用一張網(wǎng)友發(fā)表的非常直觀的使用說明圖
? XDoclet 中的合并點(diǎn)
在 XDoclet 的文檔中,您會(huì)非常頻繁地看到術(shù)語 合并點(diǎn)(merge point)和 合并文件(merge file)。合并文件是文本文件,您可以把它合并到 XDoclet 生成代碼的指定位置——“合并點(diǎn)”上(由模板指定)。可以用合并文件來包含靜態(tài)文本(例如代碼片斷和 XML 片斷),這些文本可能很難或者不能用 XDoclet 的能力生成。例如,在示例代碼的 metadata/web 目錄下,您會(huì)找到這些文件。在代碼生成期間,可以用到這些文件合并配置文件的一部分[引用]。
?XDoclet中的模板
XDoclet使用代碼模板來生成代碼。模板(template)是你想生成文件的原型。模板里使
用一些XML標(biāo)簽來指導(dǎo)模板引擎如何根據(jù)輸入類以及它們的元數(shù)據(jù)來調(diào)整代碼的生成。
模板有點(diǎn)像JSP文件。它們都包含文件和XML標(biāo)簽,生成輸出文件時(shí)XML標(biāo)簽會(huì)被解析,
然后生成文本并顯示在XML標(biāo)簽所處的位置上。除了以XDt為命名空間打頭的XML標(biāo)簽
會(huì)被XDoclet引擎解析以外,其余的XML標(biāo)簽XDoclet會(huì)忽略不管。
? AppFuse中生成Action類的XDoclet模板
public final class <XDtClass:className/>Action extends BaseAction {
public ActionForward cancel(ActionMapping mapping, ActionForm form,HttpServletRequest request,
HttpServletResponse response)
throws Exception {
return search(mapping, form, request, response);
}
public ActionForward delete(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
if (log.isDebugEnabled()) {
log.debug("Entering 'delete' method");
}
ActionMessages messages = new ActionMessages();
<XDtClass:className/>Form <XDtForm:classNameLower/>Form = (<XDtClass:className/>Form) form;
// Exceptions are caught by ActionExceptionHandler
Manager mgr = (Manager) getBean("manager");
mgr.removeObject(<XDtClass:className/>.class, new Long(<XDtForm:classNameLower/>Form.getId()));
messages.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage("<XDtForm:classNameLower/>.deleted"));
// save messages in session, so they'll survive the redirect
saveMessages(request.getSession(), messages);
return search(mapping, form, request, response);
}
… …
}
? AppFuse中自動(dòng)生成的對應(yīng)Action類
public final class PersonAction extends BaseAction {
public ActionForward cancel(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
return search(mapping, form, request, response);
}
public ActionForward delete(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
if (log.isDebugEnabled()) {
log.debug("Entering 'delete' method");
}
ActionMessages messages = new ActionMessages();
PersonForm personForm = (PersonForm) form;
// Exceptions are caught by ActionExceptionHandler
Manager mgr = (Manager) getBean("manager");
mgr.removeObject(Person.class, new Long(personForm.getId()));
messages.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage("person.deleted"));
// save messages in session, so they'll survive the redirect
saveMessages(request.getSession(), messages);
return search(mapping, form, request, response);
}
… …
}
posted on 2006-11-05 09:00
xzc 閱讀(446)
評論(0) 編輯 收藏 所屬分類:
Xdoclet