- 浏览: 476123 次
- 性别:
- 来自: 福州
文章分类
最新评论
-
学渣村的好村长:
很
java 列表转树形 -
wawj0819:
...
java 列表转树形 -
fireinjava:
LyAn_爱踢爱死 写道我有疑问,关于数组时你说collect ...
mybatis sql in 查询 -
LyAn_爱踢爱死:
我有疑问,关于数组时你说collection="ar ...
mybatis sql in 查询 -
yrsheng:
不行!楼主你真的试过吗?
Spring mvc 传递数组
自己写了个copyProperties方法,虽没apache与spring的强大,但功能自己够用就好,而且不用引入第三方库。
package bean; import java.lang.reflect.Method; /** * @version 1.0 * @author fireinjava * @data Dec 11, 2009 * @描述: 存放字段及其要用到的对应get(来自sourceBean)与set(来自targetBean)方法 */ public class PropertyMethod { private String propertyName; private Method methodRead; private Method methodWrite; public String getPropertyName() { return propertyName; } public void setPropertyName(String propertyName) { this.propertyName = propertyName; } public Method getMethodRead() { return methodRead; } public void setMethodRead(Method methodRead) { this.methodRead = methodRead; } public Method getMethodWrite() { return methodWrite; } public void setMethodWrite(Method methodWrite) { this.methodWrite = methodWrite; } }
package bean; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; /** * @version 1.0 * @author fireinjava * @data Dec 11, 2009 * @描述:拷贝Bean(适用于标准JavaBean,且拷贝的属性类型必须一致) */ public class BeanUtils { public static void copyProperties(Object source, Object target) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { copyProperties(source, target, null); } /** * 当某个属性不必复制或两个Bean属性名称一样且类型不一样时用(得另外设值) */ public static void copyProperties(Object source, Object target, String[] ignoreProperties) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { if (source == null) throw new IllegalArgumentException("Source must not be null"); else if (target == null) throw new IllegalArgumentException("Target must not be null"); List<String> ignoreList = null; if (ignoreProperties != null) ignoreList = Arrays.asList(ignoreProperties); List<PropertyMethod> listPm = new ArrayList<PropertyMethod>(); Map<String, Method> mapMethodWrite = new HashMap<String, Method>(); Map<String, Method> mapMethodRead = new HashMap<String, Method>(); Method[] methodTarget = target.getClass().getMethods(); for (Method mt : methodTarget) if (mt.getName().startsWith("set")) { String propertyName = mt.getName().substring(3, 4).toLowerCase() + mt.getName().substring(4); if (ignoreList == null || !ignoreList.contains(propertyName)) mapMethodWrite.put(propertyName, mt); } Method[] methodSource = source.getClass().getMethods(); for (Method ms : methodSource) if (ms.getName().startsWith("get")) mapMethodRead.put(ms.getName().substring(3, 4).toLowerCase() + ms.getName().substring(4), ms); else if (ms.getName().startsWith("is")) mapMethodRead.put(ms.getName().substring(2, 3).toLowerCase() + ms.getName().substring(3), ms); Iterator<String> iter = mapMethodWrite.keySet().iterator(); while (iter.hasNext()) { String propertyName = iter.next(); if (mapMethodRead.get(propertyName) != null) { PropertyMethod pm = new PropertyMethod(); pm.setPropertyName(propertyName); pm.setMethodRead(mapMethodRead.get(propertyName)); pm.setMethodWrite(mapMethodWrite.get(propertyName)); listPm.add(pm); } } for (PropertyMethod pm : listPm) { Object[] obj = new Object[1]; obj[0] = pm.getMethodRead().invoke(source, new Object[0]); pm.getMethodWrite().invoke(target, obj); } } }
其中List<PropertyMethod> listPm = new ArrayList<PropertyMethod>();用来存放完整的属性及属性Get/set方法,不借助 PropertyMethod而直接用两个Map也可。
另外两种copyProperties:
org.apache.commons.beanutils.BeanUtils.copyProperties(target, source);
//source中的Timestamp类型属性值不可为null 否则会报错
org.springframework.beans.BeanUtils.copyProperties(source, target);
发表评论
-
将编码为GBK的Java源文件批量转为UTF-8
2016-09-10 11:41 1520转自 http://my.oschina.net/binny ... -
判断上传的文件是否图片格式
2015-08-16 01:49 4498转自 http://stackoverflow.co ... -
StreamCorruptedException invalid type code 34
2014-12-15 22:47 1164反序列的时候抛错: java.io.StreamCor ... -
ant property 修改
2013-12-17 16:57 2937ant借助antcrontrib.jar修改propert ... -
websphere liberty 安装 部署
2013-08-21 00:24 5836websphere application server l ... -
java 列表转树形
2013-02-01 10:31 27890往往要将数据库中的菜单配置转成树形结构展示到前台,这边提供 ... -
判断文件是否UTF-8编码
2012-12-04 08:50 3823判断文件是否UTF-8编码: 对于UTF-8编码 ... -
java replaceAll 的类似group用法
2012-08-30 23:25 2255replaceAll比较特殊的用法介绍一个: eg:把中括号 ... -
java.lang.UnsupportedClassVersionError: Bad version number in .class file
2012-05-25 10:42 1288项目在Tomcat跑的时候报Bad version numbe ... -
weblogic出现response already committed
2011-11-30 15:06 5725jsp页面导出时在weblogic下会出现response a ... -
XML解析 Invalid byte 2 of 2-byte UTF-8 sequence
2011-10-10 00:32 1769String xmlStr = "<?xml ... -
VBS脚本设置Java环境变量
2011-07-26 16:09 2111转自 http://blog.csdn.net/sunyuji ... -
jsp获取网站域名 域名解析
2011-07-22 17:23 14035部署主机如果有弄域名解析的话访问http://www.doma ... -
在顶层iframe页面添加图片div
2011-06-27 11:59 2790又解决了一个问题...之前想太复杂了... 父页面ht ... -
apache转发 js 跨域
2011-01-24 10:20 5089安装Apache HTTP Server 打开apache安 ... -
MyEclipse 启动tomcat的时候报cannot connect to vm
2010-12-22 00:58 3111MyEclipse 启动tomcat的时候突然报cannot ... -
Eclipse常用快捷键
2010-09-20 10:23 1345http://www.eclipse.org/downlo ... -
table导出为Excel jxl
2010-09-02 16:59 3400平时用js导出table有些不足,导出的Excel内容也不够好 ... -
Java打Tag CvsClient
2010-08-26 21:16 2325增量编译的时候需要一个一个文件打Tag,不方便,网上也没发现现 ... -
Tomcat布署
2010-08-16 19:25 11401.项目布署 tomcat安装目录下加个配置文件即可 ...
相关推荐
主要介绍了java Beanutils.copyProperties( )用法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
一个BeanUtils.copyProperties的小型快速替代。 起因 由于BeanUtils(Spring或Apache Commons)的copyProperties实现是利用反射实现的,它在大量调用时具有比较严重的性能问题。 BeanMapper通过javassist类库实现在...
apache的BeanUtils的copyProperty以及copyProperties方法不支持String与java.util.Date的类型相互转换 该包对源码进行了部分修改,使其支持对String与Date之间的类型转换。 2008-07-17更新: 增加了...
apache的BeanUtils的copyProperty以及copyProperties方法不支持String与java.util.Date的类型相互转换; 该包对源码进行了部分修改,使其支持对String与Date之间的类型转换。 2008-07-17更新: 增加了BeanUtils的copy...
继承org.springframework.beans.BeanUtils 修改copyProperties方法赋值时,为null时不进行处理,方便开发工具类
apache的BeanUtils的copyProperty以及copyProperties方法不支持String与java.util.Date的类型相互转换 该包对源码进行了部分修改,使其支持对String与Date之间的类型转换. 注:该包在对null的Date转换时会抛出No value...
BeanUtils.copyProperties 可以直接copy对象中的内容 过滤采用stream 随机数random 断点调试 读懂报错信息 断言到底是检测什么? 单元测试就一定要断言吗 无用的断言 关于错误 就是{日志+异常} 里层外层的错误报告 ...
业务代码解耦方案与案例源码,类似于BeanUtils.copyProperties工具,不仅能进行相同字段名相同类型的字段值复制,还能进行不同类型或字段的转换
shoppingmall2 ... 除此之外,还可以使用BeanUtils.copyProperties,例如Account account = modelMapper.map(AccountDto.Create dto,Account.class); 像这样。 用@Valid等检查域上的对象,并将结果值作为Bi
public class SysUserGroup implements java.io.Serializable { private Integer id; private String remark; // 备注 private String name; // 部门名称 private String principal; // 部门负责...