您的位置:首页 > 编程语言 > Java开发

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

2015-04-28 16:46 471 查看
转自:

http://www.blogjava.net/nokiaguy/archive/2009/02/archive/2009/02/archive/2009/02/archive/2009/02/archive/2009/02/archive/2009/02/20/255819.html

在使用Struts动作时,每一个Action都需要编写一个类,并且要在struts-config.xml进行配置。这对于一个拥有很多Action的Web程序来说,工作量是非常大的。为此,Struts提供了DispatchAction类,这个类允许将一个Action作为一个方法来调用。在Web浏览器中通过请求参数来指定要调用的动作。

虽然DispatchAction类是一个抽象类,但其中却没有一个抽象方法。因此,DisplatchAction的子类不用实现任何DisplatchAction类中的方法。但如果要处理Action代码,就必须根据相应的Action来编写Action方法。一个Action方法除了方法名和execute方法不一样外,其他的都和execute方法完全一样。但编写Action方法时要注意,Action方法名必须和用于指定动作的请求参数值一致(大小写也必须一致)。在下面的例子中演示了通过DispatchAction类实现方法和Action的对应。在这个例子中,有三个方法:fr、en和unspecificed。其中fr和en是两个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);

}

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

在DispatchAction类中有一个dispatchMethod方法,可以在这个方法中处理请求参数值为空串(也就是当“language=”时将方法名设为null)和Action方法未找到的情况。在Action类中有两个特殊方法:execute和perform。如果调用了这两个方法,将会出现递归调用的情况。因此,在调用这两个方法时也需要将方法名设为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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐