请问如何把一个web页面转换成POJO?有没有可以借助的工具呢~
------解决方案--------------------------------------------------------
可以自己写一个通用的toPojo方法,里面JavaBeanDateUtil类代码没有贴出,有兴趣可以联系我。
主要代码如下,可供参考,
public Object toPojo(HttpServletRequest request, Class pojoClass)
throws Exception {
Object pojo = pojoClass.newInstance();
PropertyDescriptor pds[] = this.getPropertyDescriptors(pojoClass);
for (int i = 0; i < pds.length; i++) {
String name = pds[i].getName();
if (!"class".equals(name)
&& this.isBasicType(pds[i].getPropertyType())) {
String value = request.getParameter(name);
if (value != null)
this.setProperty(pds[i], pojo, value);
}
}
return pojo;
}
//导出pojo的属性数组
protected PropertyDescriptor[] getPropertyDescriptors(Class beanClass) {
BeanInfo beanInfo = null;
try {
beanInfo = Introspector.getBeanInfo(beanClass);
} catch (IntrospectionException e) {
return new PropertyDescriptor[0];
}
PropertyDescriptor descriptors[] = (PropertyDescriptor[]) null;
descriptors = beanInfo.getPropertyDescriptors();
if (descriptors == null)
descriptors = new PropertyDescriptor[0];
return descriptors;
}
//判断pojo属性是否为基本类型
protected boolean isBasicType(Class cls) {
String className = cls.getName();
return "java.lang.String".equals(className)
|| "java.lang.Integer".equals(className)
|| "java.lang.Long".equals(className)
|| "java.lang.Short".equals(className)
|| "java.lang.Double".equals(className)
|| "java.lang.Float".equals(className)
|| "java.math.BigDecimal".equals(className)
|| "java.math.BigInteger".equals(className)
|| "int".equals(className) || "long".equals(className)
|| "short".equals(className) || "double".equals(className)
|| "float".equals(className)
|| "java.util.Date".equals(className)
|| "java.util.Calendar".equals(className)
|| "java.sql.Date".equals(className)
|| "java.sql.Time".equals(className)
|| "java.sql.Timestamp".equals(className)
|| "java.sql.Blob".equals(className)
|| "java.lang.Boolean".equals(className);
}
//写入pojo属性值
protected Object setProperty(PropertyDescriptor pd, Object bean,
String value) {
Method writeMethod = pd.getWriteMethod();
if (writeMethod == null && (pd instanceof IndexedPropertyDescriptor))
writeMethod = ((IndexedPropertyDescriptor) pd)
.getIndexedWriteMethod();
if (writeMethod != null)
try {
Object args[] = new Object[1];
args[0] = parseObject(value, pd.getPropertyType());
return writeMethod.invoke(bean, args);
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
//将传入的页面值转换为pojo中对应的类型
protected Object parseObject(String fromObj, Class toClass)
{
Object objReturn = null;
if(fromObj.equals(""))
return objReturn;
String toClassName = toClass.getName();
if(fromObj.getClass().getName().equals(toClassName))
objReturn = fromObj;
else
if("java.util.Date".equals(toClassName))
objReturn = JavaBeanDateUtil.toDate(fromObj);
else
if("java.util.Calendar".equals(toClassName))
objReturn = JavaBeanDateUtil.toCalendar(fromObj);
else
if("java.sql.Date".equals(toClassName))
objReturn = new Date(JavaBeanDateUtil.toDate(fromObj).getTime());
else
if("java.sql.Time".equals(toClassName))