书中的FormBeanUtil这个类怎么代码里没有

04-06-24 mozhou
能不能提供,谢谢

banq
2004-06-26 18:35

/**
 * 工具类,相关ActionForm 或Model之类的工具箱
 *
 * <p>Copyright: Jdon.com Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author banq
 * @version 1.0
 */
public final class FormBeanUtil {

  public final static String module = FormBeanUtil.class.getName();

  public final static String FORWARD_SUCCESS_NAME = "success";
  public final static String FORWARD_FAILURE_NAME = "failure";

  /**
   * 将ActionForm保存在struts_config.xml定义的attribute中
   * @param form
   * @param mapping
   * @param request
   */
  public static void saveActionForm(ActionForm form, ActionMapping mapping,
                                    HttpServletRequest request) {
    if ( (form != null) && (mapping.getAttribute() != null)) {
      if ("request".equals(mapping.getScope())) {
        request.setAttribute(mapping.getAttribute(), form);
      } else {
        HttpSession session = request.getSession();
        session.setAttribute(mapping.getAttribute(), form);
        request.setAttribute(mapping.getAttribute(), form);
      }
    }
  }

  /**
   * 将保存在struts_config.xml定义的attribute中ActionForm取出
   * @param form
   * @param mapping
   * @param request
   */
  public static ActionForm loadActionForm(ActionMapping mapping,
                                          HttpServletRequest request) {
    if ("request".equals(mapping.getScope())) {
      return (ActionForm) request.getAttribute(mapping.getAttribute());
    } else {
      HttpSession session = request.getSession();
      return (ActionForm) session.getAttribute(mapping.getAttribute());
    }

  }

  /**
   * 删除保存在attribute中的ActionForm实例
   * @param mapping
   * @param request
   */
  public static void removeActionForm(ActionMapping mapping,
                                      HttpServletRequest request) {
    if (mapping.getAttribute() != null) {
      if ("request".equals(mapping.getScope()))
        request.removeAttribute(mapping.getAttribute());
      else {
        HttpSession session = request.getSession();
        session.removeAttribute(mapping.getAttribute());
        request.removeAttribute(mapping.getAttribute());
      }
    }
  }

  public static String getFormName(ActionMapping mapping) throws Exception{
    String formName = "NoFormName Error!";
    if (mapping.getName() != null)
      formName = mapping.getName();
    else if ( (mapping.getAttribute() != null))
      formName = mapping.getAttribute();
    else
       throw new Exception("not found the attribute or name in action config");
    return formName;
  }

  public static boolean validateAction(String actionName, ActionMapping mapping) {
    boolean res = true;
    int result = actionTransfer(actionName); //如果没有使用规定名称
    if (result == 0)
      res = false;

    if (mapping.findForward(actionName) == null) //如果配置文件没有该名称
      res = false;

    return res;

  }

  public static String getName(HttpServletRequest request) throws Exception {
    Principal principal = request.getUserPrincipal();
    if (principal == null) {
      Debug.logError(" No Principal", module);
      throw new Exception(" No Principal");
    }
    return principal.getName();
  }

  public static ActionErrors notNull(Object object, String errorsInfo) {
    ActionErrors errors = new ActionErrors();
    String Id = (String) object;
    if (object == null) {
      errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(errorsInfo));
    }
    return errors;
  }

  /**
   * create a Event
   */
  public static EventModel createEvent(ModelForm form, Model model) throws
      Exception {
    EventModel em = new EventModel();
    em.setActionName(module);
    try {
      PropertyUtils.copyProperties(model, form);
      em.setModel(model);
      String action = form.getAction();
      em.setActionType(FormBeanUtil.actionTransfer(action));
    } catch (Exception ex) {
      Debug.logError("create Event error:" + ex, module);
      throw new Exception(ex);
    }
    return em;
  }

  /**
   * 将字符串数据操作类型转为数字型,提高以后的处理速度
   * @param actionName
   * @return
   */
  public static int actionTransfer(String actionName) {
    if (actionName.equalsIgnoreCase(ModelForm.CREATE_STR))
      return EventSupport.CREATE;
    else if (actionName.equalsIgnoreCase(ModelForm.EDIT_STR))
      return EventSupport.EDIT;
    else if (actionName.equalsIgnoreCase(ModelForm.UPDATE_STR))
      return EventSupport.UPDATE;

    else if (actionName.equalsIgnoreCase(ModelForm.DELETE_STR))
      return EventSupport.DELETE;
    else if (actionName.equalsIgnoreCase(ModelForm.VIEW_STR))
      return EventSupport.VIEW;
    else if (actionName.equalsIgnoreCase(ModelForm.ADD_STR))
      return EventSupport.ADD;
    else if (actionName.equalsIgnoreCase(ModelForm.REMOVE_STR))
      return EventSupport.REMOVE;
    else
      return 0;
  }

猜你喜欢