/**
* 因为目前没有数组暂不支持数组
* @param className
* @param context
* @return
*/
public static <T> T getObject(Class<T> className,IPresentationContext context){
return getObject(className,context,null,null);
}
public static <T> T getObject(Class<T> className,IPresentationContext context,String dateFormat){
return getObject(className,context,dateFormat,null);
}
public static <T> T getObject(Class<T> className,IPresentationContext context,Map<String,Integer> replace){
return getObject(className,context,null,replace);
}
/**
* 因为目前没有数组暂不支持数组
* @param className 对象的类型
* @param context 对request的封装
* @param dateFormat 日期格式
* @param replace 有时候我们会用<b>1</b>代表一些值比如通过<b>0</b>代表不通过
* @return
*/
public static <T> T getObject(Class<T> className,IPresentationContext context,String dateFormat,Map<String,Integer> replace){
T obj = null;
BeanInfo beanInfo = null;
try {
obj = className.newInstance();
beanInfo = Introspector.getBeanInfo(className);
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IntrospectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
PropertyDescriptor[] propertyDescriptorArr = beanInfo.getPropertyDescriptors();
for(PropertyDescriptor propertyDescriptor : propertyDescriptorArr){
String fieldName = propertyDescriptor.getName();
String fieldValue = context.getParameter(fieldName);
if(StringUtils.isNotEmpty(fieldValue)){
Class<?> fieldClass = propertyDescriptor.getPropertyType();
Method method = propertyDescriptor.getWriteMethod();
try {
if(replace.get(fieldValue) != null){
method.invoke(obj, replace.get(fieldValue));
}else if("java.lang.Integer".equals(fieldClass.getName())||"int".equals(fieldClass.getName())){
method.invoke(obj, Integer.parseInt(fieldValue));
}else if("java.util.Date".equals(fieldClass.getName())){
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
method.invoke(obj, sdf.parse(fieldValue));
}else if("java.lang.Float".equals(fieldClass.getName()) || "float".equals(fieldClass.getName())){
method.invoke(obj, Float.parseFloat(fieldValue));
}else{
method.invoke(obj, fieldValue);
}
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
if(e instanceof NumberFormatException){
}
}
}
}
return obj;
}
* 因为目前没有数组暂不支持数组
* @param className
* @param context
* @return
*/
public static <T> T getObject(Class<T> className,IPresentationContext context){
return getObject(className,context,null,null);
}
public static <T> T getObject(Class<T> className,IPresentationContext context,String dateFormat){
return getObject(className,context,dateFormat,null);
}
public static <T> T getObject(Class<T> className,IPresentationContext context,Map<String,Integer> replace){
return getObject(className,context,null,replace);
}
/**
* 因为目前没有数组暂不支持数组
* @param className 对象的类型
* @param context 对request的封装
* @param dateFormat 日期格式
* @param replace 有时候我们会用<b>1</b>代表一些值比如通过<b>0</b>代表不通过
* @return
*/
public static <T> T getObject(Class<T> className,IPresentationContext context,String dateFormat,Map<String,Integer> replace){
T obj = null;
BeanInfo beanInfo = null;
try {
obj = className.newInstance();
beanInfo = Introspector.getBeanInfo(className);
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IntrospectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
PropertyDescriptor[] propertyDescriptorArr = beanInfo.getPropertyDescriptors();
for(PropertyDescriptor propertyDescriptor : propertyDescriptorArr){
String fieldName = propertyDescriptor.getName();
String fieldValue = context.getParameter(fieldName);
if(StringUtils.isNotEmpty(fieldValue)){
Class<?> fieldClass = propertyDescriptor.getPropertyType();
Method method = propertyDescriptor.getWriteMethod();
try {
if(replace.get(fieldValue) != null){
method.invoke(obj, replace.get(fieldValue));
}else if("java.lang.Integer".equals(fieldClass.getName())||"int".equals(fieldClass.getName())){
method.invoke(obj, Integer.parseInt(fieldValue));
}else if("java.util.Date".equals(fieldClass.getName())){
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
method.invoke(obj, sdf.parse(fieldValue));
}else if("java.lang.Float".equals(fieldClass.getName()) || "float".equals(fieldClass.getName())){
method.invoke(obj, Float.parseFloat(fieldValue));
}else{
method.invoke(obj, fieldValue);
}
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
if(e instanceof NumberFormatException){
}
}
}
}
return obj;
}