Request To JavaBean(请求对象转换为JavaBean对象)
2023-09-14 08:57:44 时间
背景:
经常要从request等对象取出值来赋入bean中,如果不用MVC框架的绑定功能的话,麻烦
一 参考资料
1 http://jc-dreaming.iteye.com/blog/563893
2 http://www.iteye.com/topic/76043
3 http://xubindehao.iteye.com/blog/754807
4 http://javafenger.iteye.com/blog/96829
5 http://www.kaila.com.cn/space.php?uid=7114&do=blog&id=458578
6 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());
相关文章
- AnonymousType匿名类型和对象之间的转换
- Js中把JSON字符串转换为JSON对象(eval()、new Function())
- 源码解读:KubeVela 是如何将 appfile 转换为 K8s 特定资源对象的
- python 时间字符串和时间戳之间的转换
- String() 函数把对象的值转换为字符串。
- golang基本数据类型和string的相互转换
- C# 将一个对象转换为指定类型
- Atiitt 图像处理的常见功能业务用途与类库与功能实现 目录 1. 常见业务场景2 1.1. 缩略图2 1.2. 判断图像大小分辨率要求 长度 宽度2 1.3. 图像格式 转换,,黑白图像
- Atitit 跨平台异常处理(2)--------异常转换 -----java c# js异常对象结构比较and转换
- atitit.XML类库选型及object 对象bean 跟json转换方案
- JavaScript:Date将时间戳转换为时间对象
- fastjson:Json和Java对象转换
- eclipse package,source folder,folder差别及相互转换
- Java中使用Jackson,对JSON和对象进行转换
- kettle的转换组件
- Asp.Net转换Html加号显示为空格的字符!(自已备用)
- 前端Json对象与Json字符串互转(4种转换方式)
- MOS电平转换,1.8V转3.3V,3.3V转1.8V
- 【第13天】给定一个十进制数字 B ,请你把它转换为R进制打印 | 进制转换