使用DispatchAction类调用多个Action方法

        在DispatchAction的子类中定义多个Action,通过operation参数指定调用哪一个方法。如果operation是空字符串或是execute,那么就调用默认的方法unspecificed方法.

     

public class ProductAction extends DispatchAction{
	/**
	 * 在Action类中有一个特殊的方法:execute。如果调用了这个方法,将会出现递归调用的情况
	 *1.如果访问的是execute方法,将name设置成null,访问默认的方法unspecified
	 *2.在没找到对应的方法的情况下,也将name设置成null,调用unspecified
	 * 
	 */
	protected ActionForward dispatchMethod(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response, String name) throws Exception {
		System.out.println("dispatchMethod..........................................");
		System.out.println(name);
		ActionForward af = null;
		if("".equals(name)||"execute".equals(name)){
			name = null;
		}
		try {
			af = super.dispatchMethod(mapping, form, request, response, name);
		} catch (Exception e) {//如果没找到对应的方法,会报错
			name = null;//name设置成null,就是找默认的方法
			af = super.dispatchMethod(mapping, form, request, response, name);
		}
		
		
		return af;
	}
	/**
	 * 当指定的Action不存在时,调用这个action
	 */
	@Override
	protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		System.out.println("unspecified.........................................");
		return null;
	}
	
//	@Override
//	public ActionForward execute(ActionMapping mapping, ActionForm form,
//			HttpServletRequest request, HttpServletResponse response)
//			throws Exception {
//		// TODO Auto-generated method stub
//		System.out.println("execute.......................................");
//		return null;
//	}

	/**test:
	 * 1.在有execute的方法前提下,会调用execute,在没有exectute的时候,才可以调用别的
	 * 2.struts会先找execute方法,没如果找不到execute才会根据传参找对应方法
	 * 
	 */
	/*
	 * 根据商品名称 查询商品信息
	 */
	public ActionForward queryProductInfobyName(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		System.out.println("query product info ................................");
		return null;
	}
	
	/*
	 * 录入商品信息
	 * @throws Exception
	 */
	public ActionForward insertProductInfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		System.out.println("insert productInfo.................................");
		return null;
	}
}

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

<action path="/product" type="action.productAction" parameter="operation"/>

3.通过如下方式调用指定的方法

http://localhost:8080/struts1_20140801/product.do?operation=execute  调用默认的方式unspecificed

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

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

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> if  (name  ==   null )    //  name表示Action方法名
{
    return   this .unspecified(mapping, form, request, response);
}

 

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

DispatchAction类中有一个dispatchMethod方法,可以在这个方法中处理请求参数值为空串(也就是当“language=”时将方法名设为null)和Action方法未找到的情况。在Action类中有一个特殊方法:execute。如果调用了这两个方法,将会出现递归调用的情况。因此,在调用这两个方法时也需要将方法名name设为null

 

/**
	 * 在Action类中有一个特殊的方法:execute。如果调用了这个方法,将会出现递归调用的情况
	 *1.如果访问的是execute方法,将name设置成null,访问默认的方法unspecified
	 *2.在没找到对应的方法的情况下,也将name设置成null,调用unspecified
	 * 
	 */
	protected ActionForward dispatchMethod(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response, String name) throws Exception {
		System.out.println("dispatchMethod..........................................");
		System.out.println(name);
		ActionForward af = null;
		if("".equals(name)||"execute".equals(name)){
			name = null;
		}
		try {
			af = super.dispatchMethod(mapping, form, request, response, name);
		} catch (Exception e) {//如果没找到对应的方法,会报错
			name = null;//name设置成null,就是找默认的方法
			af = super.dispatchMethod(mapping, form, request, response, name);
		}
		
		
		return af;
	}

 

猜你喜欢

转载自weigang-gao.iteye.com/blog/2100879