struts1(11)-----DispatchAction

通常情况下,struts框架对每个请求都交由一个action来处理。因此若对每个请求都有一个action来处理,这样将导致服务器action过多,维护困难。struts提供一种方式,将多个request请求都交由一个action来处理,DispatchAction就可以完成此功能,开发者编写一个类,继承此类。便可以实现将所有请求都交由开发者编写的action类。这个类的excute将会根据用户请求中携带的操作方法来将此请求分发到与其相应的处理方法上来处理。

下面简要描述下DispatchAction类,这个类继承了Action类,因此它可以用来处理request请求。此类维护了一个HashMap集合methods, 此map集合记录了处理所有请求的(负责分发所有请求的action)action所提供的方法。当用户提交一个请求后,将会将处理这个请求的方法存入到此map集合中。以便下次处理此操作时,直接从map集合中获取,而不再需要通过反射机制来获取此方法。源码代码如下:

protected Method getMethod(String name)
        throws NoSuchMethodException {
        synchronized (methods) {
            Method method = (Method) methods.get(name);

            if (method == null) {
                method = clazz.getMethod(name, types);//clazz为开发者编写的类,name为方法名称,types为方法的参数
                methods.put(name, method);
            }

            return (method);
        }
    }
用户提交的请求,首先交由DispatchAction的execute方法来处理,execute方法将会通过getParameter(mapping, form, request, response)方法来获取配置文件中action标签的paramter属性指定的值,该值一般为(action或者method), 这里用来它来指定使用什么方法。即用method来作为参数,通过method参数来获取用户提交的请求用什么方法来处理。如配置文件中配置如下:parameter指定为method

<action 
			path="/BookAction" 
			type="cn.itcast.action.BookActions"
			parameter="method"
			>
			<forward name="message" path="/message.jsp"></forward>
		</action>
jsp页面如下:

 <body>
    <html:link action="/BookAction?method=add">添加</html:link><br />
    <html:link action="/BookAction?method=del">删除</html:link><br />
    <html:link action="/BookAction?method=update">修改</html:link><br />
    <html:link action="/BookAction?method=query">查询</html:link><br />
  </body>
此jsp页面将通过method来指定此请求来使用哪种方法,这个参数较封装在reqeust域中,可通过request的getParameter方法来获取。当获取到方方法名称,execute将调用dispatchMethod(mapping, form, request, response, name)方法来将此请求分发到method指定的方法上去处理。此方法将利用反射机制,通过name名来出其方法,其核心源码如下:

Method method = null;

      ...
            method = getMethod(name);
       
        ActionForward forward = null;

        ...
            Object[] args = { mapping, form, request, response };

            forward = (ActionForward) method.invoke(this, args);
struts的DispatchAction可以根据请求的参数调用不同的方法, 但要注意的是开发人员在声明参数对应的方法时,方法的签名需要与 execute() 方法一致,如:

public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		request.setAttribute("message", "add method running...");
		return mapping.findForward("message");
	}
通过源码可以看出,Struts中的DispatchAction并不知道用户的参数是以什么名称提交的,因此若想DispatchAction能正常调用到用户自定义的方法,用户需要在action的配置文件中通过parameter属性告诉给struts。

另外,最重要的一点是,DispatchActionAction的子类,它重写了Actionexecute方法,因此用户继承DispatchAction时,若想使用DispatchAction提供的分发功能,切记不可覆盖其execute方法

下面是一个例子示例代码:

public class BookActions extends DispatchAction {

	
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		request.setAttribute("message", "add method running...");
		return mapping.findForward("message");
	}

	public ActionForward del(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		request.setAttribute("message", "delete method running...");
		return mapping.findForward("message");
	}
	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		request.setAttribute("message", "update method running...");
		return mapping.findForward("message");
	}
	
	public ActionForward query(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		request.setAttribute("message", "query method running...");
		return mapping.findForward("message");
	}
}
配置文件和jsp页面,在文章前面所写



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值