⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 beanutils.java

📁 jdo开发实例,一个功能全面的oa系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.gzrealmap.oa.servlet;

/**
 * <p>Title: OA</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: Realmap.cc</p>
 * @author not attributable
 * @version OA2003b
 */
import java.beans.*;
import java.util.*;
import java.sql.*;
import java.lang.reflect.Method;
import java.util.Enumeration;
import javax.servlet.ServletRequest;
import javax.servlet.http.*;
import java.util.*;

public class BeanUtils {
  private BeanUtils() {
  }

  private static BeanUtils u = new BeanUtils();
  public static BeanUtils getInstance() {
    return u;
  }
/**
 * 用于直接生成一个对象.
 */
  public Object initRequestObject(ServletRequest servletRequest,
                                  String className) {
    Object result = newObject(className);
    requestObject(result, servletRequest);
    return result;
  }

  /**
   * 新建一个空对象.
   */
  public Object newObject(String className) {
    Object result = null;
    try {
      result = java.beans.Beans.instantiate(this.
                                            getClass().getClassLoader(),
                                            className);
    }
    catch (Exception exc) {
      exc.printStackTrace();
    }
    return result;
  }
/**
 * 从 servletRequest 获取一个对象
 */
  public void requestObject(Object object, ServletRequest servletRequest) {
    try {
      introspect(object, servletRequest);
    }
    catch (Exception exc) {
      exc.printStackTrace();
    }
  }

  public void setRequestParameter(ServletRequest servletRequest, Object bean,
                                  String name) {
    try {
      introspecthelper(bean, name,
                       servletRequest.getParameter(name), servletRequest, name, false);
    }
    catch (Exception exc) {
      exc.printStackTrace();
    }
  }
  /**
   * 为一个对象设置新值
   */
  public void setBeanParameter(Object bean, String prop, Object Value) throws
      Exception {

    java.lang.reflect.Method method = null;
    Class type = null;
    Class propertyEditorClass = null;
    try {
      java.beans.BeanInfo info
          = java.beans.Introspector.getBeanInfo(bean.getClass());
      if (info != null) {
        java.beans.PropertyDescriptor pd[]
            = info.getPropertyDescriptors();
        for (int i = 0; i < pd.length; i++) {
          if (pd[i].getName().equals(prop)) {
            method = pd[i].getWriteMethod();
            type = pd[i].getPropertyType();
            propertyEditorClass = pd[i].getPropertyEditorClass();
            break;
          }
        }
      }
      if (method != null) {
        if (type.isArray()) {
          return;
        }
        else {
          if (Value == null || (prop != null && Value.equals(""))) {
            return;
          }
          Object oval = Value;
          if (Value instanceof String) {
            oval = convert(prop, (String) Value, type, propertyEditorClass);
          }
          if (oval != null) {
            method.invoke(bean, new Object[] {oval});
          }
        }
      }
    }
    catch (Exception ex) {
      throw new Exception(ex);
    }
  }

  /**
   * 从一个对象获取值
   */
  public Object getBeanParameter(Object bean, String prop) {
    Object result = null;
    java.lang.reflect.Method method = null;
    Class type = null;
    Class propertyEditorClass = null;
    try {
      java.beans.BeanInfo info
          = java.beans.Introspector.getBeanInfo(bean.getClass());
      if (info != null) {
        java.beans.PropertyDescriptor pd[]
            = info.getPropertyDescriptors();
        for (int i = 0; i < pd.length; i++) {
          if (pd[i].getName().equals(prop)) {
            method = pd[i].getReadMethod();
            break;
          }
        }
      }
      if (method != null) {
        result = method.invoke(bean, null);
      }
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
    return result;
  }

  private static void introspect(Object bean, ServletRequest request) throws
      Exception {
    Enumeration e = request.getParameterNames();
    while (e.hasMoreElements()) {
      String name = (String) e.nextElement();
      String value = request.getParameter(name);
      introspecthelper(bean, name, value, request, name, true);
    }
  }

  public static void introspectHastable(Object bean, Hashtable values) throws
      Exception {
    Enumeration e = values.keys();
    while (e.hasMoreElements()) {
      String name = (String) e.nextElement();
      String value = (String)values.get(name);
      internalIntrospecthelperHash(bean, name, value, name ,true);
    }
  }

  private static void internalIntrospecthelperHash(Object bean, String prop,
                                               String value,
                                               String param,
                                               boolean ignoreMethodNF) throws
      Exception {
    java.lang.reflect.Method method = null;
    Class type = null;
    Class propertyEditorClass = null;
    try {
      java.beans.BeanInfo info
          = java.beans.Introspector.getBeanInfo(bean.getClass());
      if (info != null) {
        java.beans.PropertyDescriptor pd[]
            = info.getPropertyDescriptors();
        for (int i = 0; i < pd.length; i++) {
          if (pd[i].getName().equals(prop)) {
            method = pd[i].getWriteMethod();
            type = pd[i].getPropertyType();
            propertyEditorClass = pd[i].getPropertyEditorClass();
            break;
          }
        }
      }
      if (method != null) {
        if (type.isArray()) {
            throw new Exception("not support Array");
        }
        else {
          if (value == null || (param != null && value.equals(""))) {
            return;
          }
          Object oval = convert(prop, value, type, propertyEditorClass);
          if (oval != null) {
            method.invoke(bean, new Object[] {oval});
          }
        }
      }
    }
    catch (Exception ex) {
      throw new Exception(ex);
    }
    if (!ignoreMethodNF && (method == null)) {
      if (type == null) {
        throw new Exception(
            "error.beans.noproperty");
      }
      else {
        throw new Exception(
            "error.beans.nomethod.setproperty");
      }
    }
  }


  private static void introspecthelper(Object bean, String prop,
                                       String value, ServletRequest request,
                                       String param, boolean ignoreMethodNF) throws
      Exception {
    internalIntrospecthelper(
        bean, prop, value, request, param, ignoreMethodNF);
  }

  private static void internalIntrospecthelper(Object bean, String prop,
                                               String value,
                                               ServletRequest request,
                                               String param,
                                               boolean ignoreMethodNF) throws
      Exception {
    java.lang.reflect.Method method = null;
    Class type = null;
    Class propertyEditorClass = null;
    try {
      java.beans.BeanInfo info
          = java.beans.Introspector.getBeanInfo(bean.getClass());
      if (info != null) {
        java.beans.PropertyDescriptor pd[]
            = info.getPropertyDescriptors();
        for (int i = 0; i < pd.length; i++) {
          if (pd[i].getName().equals(prop)) {
            method = pd[i].getWriteMethod();
            type = pd[i].getPropertyType();
            propertyEditorClass = pd[i].getPropertyEditorClass();
            break;
          }
        }
      }
      if (method != null) {
        if (type.isArray()) {
          if (request == null) {
            throw new Exception("jsp.error.beans.setproperty.noindexset");
          }
          Class t = type.getComponentType();
          String[] values = request.getParameterValues(param);

          //XXX Please check.
          if (values == null) {
            return;
          }
          if (t.equals(String.class)) {
            method.invoke(bean, new Object[] {values});
          }
          else {
            Object tmpval = null;
            createTypedArray(prop, bean, method, values, t,
                             propertyEditorClass);
          }
        }
        else {
          if (value == null || (param != null && value.equals(""))) {
            return;
          }
          Object oval = convert(prop, value, type, propertyEditorClass);
          if (oval != null) {
            method.invoke(bean, new Object[] {oval});
          }
        }
      }
    }
    catch (Exception ex) {
      throw new Exception(ex);
    }
    if (!ignoreMethodNF && (method == null)) {
      if (type == null) {
        throw new Exception(
            "error.beans.noproperty");
      }
      else {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -