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

本文介绍如何使用Java的BeanUtil工具类进行DTO转换,包括将请求参数转换为DTO对象集合、重写DTO对象及对象方法绑定。通过提供的代码示例,详细展示了从HttpServletRequest获取DTO对象的方法。
摘要由CSDN通过智能技术生成

一、前言

定义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  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  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  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  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@}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值