一、BeanUtils介绍

commons-beanutils是利用反射机制对JavaBean的属性进行处理,提供了对于JavaBean的各种处理方法。众所周知,一个JavaBean通常包含了大量的属性,而对于JavaBean的处理导致大量get/set方法的堆积,使用BeanUtils能简化我们的代码量,将我们的双手从get/set方法中解放出来,让我们节省时间去搬其它的砖头。

二、BeanUtils基本使用

      1,通过BeanUtils类可以直接get和set一个属性的值,方法如下(第一个参数是JavaBean对象,第二个参数是要操作的属性名):

getSimpleProperty(Object bean, String name)
Return the value of the specified simple property of the specified bean, converted to a String. setProperty(Object bean, String name, Object value)
Set the specified property value, performing type conversions as required to conform to the type of the destination property.

  2,一个更常用更重要的方法是:copyProperties()

copyProperties(Object dest, Object orig)
Copy property values from the origin bean to the destination bean for all cases where the property names are the same. copyProperty(Object bean, String name, Object value)
Copy the specified property value to the specified destination bean, performing any type conversion that is required.

需要注意的是这种copy都是浅拷贝,复制后的两个Bean的同一个属性可能拥有同一个对象的引用。所以在使用时要小心,特别是对于属性为自定义类的情况。

三、封装web请求参数

  定义一个静态方法copyParmas(),用于将request请求参数值赋给相应的bean的对应属性,若请求参数名与bean属性名不匹配,该值会被舍弃。

    public static Object copyParams(Class targetClass, HttpServletRequest request){
Object bean = null;
try {
bean = targetClass.newInstance();
Map params = request.getParameterMap();
Set set = params.entrySet();
for(Iterator iterator = set.iterator();iterator.hasNext();){
Entry entry = (Entry) iterator.next();
String name = (String) entry.getKey();
Object[] values = (Object[]) entry.getValue(); if(values!=null){
if(values.length == 1){
BeanUtils.copyProperty(bean, name, values[0]);
}else {
BeanUtils.copyProperty(bean, name, values);
}
}
}
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return bean;
}

四、copyProperties性能

  不幸的是Commons BeanUtil的copyProperties性能较差,而直接get/set虽然丑陋麻烦,但是性能很好。具体情况可参看文章:http://tntest.iteye.com/blog/399577 。

利用Common-BeanUtils封装请求参数的更多相关文章

  1. 十一、Struts2封装请求参数的方式

    十一.Struts2封装请求参数的方式 方式一.Action 本身作为model对象,通过成员setter封装(一个名字为params的拦截器干的) 注意:表单中的名称要和动作类中的名称一致(这是必须 ...

  2. struts2封装请求参数

    利用struts2框架进行将页面请求的参数封装有三种方法 第一种(不推荐) 就是动作类和bean中结合在一起,这样方法比较简单,但是很混乱. 代码: package com.example.actio ...

  3. asp.net中Request请求参数的自动封装

    这两天在测一个小Demo的时候发现一个很蛋疼的问题----请求参数的获取和封装,例: 方便测试用所以这里是一个很简单的表单. <!DOCTYPE html> <html xmlns= ...

  4. Spring MVC如何接收浏览器传递来的请求参数--request--形参--实体类封装

    阅读目录 1. 通过HttpServletRequest获得请求参数和数据 2. 处理方法形参名==请求参数名 3. 如果形参名跟请求参数名不一样怎么办呢?用@RequestParam注解 4. 用实 ...

  5. 0056 Spring MVC如何接收浏览器传递来的请求参数--request--形参--实体类封装

    浏览器总会向服务器传递一些参数,那么Spring MVC如何接收这些参数? 先写个简单的html,向服务器传递一些书籍信息,如下: <!DOCTYPE html> <html> ...

  6. springmvc 请求参数解析细节

    springmvc 的请求流程,相信大家已经很熟悉了,不熟悉的同学可以参考下资料! 有了整体流程的概念,是否对其中的实现细节就很清楚呢?我觉得不一定,比如:单是参数解析这块,就是个大学问呢? 首先,我 ...

  7. Struts2之处理请求参数

    时间:2017-1-11 11:05 --Struts2中获取请求参数(重点)1.Struts2是一个MVC框架,那么分别表示什么?    View:JSP    Model:Action    Co ...

  8. 【整理】01. localhost_access_log 记录post请求参数

    环境:apache-tomcat-7.0.57 利用Filter过去request请求参数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 ...

  9. SpringMVC 获取请求参数

    1.获取Request response对象 在SpringMVC的注解开发中,可以选择性的接收Request和Response对象来使用 2.获取request对象请求参数 a.通过request对 ...

随机推荐

  1. 数据结构之 线性表---单链表操作A (删除链表中的指定元素)

    数据结构上机测试2-1:单链表操作A Time Limit: 1000MS Memory limit: 4096K 题目描述 输入n个整数,先按照数据输入的顺序建立一个带头结点的单链表,再输入一个数据 ...

  2. HDU5950 Recursive sequence —— 矩阵快速幂

    题目链接:https://vjudge.net/problem/HDU-5950 Recursive sequence Time Limit: 2000/1000 MS (Java/Others)   ...

  3. 近期测试BUG总结

    前些日子上线了新版的app,在上线后发现了几个重大的bug,在此总结,在以后的测试工作中需要额外的关注. 需求流程bug 页面刷新bug 标签栏刷新bug 第一个bug出现的原因是产品需求与运营实际操 ...

  4. opengl in medical imaging

    医学可视化 http://schorsch.efi.fh-nuernberg.de/roettger/index.php/Lectures/MedicalVisualization http://ww ...

  5. java编程思想-基础

    interface: 方法默认为public:成员变量默认 static and final 对象数组的定义:理解? 多接口继承:可以多个接口,但只有一个具体类,具体类在前面 自:多接口继承时,来自不 ...

  6. QQ通信原理

    转载自http://blog.csdn.net/li_xiao_ming/article/details/8106857 下面有4个基本的问答: 问题一:为什么只要可以连上互联网的计算机都可以用QQ相 ...

  7. SQL.py

    import sqlite3,sys def convert(value): if value.startswith('~'): return value.strip('~') if not valu ...

  8. [APIO 2014] 序列分割

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=3675 [算法] 首先 , 我们发现将一段序列切成若干段所获得的收益与顺序无关 于是我 ...

  9. samba 安装运行

    samba 安装步骤 1.若之前有安装过相关软件包,先卸载之:sudo apt-get autoremove samba samba-commonsudo apt-get autoremove sys ...

  10. [转]Eclipse创建Maven项目

    构建Maven项目的完整过程--普通web项目(Eclipse) 进行以下步骤的前提是你已经安装好本地maven库和eclipse中的maven插件了(有的eclipse中已经集成了maven插件) ...