XDoclet簡化Struts開發

來源:互聯網
上載者:User

Struts是一個Web開發架構,是用Struts避免了簡單的JSP + Servlet開發過程,維護過程中的一系列問題,但是struts設定檔的編輯始終是一個問題。下面我們使用Xdoclet來自動產生struts設定檔。Xdoclet是一個使用Java代碼中的注釋來產生介面檔案,或者設定檔的開源工具。


所有得Struts的各組件關係如上所示,其中有兩個主要類,DispatchAction和DispatchValueBean。DispatchAction從上個頁面獲得輸入,根據輸入定位到不同的頁面(0定位到dispatch_0.jsp,1定位dispatch_1.jsp)。

可以看看下列代碼(只涉及到Xdoclet相關的部分):

//DispatchValueBean.java

/**

 *

 * @author mazhao

 * @struts.form

 *    name="DispatchValueBean"

 */

public class DispatchValueBean extends org.apache.struts.action.ActionForm {

 

    private String dispatchValue = "0";

    public DispatchValueBean () {

    }

 

    public String getDispatchValue()

    {

        return dispatchValue;

    }

    public void setDispatchValue(String dispatchValue)

    {

        this.dispatchValue = dispatchValue;

    }

}

上述的藍色代碼錶示自己是一個FormBean,且FormBean的名字是DispatchValueBean。

 

//DispatchAction.java

/**

 *

 * @author mazhao

 * @struts.action

 *   name="DispatchValueBean"

 *   path="/DispatchAction.do"

 *   input="/index.jsp"

 *

 * @struts.action-forward

 *   name="dispatch_0"

 *   path="/dispatch_0.jsp"

 *   redirect="false"

 *

 * @struts.action-forward

 *   name="dispatch_1"

 *   path="/dispatch_1.jsp"

 *   redirect="false"

 *

 */

public class DispatchAction extends org.apache.struts.action.Action {

   

    private String dispatchValue = "0";

   

   

    public DispatchAction() {

    }

   

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

        DispatchValueBean dispatchBean = (DispatchValueBean)form;

        String value = dispatchBean.getDispatchValue();

        if("0".equals(value))

        {

            return mapping.findForward("dispatch_0");

        }

        else

        {

            return mapping.findForward("dispatch_1");

        }

    }

 

    public String getDispatchValue()

    {

        return dispatchValue;

    }

    public void setDispatchValue(String dispatchValue)

    {

        this.dispatchValue = dispatchValue;

    }

}

上述的藍色代碼說明該Action所使用的FormBean,輸入頁面,path和不同的ActionForward。

根據如上的代碼可以使用如下的build檔案來自動產生struts-config.xml:

<!—- 將xdoclet的jar檔案包含到編譯路徑中 -->

<path id="compile.classpath">

<fileset dir="${xdoclet.dir}">

<include name="*.jar"/>

</fileset>

</path>

 

<!—-定義子定義的任務標籤 -->

<taskdef

name="webdoclet"

classname="xdoclet.modules.web.WebDocletTask"

classpathref="compile.classpath"

        />

 

<!—使用自訂的任務標籤產生struts-config.xml檔案-->

<target

name="webdoclet"

depends="prepare"

description="Generate deployment descriptors (run actionform to generate forms first)">        <echo>+---------------------------------------------------+</echo>

<echo>|                                                            |</echo>

<echo>| R U N N I N G   W E B D O C L E T                     |</echo>

<echo>|                                                                 |</echo>

<echo>+---------------------------------------------------+</echo>

<webdoclet

destdir="ant"

mergedir="ant/merge"

       verbose="false"

>

<fileset dir="JavaSource">

                             <include name="**/*Action.java"/>

                              <include name="**/*Bean.java"/>

</fileset>

                      <strutsconfigxml

                             destdir="ant"

                              />

</webdoclet>

</target>

建議在詳細設計階段使用這種方式產生代碼架構和struts-config.xml設定檔。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.