1、实例化当前javabean类
2、获取当前javabean类的所有成员变量.
3、设置set或者get方法
4、获取javabean成员变量类型
5、方法注入
public class HttpUtil {
public static Object getParameterValue(HttpServletRequest request,String ECID){
Object obj = null;
try{
obj = Class.forName(ECID).newInstance();
Class classType = obj.getClass();
// get all field
Field[] field = classType.getDeclaredFields();
for(int i=0;i<field.length;i++){
String params = field[i].toString();
Object value = (Object)request.getParameter(params);
String setMethod = "set" + params.substring(0,1).toUpperCase() + params.substring(1);
Method m = classType.getMethod(setMethod, new Class[]{field[i].getType()});
/* String getMethod = "get" + params.substring(0,1).toUpperCase() + params.substring(1);
Method get = classType.getMethod(getMethod, new Class[]{field[i].getType()});
inject get method
get.invoke(obj, new Object[]{value.toString()});*/
if(value != null && m != null){
if(field[i].getType().equals("class java.lang.Integer"))
m.invoke(obj, new Object[]{Integer.parseInt(value.toString())});
if(field[i].getType().equals("class java.lang.Double"))
m.invoke(obj, new Object[]{Double.valueOf(value.toString())});
if(field[i].getType().equals("class java.lang.Long"))
m.invoke(obj, new Object[]{Long.valueOf(value.toString())});
if(field[i].getType().equals("class java.lang.Float"))
m.invoke(obj, new Object[]{Float.valueOf(value.toString())});
else
m.invoke(obj, new Object[]{value.toString()});
}
}
}catch(Exception ex){
System.out.println("errMsg:" + ex.getMessage());
}
return obj;
}
}
2、获取当前javabean类的所有成员变量.
3、设置set或者get方法
4、获取javabean成员变量类型
5、方法注入
public class HttpUtil {
public static Object getParameterValue(HttpServletRequest request,String ECID){
Object obj = null;
try{
obj = Class.forName(ECID).newInstance();
Class classType = obj.getClass();
// get all field
Field[] field = classType.getDeclaredFields();
for(int i=0;i<field.length;i++){
String params = field[i].toString();
Object value = (Object)request.getParameter(params);
String setMethod = "set" + params.substring(0,1).toUpperCase() + params.substring(1);
Method m = classType.getMethod(setMethod, new Class[]{field[i].getType()});
/* String getMethod = "get" + params.substring(0,1).toUpperCase() + params.substring(1);
Method get = classType.getMethod(getMethod, new Class[]{field[i].getType()});
inject get method
get.invoke(obj, new Object[]{value.toString()});*/
if(value != null && m != null){
if(field[i].getType().equals("class java.lang.Integer"))
m.invoke(obj, new Object[]{Integer.parseInt(value.toString())});
if(field[i].getType().equals("class java.lang.Double"))
m.invoke(obj, new Object[]{Double.valueOf(value.toString())});
if(field[i].getType().equals("class java.lang.Long"))
m.invoke(obj, new Object[]{Long.valueOf(value.toString())});
if(field[i].getType().equals("class java.lang.Float"))
m.invoke(obj, new Object[]{Float.valueOf(value.toString())});
else
m.invoke(obj, new Object[]{value.toString()});
}
}
}catch(Exception ex){
System.out.println("errMsg:" + ex.getMessage());
}
return obj;
}
}