/**
* 实体类映射
* lv
* 将页面的按照类的域名传递的值进行保存,暂时只支持String字段,
* 页面name必须按照 perfix.字段名
*/
public <T extends BaseModel<T>> T getNewModel(Class<? extends T> modelClass, String perfix,HttpServletRequest request) {
T t = null;
try {
t = modelClass.newInstance();
} catch (Exception e) {
return null;
}
Class c = t.getClass();
Field field[] = c.getDeclaredFields();
HttpServletRequest request = getRequest();
Method method = null;
for(Field f : field) {
//页面的属性值
String pageValue = request.getParameter(perfix+"."+f.getName());
if(pageValue == null) {
continue;
}
try {
String columnType = f.getGenericType().toString();
if("class java.lang.String".equals(columnType)) {
method = c.getMethod("set"+f.getName().substring(0, 1).toUpperCase()+f.getName().substring(1), String.class);
method.invoke(t, pageValue);
}else if("class java.lang.Integer".equals(columnType)) {
method = c.getMethod("set"+f.getName().substring(0, 1).toUpperCase()+f.getName().substring(1), Integer.class);
method.invoke(t, pageValue);
}else if("class java.lang.Boolean".equals(columnType)) {
method = c.getMethod("set"+f.getName().substring(0, 1).toUpperCase()+f.getName().substring(1), Boolean.class);
method.invoke(t, pageValue);
}
} catch (Exception e) {
continue;
}
}
return t;
}