反射练习之action消除execute方法

反射练习之action消除execute方法
/**
 * @author yzy
 * 作为BaseAction 供其他Action 继承, 其他Action 不需要写execute(mapping, form, request, response)
 * 方法,直接写与mapping.getParameter()[struts-xml 的<action> 中parameter] 对应的 方法名(mapping, form, request, response)方法;
 *
 * 或者mapping.getPath()[struts-xml 的<action> 中path] 固定写为 method_0,直接写与mapping.getParameter()对应的
 * 方法名(String ... str)方法
 */

@Override
 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
          actionForward = mapping.findForward("forward");
  
          if("method_0".equals(mapping.getPath())){
                   method_0(mapping, form, request, response);
          } else {
     
          try {
        // 正常Struts 方法名(ActionMapping mapping, ActionForm form, 
        //     HttpServletRequest request, HttpServletResponse response)

                    this.getClass().getMethod(mapping.getParameter(), ActionMapping.class, ActionForm.class, 
                              HttpServletRequest.class, HttpServletResponse.class).invoke(this, mapping, form, request, response);
           } catch (Exception e) {
                    System.err.println("no serach method");
           }
  }
  
          return actionForward;
 }
 /*
  * 方法名(String ... str)
  */
 private void method_0(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
          String param = request.getQueryString();
          String[] params = param.split("&");
          int len = params.length;
          Class[] classes = new Class[len - 1];
          for(int i = 0; i < len; i++){
                   classes[i] = String.class;
          }
  
          try {
                   this.getClass().getMethod(mapping.getParameter(), classes).invoke(this, params);
          } catch (Exception e) {
                   System.err.println("no serach method"); // str[] 可能参数个数不一致
          }
  
 }

你可能感兴趣的:(反射练习之action消除execute方法)