博客首页 注册 建议与交流 排行榜 加入友情链接         宝宝相册的专门空间
推荐 投诉 搜索: 帮助

IT民工窝棚

有意思的事很多,有多少有意义的呢?
   qbq.cublog.cn
关于作者  
姓名:QBQ
职业:PG
年龄:25
位置:DL
个性介绍:有意思的事很多,有多少有意义的呢?

我的分类  




DispatchAction、LookupDispatchAction、SwitchAction的应

DispatchAction

=======================  

org.apache.struts.actions.DispatchAction.java

DispatchAction的作用简单地说就是把原来我们写在多个acton里的操作放在同一个 action里处理。

举个例子就是如果在你的系统中有文章的管理操作,那么通常有以下操作:添加文章、察看文章、搜索文章等等,这样的话一般你会写三个action[ArtilceSaveAction ArticleViewAction ArticleSearchAction ]分别处理各个操作,虽然说这样看起来是非常清晰、流畅的操作,但是你会发现在三个action理由太多的相同的东西。现在利用DispatchAction,我们可以把“相似”的action放在一个action里操作。

下面以上边的三个action和到一个action里为例:

import ****;

import org.apache.struts.actions.DispatchAction;

public class ArticleAction extends DispatchAction{

    /**

    *AritcleAddAction

    */

    public ActionForward add(ActionMapping mapping,

                            ActionForm form,

                            HttpServletRequest request,

                            HttpServletResponse response)

          throws Exception {
          ...

          ...

          }         

    /**

    *AritcleViewAction

    */

    public ActionForward view(ActionMapping mapping,

                            ActionForm form,

                            HttpServletRequest request,

                            HttpServletResponse response)

          throws Exception {

          ...

          ...

          }         

    /**

    *AritcleSearchAction

    */

    public ActionForward search(ActionMapping mapping,

                            ActionForm form,

                            HttpServletRequest request,

                            HttpServletResponse response)

          throws Exception {

          ...

          ...

          }

}

好了,该action的框架已经完成,但是要想可用,还要有一步不可少的操作,

那就是更改你的action mapping ,还以上边的例子,如下:

<action path="/article"

input="/article/***.jsp"

parameter="method" <!--#####################-->

scope="request"

type="com.***.ArticleAction"  

validate="false">

<forward name="Success" path="/article/***.jsp" redirect="true"/>

</action>

看到上边你会发现,它和我们通常的写法多个一项:“parameter="method",这是有道理的并且非常重要:DispatchAction会根据具体的method值来确定调用add,view 或者search 如下面的来自client的请求,article.do?method=add 则会触发添加文章的操作。

LookupDispatchAction

========================

org.apache.struts.actions.LookupDispatchAction.java

从名字大概我们也能看出LookupDispatchActionDispatchAction的子类。他们从功能上有许多相似的地方。 通常它主要应用于“在一个表单中有多个提交按钮而这些按钮又有一个共同的名字”,这些按钮的名字要和具体的action mapping中的parameter的值对应。[这点很重要]

如下代码截取自struts-config.xml

<action path="/editArticle"

        type="com.****.EditArticleAction"

        name="AtricleForm"

        scope="request"

        parameter="action"><!--按钮的名字此处为“action-->

    <forward name="success" path="/***.jsp"/>

</action>

下面给出一个jsp页面的表单部分

<html:form action="/editArticle"/>

    <html:submit property="action">

       <bean:message key="button.view"/>

    </html:submit>

    <html:submit property="action">

       <bean:message key="button.delete"/>

    </html:submit>   

</html:form>

那么相应的ApplicationResources.properties中就会有如下片断:

button.view=View The Article

button.delete=Delete The Atricle

此时还并为完成,在LookupDispatchAction中有一个抽象方法:

    /**

     * Provides the mapping from resource key to method name

     *

     *@return          Resource key / method name map

     */

    protected abstract Map getKeyMethodMap();

   

这个方法你应该在EditArticleAction中实现,如下:

protected Map getKeyMethodMap(){

      Map map = new HashMap();

      map.put("button.view", "view");

      map.put("button.delete", "delete");

      return map;

}

好了,假设在你的EditArticleAction有如下方法:

public ActionForward view(ActionMapping mapping,

          ActionForm form,

          HttpServletRequest request,

          HttpServletResponse response)

          throws IOException, ServletException {

      //......

      //......

      return mapping.findForward("success");

}

public ActionForward delete(ActionMapping mapping,

          ActionForm form,

          HttpServletRequest request,

          HttpServletResponse response)

          throws IOException, ServletException {

      //......

      //......

      return mapping.findForward("success");

}

下面实例几个假设client端的请求:

http://....../editArticle.do此时页面有两个按钮,按钮1View The Article,"",按钮2Delete The Atricle

当提交按钮1时调用EditArticleAction里的view方法;

当提交按钮2时调用EditArticleAction里的delete方法;

以下还有一点说明;

如果我有一个按钮要触发actionAA方法,但是在该action没有AA方法,此时将抛出异常;如果该action中有两个AA方法,则会调用第一个。

SwitchAction

========================

org.apache.struts.actions.SwitchAction

SwitchAction主要在多模块应用中执行模块切换作用。

Struts多模块工程中有两种方法可以切换到新模块上.首先,您可以在 struts-config.xml 中创建一个新的全局或局部转发“success”(参见下面代码)。然后可以使用 mapping.findForward("success") 切换到这个新模块上来。


<forward name="success"   contextRelative="true"
   path="/newModule/index.do"   redirect="true"/>
contextRelative
=true的意思是path是相对于应用上下文的路径,contextRelative=false则是指path是相对于模块上下文的路径。

其次,创建一个如下所示的操作,其类型是一个 SwitchAction 操作:

<action path="/switchTo" type="org.apache.struts.actions.SwitchAction" validate="false" />

然后在 struts-config-newModule.xml 中创建如下所示的操作映射。

<action-mappings>
   <action path="/index" type="com.asprise.struts.newmodule.action.IndexAction">
      <forward name="success" path="/index.jsp"/>
   </action>
</action-mappings>

接下来,编写 easyStruts/newModule/index.jsp 的代码。index.jsp 只会显示一条消息“<h1>Youare in module: newModule</h1>”。现在,启动 Tomcat 服务器,并输入 http://127.0.0.1:8080/easyStruts/switchTo.do?prefix=/newModule&page=/index.do既可。如果您想切换回默认的模块,只需输入 http://127.0.0.1:8080/easyStruts/switchTo.do?prefix=&page=/owner.jsp 即可。

 发表于: 2008-05-09,修改于: 2008-05-09 09:22 已浏览93次,有评论0条 推荐 投诉

  网友评论

  发表评论



Copyright © 2001-2006 ChinaUnix.net All Rights Reserved

感谢所有关心和支持过ChinaUnix的朋友们
页面生成时间:0.03448

京ICP证041476号