服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

覆盖dispatchaction中的分发方法


  背景:
  
  在使用struts时我们经常会用到dispatchaction.有了这个类,我们不需要针对每一个action都要写一个特定的类,而是可以把一些相关的方法放到一个类中.
  
  dispatchacton中使用了reflection来根据你传入的method参数的值来获取相应的参数来处理你的请求.正如他的方法 -- 他根据你传入的请求参数,用不同的方法来处理你的请求.
  
  但是,通常我们还会遇到另一种情况,如果我想在之行实际的action处理方法之前或者之后再去做一些别的事情而又不想修改我们实际存在的action类呢?这里我将介绍一种方法.
  
  只要看看struts中dispatchaction(以下简写做da)的源文件你就会发现,它有一个dispatchmethod方法,接受5个参数.其中4个就是我们通常的struts action里的(mapping,request,response,form),还有一个参数就是指定方法的参数的名字.
  
  protected actionforward dispatchmethod(actionmapping mapping,
  actionform form, httpservletrequest request,
  httpservletresponse response, string name) throws exception
  
  其实现方法如下:
  
  // make sure we have a valid method name to call.
  // this may be null if the user hacks the query string.
  if (name == null) {
  return this.unspecified(mapping, form, request, response);
  }
  
  // identify the method object to be dispatched to
  method method = null;
  try {
  method = getmethod(name);
  
  } catch (nosuchmethodexception e) {
  string message = messages.getmessage("dispatch.method", mapping
  .getpath(), name);
  log.error(message, e);
  throw e;
  }
  
  actionforward forward = null;
  try {
  object args[] = { mapping, form, request, response };
  
  //特殊的url不进行处理(add)
  string actionurl = request.getservletpath(); // "/rolesign.do"
  boolean exception = isexception(actionurl);
  if(exception) {
  logger.debug("requesturl :"+actionurl+",no pre and post process");
  }
  // 预处理(add)
  if (!disabled && !exception) {
  logger.debug("preprocess begin");
  preprocess(request);
  logger.debug("preprocess end");
  }
  
  forward = (actionforward) method.invoke(this, args);
  
  // 后处理(add)
  if (!disabled && !exception ){
  logger.debug("postprocess begin");
  postprocess(request);
  logger.debug("postprocess end");
  }
  } catch (classcastexception e) {
  string message = messages.getmessage("dispatch.return", mapping
  .getpath(), name);
  log.error(message, e);
  throw e;
  
  } catch (illegalaccessexception e) {
  string message = messages.getmessage("dispatch.error", mapping
  .getpath(), name);
  log.error(message, e);
  throw e;
  
  } catch (invocationtargetexception e) {
  // rethrow the target exception if possible so that the
  // exception handling machinery can deal with it
  throwable t = e.gettargetexception();
  if (t instanceof exception) {
  throw ((exception) t);
  } else {
  string message = messages.getmessage("dispatch.error", mapping
  .getpath(), name);
  log.error(message, e);
  throw new servletexception(t);
  }
  }
  
  // return the returned actionforward instance
  return (forward);
  
  大部分代码还是从da的实现方法中copy过来,但是我在这里加入了3个地方.分别是:
  
  1.对请求url的识别,确定是否使用预/后处理
  
  2.预处理方法调用
  
  3.后处理方法调用
  
  当然你要自己写预处理方法和后处理方法.这里你可以传任意你想要的参数给request,然后通过reflection来动态调用任意的java方法,比如你可以在页面设置一个prefunction参数,它的值是classname.methodname.然后你可以在preprocess的实现中通过reflection来查找这个方法并执行.
  
  如果你想让项目中所有的action都有这种特性,则只要让根action继承da并覆盖它的dispatchmethod方法即可.
  
  这是在实际项目中用到的一个特性,为了尽可能少的修改原有的代码使用了这种折中的做法.使用reflection时建议不要执行太过复杂的方法,可能会使你的action响应慢的.

扫描关注微信公众号