/**
   * 解析默认的Action配置
   *
   * @param methodReqMapVal
   * @param moduleName
   * @return
   */
  private static ActionConfigBean parseDefaultActionConfig(String methodName, String moduleName) {
    String uriMapping = null;
    String httpMethod = null;
    ActionConfigBean acb = new ActionConfigBean();

    if (ActionMethod.INDEX.equals(methodName)) {
      uriMapping = "/";
      httpMethod = HttpMethod.GET;
      ResultConfigBean rcb = new ResultConfigBean();
      rcb.setName("jsp");
      rcb.setLocation(moduleName + "/view/index.jsp");
      acb.getResult().add(rcb);
      ResultConfigBean rcb2 = new ResultConfigBean();
      rcb2.setName("html");
      rcb2.setType(RenderType.FREEMARKER);
      rcb2.setLocation(moduleName + "/view/index.html");
      acb.getResult().add(rcb2);
    } else if (ActionMethod.CREATE.equals(methodName)) {
      uriMapping = "/";
      httpMethod = HttpMethod.POST;

      ResultConfigBean rcb = new ResultConfigBean();
      rcb.setName(ActionMethod.INDEX);
      rcb.setLocation(moduleName);
      rcb.setType(RenderType.ACTION);
      acb.getResult().add(rcb);
    } else if (ActionMethod.UPDATE.equals(methodName)) {
      uriMapping = "/{id}";
      httpMethod = HttpMethod.PUT;
      ResultConfigBean rcb = new ResultConfigBean();
      rcb.setName(ActionMethod.INDEX);
      rcb.setLocation(moduleName);
      rcb.setType(RenderType.ACTION);
      acb.getResult().add(rcb);
    } else if (ActionMethod.SHOW.equals(methodName)) {
      uriMapping = "/{id}";
      httpMethod = HttpMethod.GET;

      ResultConfigBean rcb = new ResultConfigBean();
      rcb.setName("jsp");
      rcb.setLocation(moduleName + "/view/show.jsp");
      acb.getResult().add(rcb);
      ResultConfigBean rcb2 = new ResultConfigBean();
      rcb2.setName("html");
      rcb2.setType(RenderType.FREEMARKER);
      rcb2.setLocation(moduleName + "/view/show.html");
      acb.getResult().add(rcb2);
    } else if (ActionMethod.EDIT.equals(methodName)) {
      uriMapping = "/{id}/edit";
      httpMethod = HttpMethod.GET;
      ResultConfigBean rcb = new ResultConfigBean();
      rcb.setName("jsp");
      rcb.setLocation(moduleName + "/view/edit.jsp");
      acb.getResult().add(rcb);
      ResultConfigBean rcb2 = new ResultConfigBean();
      rcb2.setName("html");
      rcb2.setType(RenderType.FREEMARKER);
      rcb2.setLocation(moduleName + "/view/edit.html");
      acb.getResult().add(rcb2);
    } else if (ActionMethod.DESTROY.equals(methodName)) {
      uriMapping = "/{id}";
      httpMethod = HttpMethod.DELETE;

      ResultConfigBean rcb = new ResultConfigBean();
      rcb.setName(ActionMethod.INDEX);
      rcb.setLocation(moduleName);
      rcb.setType(RenderType.ACTION);
      acb.getResult().add(rcb);
    } else if (ActionMethod.NEW.equals(methodName)) {
      uriMapping = "/new";
      httpMethod = HttpMethod.GET;
      ResultConfigBean rcb = new ResultConfigBean();
      rcb.setName("jsp");
      rcb.setLocation(moduleName + "/view/new.jsp");
      acb.getResult().add(rcb);
      ResultConfigBean rcb2 = new ResultConfigBean();
      rcb2.setName("html");
      rcb2.setType(RenderType.FREEMARKER);
      rcb2.setLocation(moduleName + "/view/new.html");
      acb.getResult().add(rcb2);
    } else {
      acb = null;
    }

    if (acb != null) {
      acb.setHttpMethod(httpMethod);
      acb.setUriMapping(uriMapping);
    }

    return acb;
  }
  private void handleResult() throws Exception {

    this.exeActionLog();

    if (retn == null) return;
    String baseUrl =
        (String) this.context.getServletContext().getAttribute(MVCConfigConstant.BASE_URL_KEY);
    if (File.class.isAssignableFrom(retn.getClass())) {
      File file = (File) retn;
      this.handleDownload(file);
      return;
    } else if (File[].class.isAssignableFrom(retn.getClass())) {
      File[] files = (File[]) retn;

      String fileName = CommonUtil.getNowTime("yyyyMMddHHmmss") + "_" + "download.zip";

      HttpServletResponse resp = this.context.getResponse();
      resp.reset();
      resp.setContentType("application/zip");
      resp.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");

      ServletOutputStream outputStream = resp.getOutputStream();
      ZipOutputStream zip = new ZipOutputStream(outputStream);

      for (File file : files) {
        byte[] b = new byte[1024];
        int len;
        zip.putNextEntry(new ZipEntry(file.getName()));
        FileInputStream fis = new FileInputStream(file);
        while ((len = fis.read(b)) != -1) {
          zip.write(b, 0, len);
        }

        fis.close();
      }

      zip.flush();
      zip.close();
      outputStream.flush();

      return;
    }

    if (!String.class.isAssignableFrom(retn.getClass())) {
      String mimeType = null;
      Produces prod = this.method.getAnnotation(Produces.class);
      if (prod != null && prod.value() != null && prod.value().length > 0)
        mimeType = prod.value()[0];

      if (mimeType == null || mimeType.trim().length() == 0)
        mimeType =
            this.context.getRequest().getParameter(MVCConfigConstant.HTTP_HEADER_ACCEPT_PARAM);

      if (mimeType == null || mimeType.trim().length() == 0) {
        String contentType = this.context.getRequest().getContentType();
        if (contentType != null) {
          this.context.getResponse().setContentType(contentType);
          mimeType = contentType.split(";")[0];
        }
      }

      if (this.context.getWriter() == null)
        this.context.setWriter(this.context.getResponse().getWriter());

      if (MIMEType.JSON.equals(mimeType) || "json".equalsIgnoreCase(mimeType)) {
        this.context.getResponse().setContentType(MIMEType.JSON);
        this.context.getWriter().print(CommonUtil.toJson(retn));
      } else if (MIMEType.XML.equals(mimeType) || "xml".equalsIgnoreCase(mimeType)) {
        Class<?> cls = retn.getClass();
        if (Collection.class.isAssignableFrom(cls)) {
          Class<?> _cls = ClassUtil.getPojoClass(this.method);
          if (_cls != null) cls = _cls;
        }

        XMLWriter writer = BeanXMLUtil.getBeanXMLWriter(retn);
        writer.setCheckStatck(true);
        writer.setSubNameAuto(true);
        writer.setClass(cls);
        writer.setRootElementName(null);
        this.context.getResponse().setContentType(MIMEType.XML);
        this.context.getWriter().print(writer.toXml());
      } else {
        this.context.getWriter().print("暂时不支持JSON 、XML以外的表述形式");
      }

      this.context.getWriter().flush();

      return;
    }

    List<String> produces = this.context.getActionConfigBean().getProduces();
    if (produces != null && produces.size() > 0)
      for (String produce : produces) {
        this.context.getResponse().setContentType(produce);
        break;
      }

    String re = String.valueOf(retn);

    for (Field f : fields) {
      Method getter = ru.getGetter(f.getName());
      if (getter == null) continue;

      String name = f.getName();
      if (this.context.getModel().containsKey(name)) continue;

      this.context.getModel().put(name, getter.invoke(actionObject));
    }

    this.context.getModel().put(MVCConfigConstant.BASE_URL_KEY, baseUrl);

    // 客户端重定向
    if (re.startsWith(RenderType.REDIRECT + ":")) {
      String url = re.substring((RenderType.REDIRECT + ":").length());
      String location = url;

      this.context.getResponse().sendRedirect(CommonUtil.replaceChinese2Utf8(location));

      return;
    } else if (re.startsWith(RenderType.ACTION + ":")) {
      String path = re.substring((RenderType.ACTION + ":").length());
      // ACTION 重定向
      handleActionRedirect(context, path, baseUrl);

      return;
    } else if (re.startsWith(RenderType.OUT + ":")) {
      String location = re.substring((RenderType.OUT + ":").length());
      this.context.getWriter().print(location);
      this.context.getWriter().flush();

      return;
    } else if (re.startsWith(RenderType.FORWARD + ":")) {
      String location = re.substring((RenderType.FORWARD + ":").length());
      HttpServletRequest request = this.context.getRequest();
      request.setAttribute(MVCConfigConstant.REQ_PARAM_MAP_NAME, this.context.getQueryParamMap());

      for (Iterator<Entry<String, Object>> it = this.context.getModel().entrySet().iterator();
          it.hasNext(); ) {
        Entry<String, Object> entry = it.next();
        request.setAttribute(entry.getKey(), entry.getValue());
      }

      // 服务端跳转
      request
          .getRequestDispatcher(MVCConfigConstant.FORWARD_BASE_PATH + "/" + location)
          .forward(request, this.context.getResponse());

      return;
    } else if (re.startsWith(RenderType.FREEMARKER + ":")) {
      String location = re.substring((RenderType.FREEMARKER + ":").length());
      // FreeMarker 渲染
      Configuration cfg = new Configuration();
      // 指定模板从何处加载的数据源,这里设置成一个文件目录。
      cfg.setDirectoryForTemplateLoading(
          new File(ConfigConstant.ROOT_PATH + MVCConfigConstant.FORWARD_BASE_PATH));
      // 指定模板如何检索数据模型
      cfg.setObjectWrapper(new DefaultObjectWrapper());
      cfg.setDefaultEncoding("utf-8");

      Template template = cfg.getTemplate(location);
      template.setEncoding("utf-8");

      template.process(this.context.getModel(), this.context.getWriter());

      return;
    } else if (re.startsWith(RenderType.VELOCITY + ":")) {
      String location = re.substring((RenderType.VELOCITY + ":").length());
      File viewsDir = new File(ConfigConstant.ROOT_PATH + MVCConfigConstant.FORWARD_BASE_PATH);
      // 初始化Velocity模板引擎
      Properties p = new Properties();
      p.setProperty("resource.loader", "file");
      p.setProperty(
          "file.resource.loader.class",
          "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
      p.setProperty("file.resource.loader.path", viewsDir.getAbsolutePath());
      p.setProperty("file.resource.loader.cache", "true");
      p.setProperty("file.resource.loader.modificationCheckInterval", "2");
      p.setProperty("input.encoding", "utf-8");
      p.setProperty("output.encoding", "utf-8");
      VelocityEngine ve = new VelocityEngine(p);
      // Velocity获取模板文件,得到模板引用
      org.apache.velocity.Template t = ve.getTemplate(location);
      VelocityContext velocityCtx = new VelocityContext();
      for (Iterator<Entry<String, Object>> it = this.context.getModel().entrySet().iterator();
          it.hasNext(); ) {
        Entry<String, Object> e = it.next();
        velocityCtx.put(e.getKey(), e.getValue());
      }

      // 将环境变量和输出部分结合
      t.merge(velocityCtx, this.context.getWriter());
      this.context.getWriter().flush();
      return;
    } else {
      List<ResultConfigBean> results = this.context.getActionConfigBean().getResult();

      if (results == null || results.size() == 0) {
        this.context.getWriter().print(retn);
        this.context.getWriter().flush();
        return;
      }

      boolean isOut = true;
      for (ResultConfigBean r : results) {
        if (!"_props_".equals(r.getName()) && !r.getName().equals(re) && !"".equals(re)) {
          continue;
        }

        isOut = false;
        String type = r.getType();
        String location = r.getLocation();
        if (RenderType.REDIRECT.equalsIgnoreCase(type)) {
          this.context.getResponse().sendRedirect(CommonUtil.replaceChinese2Utf8(location));

          return;
        } else if (RenderType.FORWARD.equalsIgnoreCase(type)) {
          HttpServletRequest request = this.context.getRequest();
          request.setAttribute(
              MVCConfigConstant.REQ_PARAM_MAP_NAME, this.context.getQueryParamMap());

          fields = ru.getFields();
          if (fields == null) return;

          for (Iterator<Entry<String, Object>> it = this.context.getModel().entrySet().iterator();
              it.hasNext(); ) {
            Entry<String, Object> entry = it.next();
            request.setAttribute(entry.getKey(), entry.getValue());
          }
          // 服务端跳转
          request
              .getRequestDispatcher(MVCConfigConstant.FORWARD_BASE_PATH + location)
              .forward(request, this.context.getResponse());

          return;
        } else if (RenderType.FREEMARKER.equalsIgnoreCase(type)) {
          // FreeMarker 渲染
          Configuration cfg = new Configuration();
          // 指定模板从何处加载的数据源,这里设置成一个文件目录。
          cfg.setDirectoryForTemplateLoading(
              new File(ConfigConstant.ROOT_PATH + MVCConfigConstant.FORWARD_BASE_PATH));
          // 指定模板如何检索数据模型
          cfg.setObjectWrapper(new DefaultObjectWrapper());
          cfg.setDefaultEncoding("utf-8");

          Template template = cfg.getTemplate(location);
          template.setEncoding("utf-8");

          template.process(this.context.getModel(), this.context.getWriter());

          return;
        } else if (RenderType.ACTION.equalsIgnoreCase(type)) {
          // ACTION 重定向
          handleActionRedirect(context, location, baseUrl);

          return;
        } else if (RenderType.OUT.equalsIgnoreCase(type) || location.trim().length() == 0) {
          this.context.getWriter().print(location);
          this.context.getWriter().flush();

          return;
        }
      }

      if (isOut) {
        this.context.getWriter().print(retn);
        this.context.getWriter().flush();
      }
    }
  }