BeanUtils(前端赋值给后台,忽略空属性)
package com.drn.core.util; import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set; import org.springframework.beans.BeansException;
import org.springframework.beans.FatalBeanException;
import org.springframework.util.Assert; public abstract class BeanUtils extends org.springframework.beans.BeanUtils { public static void copyProperties(Object source, Object target) throws BeansException {
Assert.notNull(source, "Source must not be null");
Assert.notNull(target, "Target must not be null");
Class<?> actualEditable = target.getClass();
PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);
for (PropertyDescriptor targetPd : targetPds) {
if (targetPd.getWriteMethod() != null) {
PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName());
if (sourcePd != null && sourcePd.getReadMethod() != null) {
try {
Method readMethod = sourcePd.getReadMethod();
if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
readMethod.setAccessible(true);
}
Object value = readMethod.invoke(source);
// 这里判断以下value是否为空 当然这里也能进行一些特殊要求的处理 例如绑定时格式转换等等
if (value != null) {
boolean isEmpty = false;
if (value instanceof Set) {
Set s = (Set) value;
if (s == null || s.isEmpty()) {
isEmpty = true;
}
} else if (value instanceof Map) {
Map m = (Map) value;
if (m == null || m.isEmpty()) {
isEmpty = true;
}
} else if (value instanceof List) {
List l = (List) value;
if (l == null || l.size() < 1) {
isEmpty = true;
}
} else if (value instanceof Collection) {
Collection c = (Collection) value;
if (c == null || c.size() < 1) {
isEmpty = true;
}
} if (!isEmpty) {
Method writeMethod = targetPd.getWriteMethod();
if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
writeMethod.setAccessible(true);
}
writeMethod.invoke(target, value);
}
}
} catch (Throwable ex) {
throw new FatalBeanException("Could not copy properties from source to target", ex);
}
}
}
}
}
}
BeanUtils(前端赋值给后台,忽略空属性)的更多相关文章
- 微信小程序----关于变量对象data 和 前端wxml取后台js变量值
(一)页面变量对象data 对象data 有两个方面用途 第一,前端wxml的数据渲染是通过设置此对象中定义的变量进行关联展现的 第二,定义JS页面中的页面局部变量,使其整个页面中可使用或调用 对象d ...
- ASP.NET MVC/Core表单提交后台模型二级属性验证问题
起因 这个是网友在官网论坛的提问:https://fineui.com/bbs/forum.php?mod=viewthread&tid=22237 重新问题 本着务实求真的态度,我们先来复现 ...
- 使用XmlSerializer序列化可空属性
使用XmlSerializer可以方便的将对象序列化为xml,实现应用之间的数据交互.但是XmlSerializer却不能很好地序列化类型中的可空字段. 例如,有如下定义的类Person: [Seri ...
- Json Serialize 忽略特定属性
Json Serialize 忽略特定属性 Json Serialize SerializeFilter 忽略特定属性 key words:Json Serialize jackson fastjso ...
- android支付宝app支付(原生态)-包括android前端与java后台
本文讲解了 android开发的原生态app集成了支付宝支付, 还提供了java后台服务器处理支付宝支付的加密代码, app前端与java后台服务器使用json数据格式交互信息,java后台服务主要用 ...
- android 集成支付宝app支付(原生态)-包括android前端与java后台
本文讲解了 android开发的原生态app集成了支付宝支付, 还提供了java后台服务器处理支付宝支付的加密代码, app前端与java后台服务器使用json数据格式交互信息,java后台服务主要用 ...
- Newtonsoft.Json高级用法 1.忽略某些属性 2.默认值的处理 3.空值的处理 4.支持非公共成员 5.日期处理 6.自定义序列化的字段名称
手机端应用讲究速度快,体验好.刚好手头上的一个项目服务端接口有性能问题,需要进行优化.在接口多次修改中,实体添加了很多字段用于中间计算或者存储,然后最终用Newtonsoft.Json进行序列化返回数 ...
- EntityFramework Core饥饿加载忽略导航属性问题
前言 .NET Core项目利用EntityFramework Core作为数据访问层一直在进行中,一直没有过多的去关注背后生成的SQL语句,然后老大捞出日志文件一看,恩,有问题了,所以本文产生了,也 ...
- 将DataRow赋值给model中同名属性
/// <summary> /// 将DataRow赋值给model中同名属性 /// </summary> /// <typeparam name="T&qu ...
随机推荐
- 地图API示例
http://developer.baidu.com/map/jsdemo.htm#i6_2
- 关于参数net_buffer_length How MySQL Uses Memory
http://dev.mysql.com/doc/refman/5.6/en/memory-use.html The following list indicates some of the ways ...
- MFC学习碎片
(1)MFC程序中弹出框: MessageBox(str,_T("程序执行结果"),MB_OK); AfxMessageBox("Hello&quo ...
- 【C++】String类中的运算符重载
模块化设计: 头文件: <span style="font-size:18px;"> #ifndef operator_operator_h #define opera ...
- c# 无法加载xxx.dll 找不到指定的模块(如何指定文件夹)
如果直接放在项目运行目录,例如bin/debug可以直接加载,但是这样比较乱. 如果在放debug里面的一个文件夹里面,有可能会报错“无法加载xxx.dll 找不到指定的模块”. 如果路径写成这样就会 ...
- linux删除多行
光标放到行dd:删除所在行 光标放到行Ndd: 删除所在行下的N行
- asp.net web site中reference的version的autoupdate
https://stackoverflow.com/questions/833924/visual-studio-stop-auto-update-on-references This is vali ...
- When Cyber Security Meets Machine Learning 机器学习 安全分析 对于安全领域的总结很有用 看未来演进方向
链接:http://ucys.ugr.es/jnic2016/docs/MachineLearning_LiorRokachJNIC2016.pdf https://people.eecs.berke ...
- 13.QT多窗口切换list
Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(this); list = ...
- (转)IOS http请求的get 和 post的请求的区别
从表面的意思看get 和 post的区别get就是获取数据,post就是发送数据.这个是误区.其实两者都可以的,在IOS向服务器发送请求里面可以带参数. 那么这些误区是怎么出现的呢?先看看一下对htt ...