首页

定义BeanUtil工具类将请求参数转换为对应的DTO对象集合、获取或重写DTO对象代码示例

标签:BeanUtil,bean对象工具类,bean传值对象     发布时间:2018-12-01   

一、前言

定义BeanUtil工具类,获取指定类型的请求类型对象或对象集、对指定DTO类型进行重写、对象方法的绑定、获取指定类映射DTO对象等方法操作,详情参见代码示例。

二、代码示例

package test;@b@@b@import java.lang.reflect.InvocationTargetException;@b@import java.lang.reflect.Method;@b@import java.util.ArrayList;@b@import java.util.List;@b@import java.util.Map;@b@import java.util.StringTokenizer;@b@import javax.servlet.http.HttpServletRequest;@b@@b@public class BeanUtil {@b@@b@	public static List getDTOList(HttpServletRequest request, Class dtoClass)@b@			throws Exception {@b@		int k;@b@		List dtoList = null;@b@		if ((dtoClass == null) || (request == null))@b@			return dtoList;@b@		Method[] methods = dtoClass.getMethods();@b@		String[] fieldNames = new String[methods.length];@b@		String[][] values = new String[methods.length][];@b@		int maxSize = 0;@b@		for (int i = 0; i < methods.length; ++i) {@b@			String methodName = methods[i].getName();@b@			Class[] type = methods[i].getParameterTypes();@b@			if ((type.length == 1) && (methodName.length() > 3)@b@					&& (methodName.startsWith("set"))) {@b@				String name = methodName.substring(3, 4).toLowerCase()@b@						+ methodName.substring(4);@b@				fieldNames[i] = name;@b@				values[i] = request.getParameterValues(name);@b@				if ((values[i] != null) && (values[i].length > maxSize)) {@b@					maxSize = values[i].length;@b@				}@b@			}@b@		}@b@@b@		if (maxSize > 0) {@b@			dtoList = new ArrayList();@b@			for (k = 0; k < maxSize; ++k) {@b@				Object dtoObj = dtoClass.newInstance();@b@				for (int i = 0; i < methods.length; ++i) {@b@					try {@b@						if ((fieldNames[i] != null) && (values[i] != null)@b@								&& (values[i].length > 0)) {@b@							Class[] type = methods[i].getParameterTypes();@b@							String defvalue = values[i][(values[i].length - 1)];@b@							String strvalue = getArrayValue(values[i], k,@b@									defvalue);@b@							Object objValue = getBindValue(strvalue, type[0]);@b@@b@							if (objValue != null) {@b@								Object[] value = { objValue };@b@								invokeMothod(dtoObj, methods[i].getName(),@b@										type, value);@b@							}@b@						}@b@					} catch (Exception ex) {@b@						ex.printStackTrace();@b@					}@b@				}@b@				dtoList.add(dtoObj);@b@			}@b@		}@b@		return dtoList;@b@	}@b@@b@	public static Object getDTO(HttpServletRequest request, Class dtoClass)@b@			throws Exception {@b@		Object dtoObj = null;@b@		if ((dtoClass == null) || (request == null))@b@			return dtoObj;@b@		dtoObj = dtoClass.newInstance();@b@		setDTOValue(request, dtoObj);@b@		return dtoObj;@b@	}@b@@b@	public static void setDTOValue(HttpServletRequest request, Object dto)@b@			throws Exception {@b@		if ((dto == null) || (request == null))@b@			return;@b@		Method[] methods = dto.getClass().getMethods();@b@		for (int i = 0; i < methods.length; ++i) {@b@			try {@b@				String methodName = methods[i].getName();@b@				Class[] type = methods[i].getParameterTypes();@b@				if ((methodName.length() > 3) && (methodName.startsWith("set"))@b@						&& (type.length == 1)) {@b@					String name = methodName.substring(3, 4).toLowerCase()@b@							+ methodName.substring(4);@b@@b@					Object objValue = getBindValue(request, name, type[0]);@b@					if (objValue != null) {@b@						Object[] value = { objValue };@b@						invokeMothod(dto, methodName, type, value);@b@					}@b@				}@b@			} catch (Exception ex) {@b@				ex.printStackTrace();@b@			}@b@		}@b@	}@b@@b@	public static Object invokeMothod(Object classObject, String strMethodName)@b@			throws NoSuchMethodException, SecurityException,@b@			IllegalAccessException, IllegalArgumentException,@b@			InvocationTargetException {@b@		return invokeMothod(classObject, strMethodName, null, null);@b@	}@b@@b@	public static Object invokeMothod(Object classObject, String strMethodName,@b@			Class[] argsType, Object[] args) throws NoSuchMethodException,@b@			SecurityException, IllegalAccessException,@b@			IllegalArgumentException, InvocationTargetException {@b@		Method concatMethod = classObject.getClass().getMethod(strMethodName,@b@				argsType);@b@		return concatMethod.invoke(classObject, args);@b@	}@b@@b@	public static Object getBindValue(HttpServletRequest request,@b@			String bindName, Class bindType) {@b@		String value = request.getParameter(bindName);@b@		return getBindValue(value, bindType);@b@	}@b@@b@	public static Object getBindValue(String value, Class bindType) {@b@		if ((value == null) || (value.trim().length() == 0))@b@			return null;@b@		String typeName = bindType.getName();@b@		if (typeName.equals("java.lang.String")) {@b@			return value;@b@		}@b@		if (typeName.equals("int")) {@b@			return new Integer(value);@b@		}@b@		if (typeName.equals("long")) {@b@			return new Long(value);@b@		}@b@		if (typeName.equals("boolean")) {@b@			return new Boolean(value);@b@		}@b@		if (typeName.equals("fload")) {@b@			return new Float(value);@b@		}@b@		if (typeName.equals("double")) {@b@			return new Double(value);@b@		}@b@		return value;@b@	}@b@@b@	public static String getArrayValue(String[] array, int k, String defvalue) {@b@		if ((array != null) && (array.length > k)) {@b@			return array[k];@b@		}@b@		return defvalue;@b@	}@b@@b@	public static int parseInt(String strValue, int defval) {@b@		try {@b@			return Integer.parseInt(strValue);@b@		} catch (Exception ex) {@b@		}@b@		return defval;@b@	}@b@@b@	public static String getValueByBindName(HttpServletRequest request,@b@			String bindName, String defValue) {@b@@b@		if ((bindName == null) || (bindName.length() == 0))@b@			return null;@b@@b@		String[] names = strToArray(bindName, ".");@b@		if (names == null)@b@			return null;@b@@b@		Object curObj = null;@b@		for (int i = 0; i < names.length; ++i) {@b@			String name = names[i].trim();@b@			int midCharB = name.indexOf("[");@b@			if (midCharB > 0) {@b@@b@				while (true) {@b@					if (midCharB <= 0)@b@						break;@b@					int midCharE = name.indexOf("]", midCharB);@b@					if (midCharE == -1)@b@						return null;@b@					String attrName = name.substring(0, midCharB - 1);@b@					String strIndex = name@b@							.substring(midCharB + 1, midCharE - 1);@b@					int index = parseInt(strIndex, -1);@b@					if (index == -1) {@b@						return null;@b@					}@b@					curObj = setCurrentObject(request, curObj, attrName);@b@					if (curObj == null)@b@						return defValue;@b@					List list = (List) curObj;@b@					curObj = list.get(index);@b@					if (curObj == null)@b@						return defValue;@b@					midCharB = name.indexOf("[", midCharB + 1);@b@				}@b@@b@			}@b@@b@			curObj = setCurrentObject(request, curObj, name);@b@@b@			if (curObj == null)@b@				return defValue;@b@		}@b@@b@		if (curObj == null)@b@			label204: return defValue;@b@@b@		return curObj.toString();@b@	}@b@@b@	public static Object setCurrentObject(HttpServletRequest request,@b@			Object obj, String name) {@b@		if (obj == null) {@b@			obj = request.getAttribute(name);@b@		} else if (obj instanceof Map) {@b@			obj = ((Map) obj).get(name);@b@		} else {@b@			String menthodName = "get" + name.substring(0, 1).toUpperCase()@b@					+ name.substring(1);@b@			try {@b@				obj = invokeMothod(obj, menthodName);@b@			} catch (Exception ex) {@b@				obj = null;@b@			}@b@		}@b@@b@		return obj;@b@	}@b@@b@	public static String[] strToArray(String str, String delim) {@b@		if ((str == null) || (delim == null)) {@b@			return null;@b@		}@b@		List strVec = new ArrayList();@b@@b@		StringTokenizer st = new StringTokenizer(str, delim);@b@@b@		while (st.hasMoreElements()) {@b@			strVec.add((String) st.nextElement());@b@		}@b@		String[] strArray = new String[strVec.size()];@b@		strVec.toArray(strArray);@b@		return strArray;@b@	}@b@@b@}