XDoclet简化Struts开发

Struts是一个Web开发框架,是用Struts避免了简单的JSP + Servlet开发过程,维护过程中的一系列问题,但是struts配置文件的编辑始终是一个问题。下面我们使用Xdoclet来自动生成struts配置文件。Xdoclet是一个使用Java代码中的注释来生成接口文件,或者配置文件的开源工具。

<shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"></shapetype><stroke joinstyle="miter"></stroke><formulas></formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f><lock v:ext="edit" aspectratio="t"></lock><shape id="_x0000_i1025" type="#_x0000_t75" o:borderrightcolor="this" o:borderbottomcolor="this" o:borderleftcolor="this" o:bordertopcolor="this"></shape><imagedata src="file:///C:/DOCUME~1/mazhao/LOCALS~1/Temp/msoclip1/01/clip_image001.jpg" o:title="struts-dispatch-dia"></imagedata><bordertop type="single" width="4"></bordertop><borderleft type="single" width="4"></borderleft><borderbottom type="single" width="4"></borderbottom><borderright type="single" width="4"></borderright>

所有得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配置文件。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值