- 浏览: 145201 次
- 性别:
- 来自: 武汉
文章分类
最新评论
-
liaozhi_ITOYO:
...
ibatis 学习笔记(一) 批量处理 存储过程 -
boy_scarecrow:
不错,借鉴了!
ibatis 学习笔记(一) 批量处理 存储过程 -
qingyushan:
羡慕啊
假如我是JAVA开发人员 -
salever:
不错,貌似少一点超时设置
Java Tcp Socket聊天系统模型(主要为了学习线程) -
zhaotao_king:
不错!~
假如我是JAVA开发人员
import java.lang.reflect.Field; import java.math.BigDecimal; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import javax.servlet.http.HttpServletRequest; import org.apache.log4j.Logger; /** * <strong>封装数据类</strong> * <p> * 从request中获取值填充bean对象。 * 相应功能struts中已经实现,建议使用struts进行页面到Form的处理。 * </p> * */ public class BeanUtil { private static final Logger log = Logger.getLogger(BeanUtil.class); /** * 从request中获取值填充bean对象 * * @param objClass bean的类 * @param request 请求对象 * @return object对象 */ public static Object fillBean(Class objClass, HttpServletRequest request) { Object objInstance = null; try { objInstance = objClass.newInstance(); } catch (InstantiationException e1) { log.error(e1.getMessage(), e1); } catch (IllegalAccessException e1) { log.error(e1.getMessage(), e1); } Field field; String fieldName; String fieldValue = ""; int len; len = objClass.getDeclaredFields().length; for (int i = 0; i < len; i++) { field = objClass.getDeclaredFields()[i]; fieldName = field.getName(); try { fieldValue = request.getParameter(fieldName); } catch (Exception e1) { log.error(e1.getMessage(), e1); } if (fieldValue != null) { try { setFieldValue(field, objInstance, fieldValue); } catch (IllegalAccessException e) { log.error(e.getMessage(), e); } } } objClass = objClass.getSuperclass(); return objInstance; } /** * 将数据赋值给指定对象的相应属性 * * @param field 字段 * @param objInstance 指定对象 * @param value 数据 * @throws IllegalAccessException */ private static void setFieldValue(Field field, Object objInstance, String value) throws IllegalAccessException { String fieldType = field.getType().getName();// 取字段的数据类型 field.setAccessible(true); try { if (fieldType.equals("java.lang.String")) { field.set(objInstance, value); } else if (fieldType.equals("java.lang.Integer") || fieldType.equals("int")) { field.set(objInstance, Integer.valueOf(value)); } else if (fieldType.equals("java.lang.Long") || fieldType.equals("long")) { field.set(objInstance, Long.valueOf(value)); } else if (fieldType.equals("java.lang.Float") || fieldType.equals("float")) { field.set(objInstance, Float.valueOf(value)); } else if (fieldType.equals("java.lang.Double") || fieldType.equals("double")) { field.set(objInstance, Double.valueOf(value)); } else if (fieldType.equals("java.math.BigDecimal")) { field.set(objInstance, new BigDecimal(value)); } else if (fieldType.equals("java.util.Date")) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); formatter.setLenient(false); field.set(objInstance, formatter.parse(value)); } else if (fieldType.equals("java.sql.Date")) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); formatter.setLenient(false); Date date = formatter.parse(value); field.set(objInstance, new java.sql.Date(date.getTime())); } else if (fieldType.equals("java.lang.Boolean") || fieldType.equals("boolean")) { field.set(objInstance, Boolean.valueOf(value)); } else if (fieldType.equals("java.lang.Byte") || fieldType.equals("byte")) { field.set(objInstance, Byte.valueOf(value)); } else if (fieldType.equals("java.lang.Short") || fieldType.equals("short")) { field.set(objInstance, Short.valueOf(value)); } } catch (NumberFormatException ex) { // field.set(objInstance, null); 当使用简单数据类型会抛出异常 log.error(ex.getMessage(), ex); } catch (ParseException e) { log.error(e.getMessage(), e); field.set(objInstance, null); } catch (Exception e) { log.error(e.getMessage(), e); field.set(objInstance, null); } } }
import java.beans.PropertyDescriptor; import java.text.SimpleDateFormat; import java.util.Collection; import org.apache.commons.beanutils.PropertyUtils; import org.apache.log4j.Logger; /** * <strong>复制处理类</strong> */ public class CopyUtil { private static final Logger log = Logger.getLogger(CopyUtil.class.getName()); /** * 复制orig属性值赋值给dest * <p> * 满足如下条件时进行复制处理: * (1) dest与orig的属性名称相同; * (2) dest与orig的属性类型相同; * (3) dest的属性类型不为Class; * (4) orig属性类型不是Collection或者不是Collection的超类或接口; * </p> * * @param dest 目标对象 * @param orig 源对象 * @return 如果dest或者orig为null则返回null/如果发生异常返回null/否则返回复制填充后的dest */ public static Object copyProperties(Object dest, Object orig) { if (dest == null || orig == null) { return dest; } PropertyDescriptor[] destDesc = PropertyUtils.getPropertyDescriptors(dest); try { for (int i = 0; i < destDesc.length; i++) { Class destType = destDesc[i].getPropertyType(); Class origType = PropertyUtils.getPropertyType(orig, destDesc[i].getName()); if (destType != null && destType.equals(origType) && !destType.equals(Class.class)) { if (!Collection.class.isAssignableFrom(origType)) { try { Object value = PropertyUtils.getProperty(orig, destDesc[i].getName()); PropertyUtils.setProperty(dest, destDesc[i].getName(), value); } catch (Exception ex) { log.error("copyProperties(Object dest, Object orig)循环中异常。", ex); } } } } return dest; } catch (Exception ex) { log.error("copyProperties(Object dest, Object orig)异常。", ex); return null; } } /** * 复制orig属性值赋值给dest * <p> * 除了将String的值转换为java.util.Date的处理外,其他处理方式同copyProperties(Object dest, Object orig)。 * 最常用在将页面的查询条件(Form)转换为DTO进行数据库查询的处理。 * </p> * * @param dest 目标对象 * @param orig 源对象 * @return 如果dest或者orig为null则返回null/如果发生异常返回null/否则返回复制填充后的dest */ public static Object copyPropertiesSpecialOfStringToDate(Object dest, Object orig) { if (dest == null || orig == null) { return dest; } PropertyDescriptor[] destDesc = PropertyUtils.getPropertyDescriptors(dest); try { for (int i = 0; i < destDesc.length; i++) { Class destType = destDesc[i].getPropertyType(); Class origType = PropertyUtils.getPropertyType(orig, destDesc[i].getName()); if (destType != null && origType != null && !destType.equals(Class.class)) { if (origType.equals(destType)) { if (!Collection.class.isAssignableFrom(origType)) { try { Object valueOrig = PropertyUtils.getProperty(orig, destDesc[i].getName()); PropertyUtils.setProperty(dest, destDesc[i].getName(), valueOrig); } catch (Exception ex) { log.error("copyPropertiesSpecialOfStringToDate循环中异常-1。", ex); PropertyUtils.setProperty(dest, destDesc[i].getName(), null); } } } else if (destType.equals(java.util.Date.class) && origType.equals(String.class)) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); try { String valueOrig_string = (String) PropertyUtils.getProperty(orig, destDesc[i].getName()); if (valueOrig_string != null && valueOrig_string.trim().length() > 0) { PropertyUtils.setProperty(dest, destDesc[i].getName(), format.parse(valueOrig_string)); } else { PropertyUtils.setProperty(dest, destDesc[i].getName(), null); } } catch (Exception e) { log.error("copyPropertiesSpecialOfStringToDate循环中异常-2。", e); PropertyUtils.setProperty(dest, destDesc[i].getName(), null); } } } } return dest; } catch (Exception e) { log.error("copyPropertiesSpecialOfStringToDate异常。", e); return null; } } /** * 复制orig中指定的属性值赋值给dest * <p> * 除了指定属性名称数组外,其他处理方式同copyProperties(Object dest, Object orig)。 * </p> * * @param dest * @param orig * @param ignores * @return */ public static Object copyProperties(Object dest, Object orig, String[] ignores) { if (dest == null || orig == null) { return dest; } PropertyDescriptor[] destDesc = PropertyUtils.getPropertyDescriptors(dest); try { for (int i = 0; i < destDesc.length; i++) { if (contains(ignores, destDesc[i].getName())) { continue; } Class destType = destDesc[i].getPropertyType(); Class origType = PropertyUtils.getPropertyType(orig, destDesc[i].getName()); if (destType != null && destType.equals(origType) && !destType.equals(Class.class)) { if (!Collection.class.isAssignableFrom(origType)) { Object value = PropertyUtils.getProperty(orig, destDesc[i].getName()); PropertyUtils.setProperty(dest, destDesc[i].getName(), value); } } } return dest; } catch (Exception ex) { return null; } } /** * 属性名称数组中是否存在指定的属性 * * @param ignores 属性名称数组 * @param name 指定的属性名称 * @return 如果存在则返回true/否则返回false */ static boolean contains(String[] ignores, String name) { boolean ignored = false; for (int j = 0; ignores != null && j < ignores.length; j++) { if (ignores[j].equals(name)) { ignored = true; break; } } return ignored; } }
CopyUtil.copyProperties(userManagerDTO, initUserForm);
TaskDTO task = (TaskDTO) BeanUtil.fillBean(TaskDTO.class, request);
发表评论
-
Struts 注意问题
2010-01-18 17:40 8591-- struts form extend ... -
logic:present 和 logic:empty
2009-12-02 20:10 1862logic:empty和logic:notEmpty log ... -
BodyTagSupport,包括SKIP_PAGE, EVAL_PAGE等
2009-12-02 20:06 3116EVAL_BODY_INCLUDE:把Body读 ... -
Struts1中action线程安全问题
2009-11-18 14:52 3232struts 1.x的一个安全性问题是值得注意的。因为过去的 ... -
JSTL
2009-10-30 14:37 585<c:if test = "${fund0 ... -
html:error
2009-10-30 14:32 1126后台action error.add("erro ...
相关推荐
java中对象的作用,如何获取Request对象 java中对象的作用,如何获取Request对象
当页面编辑或运行提交时,出现“从客户端中检测到有潜在危险的request.form值”问题,该怎么办呢?如下图所示: 下面博主汇总出现这种错误的几种解决方法: 问题原因:由于在asp.net中,Request提交时出现有html...
struts2 获取request session对象
struts2中获得request对象的几种方法
js中使用 C标签 S标签 EL表达式 赋值
获取用户请求IP地址 根据IP地址获取mac地址
主要介绍了Spring实战之Bean的作用域request用法,结合实例形式分析了spring中Bean的request作用域相关使用技巧与操作注意事项,需要的朋友可以参考下
8、使用Request.QueryString()时,所获取的对象不存在,或在值为空时未赋初始值。 9、使用FindControl时,控件不存在却没有做预处理。 10、重复定义造成未将对象引用设置到对象的实例错误. 偶在ASP.Net中遇到的情况...
asp对象-Request
下面小编就为大家带来一篇使用JavaScript获取Request中参数的值方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
该代码是介绍JAVA获取当前系统的url,与大家分享,请大家下载!
熟练使用这些内置对象是开发JSP应用程序的基本要求,尤其是对于request、session和application对象更是必须要掌握的。 会使用对象所具有的方法,能熟练使用每一种对象的几种常用方法。希望对大家有用。。。
隐式对象 request 对象
通过访问请求的request获取网络IP,浏览器,操作系统等信息
在用java做基于Web的开发时,不可避免的需要和request&response这两个对象打交道,因此,对它们的学习和掌握就显得至关重要。
因为Request对象是Page对象的成员之一,所以在程序中不需要做任何的声明即可直接使用。 Server对象提供对服务器上的方法和属性进行的访问 .其类名称是HttpServerUtility. Application对象在实际网络开发中的用途...
IpUtil工具类--通过request获取IP,通过IP获取地址(需要联网,调用淘宝的IP库)。使用场景:在网站开发记录用户增删改查日志的时候,往往需要记录操作人的IP地址,来准确的生成操作日志记录等
新人备忘Request的好东西。了解方法才能更好的使用!
解决当FORM的ENCTYPE="multipart/form-data" 时request.getParameter()获取不到值的方法 ?