隨筆-314  評論-209  文章-0  trackbacks-0
          ● xdoclet學習續(xù)
            Xdoclet是一個代碼自動生成的工具
            XDoclet任務就是Ant的自定義任務,除此以外,沒有其他運行XDoclet任務的方法。
            XDoclet它有兩個重要的組件:
            進行特殊標記的 Java 源文件。
            預先定義的模板。[引用]
            
            
            Merge File用來處理無法在Source Code中加xdoclet tag的情況。
            
            ? XDoclet中的核心任務:
            <ejbdoclet>:面向EJB領域,生成EJB、工具類和布署描述符。
            <webdoclet>:面向Web開發(fā),生成serlvet、自定義標簽庫和web框架文件。
            <hibernatedoclet>:Hibernate持續(xù),配置文件、Mbeans
            <jdodoclet>:JDO,元數(shù)據(jù),vender configuration
            <jmxdoclet>:JMX,MBean接口,mlets,配置文件。
            <doclet>:使用用戶自定義模板來生成代碼。
            <documentdoclet>:生成項目文件(例如todo列報表)
            
            ? webdoclet sub task
            XDoclet并沒有和Ant一起發(fā)布,所以如果你想要使用XDoclet的話,就需要單獨的下載和
            安裝。在使用任何一個XDoclet的任務之前,你首先需要在使用Ant的<taskdef>任務來聲
            明它。
            <deploymentdescriptor>:產(chǎn)生標準的web引用配置文件web.xml,destdir屬性設定
            web.xml文件的存放位置。
            XDoclet通過合并點(merge points)支持定制,合并點是在模板文件定義里允許運行時插入定制代碼的地方,使用mergedir屬性設置。
             <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 中的合并點
            在 XDoclet 的文檔中,您會非常頻繁地看到術語 合并點(merge point)和 合并文件(merge file)。合并文件是文本文件,您可以把它合并到 XDoclet 生成代碼的指定位置——“合并點”上(由模板指定)。可以用合并文件來包含靜態(tài)文本(例如代碼片斷和 XML 片斷),這些文本可能很難或者不能用 XDoclet 的能力生成。例如,在示例代碼的 metadata/web 目錄下,您會找到這些文件。在代碼生成期間,可以用到這些文件合并配置文件的一部分[引用]。
            
            ?XDoclet中的模板
            XDoclet使用代碼模板來生成代碼。模板(template)是你想生成文件的原型。模板里使
            用一些XML標簽來指導模板引擎如何根據(jù)輸入類以及它們的元數(shù)據(jù)來調(diào)整代碼的生成。
            模板有點像JSP文件。它們都包含文件和XML標簽,生成輸出文件時XML標簽會被解析,
            然后生成文本并顯示在XML標簽所處的位置上。除了以XDt為命名空間打頭的XML標簽
            會被XDoclet引擎解析以外,其余的XML標簽XDoclet會忽略不管。
            ? 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中自動生成的對應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 閱讀(447) 評論(0)  編輯  收藏 所屬分類: Xdoclet
          主站蜘蛛池模板: 谢通门县| 肇州县| 调兵山市| 贵溪市| 晋中市| 黄平县| 普洱| 浦县| 京山县| 华池县| 三河市| 左云县| 浦江县| 中阳县| 凌源市| 星子县| 任丘市| 德惠市| 千阳县| 合山市| 长沙县| 河间市| 马龙县| 高要市| 太仓市| 离岛区| 长泰县| 榆林市| 鄂州市| 宣武区| 宜章县| 柞水县| 苍南县| 名山县| 天峻县| 博湖县| 成安县| 平遥县| 乌拉特中旗| 萨迦县| 九龙城区|