Пример #1
0
  public static ActionForm setFormByQueryParam(ActionForm form, String queryParamName) {
    try {
      String paramSeparator = "~`~";
      String paramValueSeparator = "=`=";
      Class clazz = Class.forName(form.getClass().getName());
      Method method =
          clazz.getDeclaredMethod(
              "get" + queryParamName.substring(0, 1).toUpperCase() + queryParamName.substring(1),
              (Class[]) null);
      String allQueryParam = StringUtil.nullToEmptyOfObject(method.invoke(form, (Object[]) null));
      String[] allQueryParamArray = split(allQueryParam, paramSeparator);
      Method[] methods = clazz.getDeclaredMethods();
      // 针对当前form设值
      setClassProp(methods, allQueryParamArray, paramSeparator, paramValueSeparator, form);
      // 针对父form设值
      methods = clazz.getSuperclass().getDeclaredMethods();
      // form=form.getClass().getSuperclass().cast(form);
      setClassProp(methods, allQueryParamArray, paramSeparator, paramValueSeparator, form);

    } catch (Exception e) {
      System.out.println(e);
      System.out.println("error when setting values in Helper.setFormByQueryParam==" + e);
    }
    return form;
  }
Пример #2
0
  public static ActionForm setQueryParam(ActionForm form, String queryParamName) {
    try {
      StringBuffer sbf = new StringBuffer(100);
      String paramSeparator = "~`~";
      String paramValueSeparator = "=`=";
      Class clazz = Class.forName(form.getClass().getName());
      Method method =
          clazz.getDeclaredMethod(
              "get" + queryParamName.substring(0, 1).toUpperCase() + queryParamName.substring(1),
              (Class[]) null);
      Method[] methods = clazz.getDeclaredMethods();
      String allQueryParamName =
          StringUtil.nullToEmptyOfObject(method.invoke(form, (Object[]) null));
      allQueryParamName = paramSeparator + allQueryParamName + paramSeparator;
      // 针对当前form取值
      sbf =
          getAllClassProp(
              methods, allQueryParamName, paramSeparator, paramValueSeparator, form, sbf);
      // 针对父form取值
      methods = clazz.getSuperclass().getDeclaredMethods();
      sbf =
          getAllClassProp(
              methods, allQueryParamName, paramSeparator, paramValueSeparator, form, sbf);

      if (sbf != null && !"".equals(sbf.toString().trim())) {
        method =
            clazz.getDeclaredMethod(
                "set" + queryParamName.substring(0, 1).toUpperCase() + queryParamName.substring(1),
                String.class);
        method.invoke(form, sbf.toString());
      }
    } catch (Exception e) {
      System.out.println("error when setting values in Helper.setQueryParam==" + e);
    }
    return form;
  }
Пример #3
0
  @Override
  public void process(final HttpServletRequest request, final HttpServletResponse response)
      throws IOException, ServletException {
    // indicates that we are running in legacy KNS context
    LegacyUtils.beginLegacyContext();
    try {
      if (LOG.isInfoEnabled()) {
        LOG.info(
            new StringBuffer("Started processing request: '")
                .append(request.getRequestURI())
                .append("' w/ query string: '")
                .append(request.getQueryString())
                .append("'"));
      }

      try {
        strutsProcess(request, response);
      } catch (FileUploadLimitExceededException e) {
        ActionForward actionForward =
            processException(request, response, e, e.getActionForm(), e.getActionMapping());
        processForwardConfig(request, response, actionForward);
      } finally {
        KNSGlobalVariables.setKualiForm(null);
      }

      try {
        ActionForm form = WebUtils.getKualiForm(request);

        if (form != null && form instanceof KualiDocumentFormBase) {
          String docId = ((KualiDocumentFormBase) form).getDocId();
          if (docId != null) {
            MDC.put(MDC_DOC_ID, docId);
          }
        }

        String refreshCaller = request.getParameter(KRADConstants.REFRESH_CALLER);
        if (form != null
            && KualiDocumentFormBase.class.isAssignableFrom(form.getClass())
            && !KRADConstants.QUESTION_REFRESH.equalsIgnoreCase(refreshCaller)) {
          KualiDocumentFormBase docForm = (KualiDocumentFormBase) form;
          Document document = docForm.getDocument();
          String docFormKey = docForm.getFormKey();

          UserSession userSession =
              (UserSession) request.getSession().getAttribute(KRADConstants.USER_SESSION_KEY);

          if (WebUtils.isDocumentSession(document, docForm)) {
            getSessionDocumentService()
                .setDocumentForm(docForm, userSession, request.getRemoteAddr());
          }

          Boolean exitingDocument = (Boolean) request.getAttribute(KRADConstants.EXITING_DOCUMENT);

          if (exitingDocument != null && exitingDocument.booleanValue()) {
            // remove KualiDocumentFormBase object from session and
            // table.
            getSessionDocumentService()
                .purgeDocumentForm(
                    docForm.getDocument().getDocumentNumber(),
                    docFormKey,
                    userSession,
                    request.getRemoteAddr());
          }
        }

        if (LOG.isInfoEnabled()) {
          LOG.info(
              new StringBuffer("Finished processing request: '")
                  .append(request.getRequestURI())
                  .append("' w/ query string: '")
                  .append(request.getQueryString())
                  .append("'"));
        }

      } finally {
        // MDC docId key is set above, and also during super.process() in the call to
        // processActionForm
        MDC.remove(MDC_DOC_ID);
      }
    } finally {
      LegacyUtils.endLegacyContext();
    }
  }
  /**
   * Create an appropriate form bean in the appropriate scope, if one does not already exist.
   *
   * @param context FacesContext for the current request
   * @exception IllegalArgumentException if no ActionConfig for the specified action attribute can
   *     be located
   * @exception IllegalArgumentException if no FormBeanConfig for the specified form bean can be
   *     located
   * @exception IllegalArgumentException if no ModuleConfig can be located for this application
   *     module
   */
  public void createActionForm(FacesContext context) {

    // Look up the application module configuration information we need
    ModuleConfig moduleConfig = lookupModuleConfig(context);

    // Look up the ActionConfig we are processing
    String action = getAction();
    ActionConfig actionConfig = moduleConfig.findActionConfig(action);
    if (actionConfig == null) {
      throw new IllegalArgumentException("Cannot find action '" + action + "' configuration");
    }

    // Does this ActionConfig specify a form bean?
    String name = actionConfig.getName();
    if (name == null) {
      return;
    }

    // Look up the FormBeanConfig we are processing
    FormBeanConfig fbConfig = moduleConfig.findFormBeanConfig(name);
    if (fbConfig == null) {
      throw new IllegalArgumentException("Cannot find form bean '" + name + "' configuration");
    }

    // Does a usable form bean attribute already exist?
    String attribute = actionConfig.getAttribute();
    String scope = actionConfig.getScope();
    ActionForm instance = null;
    if ("request".equals(scope)) {
      instance = (ActionForm) context.getExternalContext().getRequestMap().get(attribute);
    } else if ("session".equals(scope)) {
      HttpSession session = (HttpSession) context.getExternalContext().getSession(true);
      instance = (ActionForm) context.getExternalContext().getSessionMap().get(attribute);
    }
    if (instance != null) {
      if (fbConfig.getDynamic()) {
        String className = ((DynaBean) instance).getDynaClass().getName();
        if (className.equals(fbConfig.getName())) {
          if (log.isDebugEnabled()) {
            log.debug(
                " Recycling existing DynaActionForm instance " + "of type '" + className + "'");
          }
          return;
        }
      } else {
        try {
          Class configClass = RequestUtils.applicationClass(fbConfig.getType());
          if (configClass.isAssignableFrom(instance.getClass())) {
            if (log.isDebugEnabled()) {
              log.debug(
                  " Recycling existing ActionForm instance "
                      + "of class '"
                      + instance.getClass().getName()
                      + "'");
            }
            return;
          }
        } catch (Throwable t) {
          throw new IllegalArgumentException(
              "Cannot load form bean class '" + fbConfig.getType() + "'");
        }
      }
    }

    // Create a new form bean instance
    if (fbConfig.getDynamic()) {
      try {
        DynaActionFormClass dynaClass = DynaActionFormClass.createDynaActionFormClass(fbConfig);
        instance = (ActionForm) dynaClass.newInstance();
        if (log.isDebugEnabled()) {
          log.debug(
              " Creating new DynaActionForm instance " + "of type '" + fbConfig.getType() + "'");
          log.trace(" --> " + instance);
        }
      } catch (Throwable t) {
        throw new IllegalArgumentException(
            "Cannot create form bean of type '" + fbConfig.getType() + "'");
      }
    } else {
      try {
        instance = (ActionForm) RequestUtils.applicationInstance(fbConfig.getType());
        if (log.isDebugEnabled()) {
          log.debug(" Creating new ActionForm instance " + "of type '" + fbConfig.getType() + "'");
          log.trace(" --> " + instance);
        }
      } catch (Throwable t) {
        throw new IllegalArgumentException(
            "Cannot create form bean of class '" + fbConfig.getType() + "'");
      }
    }

    // Configure and cache the form bean instance in the correct scope
    ActionServlet servlet =
        (ActionServlet)
            context.getExternalContext().getApplicationMap().get(Globals.ACTION_SERVLET_KEY);
    instance.setServlet(servlet);
    if ("request".equals(scope)) {
      context.getExternalContext().getRequestMap().put(attribute, instance);
    } else if ("session".equals(scope)) {
      context.getExternalContext().getSessionMap().put(attribute, instance);
    }
  }