随笔 - 312, 文章 - 14, 评论 - 1393, 引用 - 0
数据加载中……

Struts1.x系列教程(18):使用DispatchAction类调用多个Action方法

本文为原创,如需转载,请注明作者和出处,谢谢!

上一篇:Struts1.x系列教程(17):使用IncludeAction和ForwardAction类包含和转入Web资源

    在使用Struts动作时,每一个Action都需要编写一个类,并且要在struts-config.xml进行配置。这对于一个拥有很多ActionWeb程序来说,工作量是非常大的。为此,Struts提供了DispatchAction类,这个类允许将一个Action作为一个方法来调用。在Web浏览器中通过请求参数来指定要调用的动作。
    虽然
DispatchAction类是一个抽象类,但其中却没有一个抽象方法。因此,DisplatchAction的子类不用实现任何DisplatchAction类中的方法。但如果要处理Action代码,就必须根据相应的Action来编写Action方法。一个Action方法除了方法名和execute方法不一样外,其他的都和execute方法完全一样。但编写Action方法时要注意,Action方法名必须和用于指定动作的请求参数值一致(大小写也必须一致)。在下面的例子中演示了通过DispatchAction类实现方法和Action的对应。在这个例子中,有三个方法:frenunspecificed。其中fren是两个Action方法,分别用于将当前页面转发到法文和英文页面,而当用于指定Action的请求参数不存在时,则调用unspecificed方法(在这个方法中将当前页面转发到中文页面)。在这个程序中使用的用于指定Action的请求参数为language(读者可以指定自己的请求参数)。
    在
<samples工程目录>\src\action目录建立一个MyDispatchAction.java文件,代码如下:

  package action;
  
  
import javax.servlet.RequestDispatcher;
  
import javax.servlet.http.*;
  
import org.apache.struts.action.*;
  
import org.apache.struts.actions.*;
  
  
public class MyDispatchAction extends DispatchAction
  {
      
// forward到法文页面
      public ActionForward fr(ActionMapping mapping, ActionForm form,
              HttpServletRequest request, HttpServletResponse response)
      {
          
try
          {
              RequestDispatcher rd 
= request.getRequestDispatcher("/newGlobal.jsp?language=fr");
              rd.forward(request, response);
          }
          
catch (Exception e)
          {
          }
          
return null;
      }
      
// forward到英文页面
      public ActionForward en(ActionMapping mapping, ActionForm form,
              HttpServletRequest request, HttpServletResponse response)
      {
          
try
          {
              RequestDispatcher rd 
= request.getRequestDispatcher("/newGlobal.jsp?language=en");
              rd.forward(request, response);
          }
          
catch (Exception e)
          {
          }
          
return null;
      }
      
// 在未使用language=fr和language=en作为访问参数的情况时调用这个方法
      protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
              HttpServletRequest request, HttpServletResponse response)
              
throws Exception
      {
          
try
          {
               
// forward到中文页面
              RequestDispatcher rd = request.getRequestDispatcher("/newGlobal.jsp?language=zh");
              rd.forward(request, response);
          }
          
catch (Exception e)
          {  
          }
          
return null;
      }
  }

    在struts-config.xml文件中加入如下的配置代码来配置MyDispatchAction

<action path="/locale" type="action.MyDispatchAction" parameter="language"/>

其中parameter参数表示用于指定Action的请求参数名。

在启动Tomcat后,可通过如下的URL进行测试:

   
显示英文页面:

http://localhost:8080/samples/locale.do?language=en

   
显示法文页面:

http://localhost:8080/samples/locale.do?language=fr

显示中文页面(默认页面):

http://localhost:8080/samples/locale.do

虽然上面的代码可以很好地调用相应的Action方法,但在一些情况时,如请求参数language指定的Action方法不存在时,就会抛出异常。那么如果我们想在非正常情况下都调用默认的处理Action动作的方法(也就是unspecificed方法)该怎么办呢?

实现上,实现这个功能也非常简单,只要我们知道在什么条件下调用unspecified方法,然后在非正常情况下,都将条件设为调用unspecified方法的条件就可实现这个功能。在查看DispatchAction类的源代码后,可找到如下的代码片段:

if (name == null)   // name表示Action方法名
{
    return this.unspecified(mapping, form, request, response);
}

从上面的代码可知,只有当namenull时才会调用unspecified方法。这个name值实际上也就是language参数的值。也就是说,只有当language参数不存在时,name才会为null。如果在language的参数值所指的Action方法不存在时或者name为空串的情况下都将name设为null,那么就可以达到我们的目的。

DispatchAction类中有一个dispatchMethod方法,可以在这个方法中处理请求参数值为空串(也就是当“language=”时将方法名设为null)和Action方法未找到的情况。在Action类中有两个特殊方法:executeperform。如果调用了这两个方法,将会出现递归调用的情况。因此,在调用这两个方法时也需要将方法名设为null。这个工作可以在DispatchAction类的getMethodName方法中实现。为了完成这个功能,需要将上面的代码放到MyDispatchAction类中。 

protected ActionForward dispatchMethod(ActionMapping mapping,
            ActionForm form, HttpServletRequest request,
            HttpServletResponse response, String name) 
throws Exception
{
      ActionForward af 
= null;
      
// 在language参数值为空串的情况下,将方法名赋为null
      if (name != null)  // name表示Action方法名,也是language的参数值     
         
if (name.equals(""))
              name 
= null;
      
try
      {
             af 
= super.dispatchMethod(mapping, form, request, response, name);
      }
      
catch(NoSuchMethodException e)  // 处理Action方法未找到的情况
      {
           
// 在language的参数值没有对应的Action方法时,将方法名赋为null
          name = null;            
          af 
= super.dispatchMethod(mapping, form, request, response, name);
      }
      
return af;
}
  
// 当language的参数值为execute或perfore时,必须将方法名赋为null,否则会出现递归调用
protected String getMethodName(ActionMapping mapping, ActionForm form,
          HttpServletRequest request, HttpServletResponse response,
          String parameter) 
throws Exception
{
      String methodName 
= super.getMethodName(mapping, form, request, response, parameter);
      
if ("execute".equals(methodName) || "perform".equals(methodName))
           
return null;  // 如果访问的是execute和perform,直接将方法名设为null
      return methodName;
}

  现在我们可以用任何请求参数来访问locale动作,只要未找到Action方法,就会调用默认的unspecified方法。读者可以使用如下的URL来实验一下:

http://localhost:8080/samples/locale.do?language=

http://localhost:8080/samples/locale.do?language=unknown


下一篇:Struts1.x系列教程(19):LookupDispatchAction类处理一个form多个submit





Android开发完全讲义(第2版)(本书版权已输出到台湾)

http://product.dangdang.com/product.aspx?product_id=22741502



Android高薪之路:Android程序员面试宝典 http://book.360buy.com/10970314.html


新浪微博:http://t.sina.com.cn/androidguy   昵称:李宁_Lining

posted on 2009-02-20 14:01 银河使者 阅读(5767) 评论(5)  编辑  收藏 所属分类: javaweb 原创struts1.x

评论

# re: Struts1.x系列教程(18):使用DispatchAction类调用多个Action方法  回复  更多评论   

这样的BLOG才有用,比那些忽悠的好多了。
2009-02-20 17:34 | bad

# re: Struts1.x系列教程(18):使用DispatchAction类调用多个Action方法[未登录]  回复  更多评论   

强烈建议楼主制作PDF,提供下载
2009-02-21 15:12 | L

# re: Struts1.x系列教程(18):使用DispatchAction类调用多个Action方法  回复  更多评论   

哈哈,struts 1.x快发完了,完事统一做pdf
2009-02-21 15:34 | 银河使者

# re: Struts1.x系列教程(18):使用DispatchAction类调用多个Action方法  回复  更多评论   

我一般不回帖的,强烈支持作者。
2009-05-14 21:36 | Harold.Zhang

# re: Struts1.x系列教程(18):使用DispatchAction类调用多个Action方法  回复  更多评论   

我顶啊
2009-12-30 22:15 | 楼主说得好

只有注册用户登录后才能发表评论。


网站导航: