背景: 
经常要从request等对象取出值来赋入bean中,如果不用MVC框架的绑定功能的话,麻烦  
一 参考资料 
http://jc-dreaming.iteye.com/blog/563893 
http://www.iteye.com/topic/76043 
http://xubindehao.iteye.com/blog/754807 
http://javafenger.iteye.com/blog/96829 
http://www.kaila.com.cn/space.php?uid=7114&do=blog&id=458578 
http://blog.csdn.net/zsx923/archive/2010/01/19/5210325.aspx 
二 代码示例

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.beanutils.BeanUtils; public class RequestToBean { /**
* ConvertUtilsBean convertUtils = new ConvertUtilsBean();
* DateConverter dateConverter = new DateConverter();
* convertUtils.register(dateConverter,Date.class);
* */ /**
* @param <T>
* @param newSource 现将要设置新值的对象
* @param source 源数据对象
*/
public static <T> void beanConvert(T newSource,T source){
try {
BeanUtils.copyProperties(newSource,source);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
} /**
* @param request 请求对象
* @param obj 要设置Bean的类型,传入试为: Bean.class
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T getBeanToRequest(HttpServletRequest request,Class<T> clazz){ //获取页面所有的请求参数名称
Enumeration<String> enume = request.getParameterNames();
T beanObj = getObjectByClass(clazz);
try{
while(enume.hasMoreElements()){
//参数名称
String propertyName = enume.nextElement();
//判断是否存在此属性
if(isCheckBeanExitsPropertyName(clazz,propertyName)){
//获取请求值
Object propertyValue = request.getParameter(propertyName);
setProperties(beanObj,propertyName,propertyValue);
} }
}catch(Exception e){} return beanObj;
} private static <T> T getObjectByClass(Class<T> clazz){
T t = null;
try {
t = clazz.newInstance();
} catch (InstantiationException e1) {
e1.printStackTrace();
} catch (IllegalAccessException e1) {
e1.printStackTrace();
}
return t;
} /**
* @param clazz Class对象
* @param propertyName 属性名称
* @return true || false 检查对象中是否存在该属性名称
*/
private static boolean isCheckBeanExitsPropertyName(Class<?> clazz,String propertyName){
boolean retValue = false;
try {
Field field = clazz.getDeclaredField(propertyName);
if(null != field){
retValue = true;
}
} catch (NoSuchFieldException e) {
Out.println("类: " + clazz.getSimpleName()+",不存在属性名: "+propertyName+" ,详细错误信息: "+e.getMessage());
}
return retValue; } /**
* 设置字段值
* @param obj 实例对象
* @param propertyName 属性名
* @param value 新的字段值
* @return
*/
public static void setProperties(Object object, String propertyName,Object value) throws IntrospectionException,
IllegalAccessException, InvocationTargetException {
PropertyDescriptor pd = new PropertyDescriptor(propertyName,object.getClass());
Method methodSet = pd.getWriteMethod();
methodSet.invoke(object,value);
}
}

  三 使用common beanutils组件 
代码: 
User user = new User(); 
BeanUtils.populate(user,request.getParameterMap());

最新文章

  1. EC笔记:第4部分:20、传递引用代替传值
  2. Mac安装Bower
  3. 安装mysql因为/tmp权限不足而导致ERROR 1045 (28000): Access denied for user root@localhost (using password: NO)的解决方案
  4. MongoDB3.0新版本几点介绍
  5. WordPress安装使用问题记录
  6. ios中webservice报文的拼接
  7. c#多层嵌套Json
  8. [Raobin] Ext.net在页面中以窗体的形式打开另外的页面
  9. asp.net(C#)禁止缓存文件
  10. CentOS的改变系统启动级别
  11. 房上的猫:java中的包
  12. datatables行编辑中,某个字段用户显示和用于行编辑名称不同时的处理。
  13. 元素 &quot;context:component-scan&quot; 的前缀 &quot;context&quot; 未绑定。
  14. 编写SHELL脚本--编写简单脚本
  15. GO入门——5. 函数
  16. python面试一:python2与python3的区别一
  17. e804. 显示JProgressBar的完成百分率
  18. 自己写操作系统---bootsector篇
  19. Node.js 介绍及安装
  20. 【原创】python web应用性能测试出现的问题以及使用的工具

热门文章

  1. HDU1003 dp 动态规划解析
  2. 程序在nor flash中真的可以运行吗?
  3. C++ 11 笔记 (二) : for循环
  4. 配置Apache服务器 数据库mySQL
  5. 为你的TabBar添加Badge-感谢分享
  6. 21个很棒的jQuery分页插件下载
  7. windows下DOS命令中查看被占用端口的进程
  8. UESTC 1425 Another LCIS
  9. find big file
  10. Apache mod_fcgid fcgid_header_bucket_read函数缓冲区溢出漏洞