package auto.system.module.core;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.servlet.http.HttpServletRequest;
//import org.apache.log4j.Logger;
//import auto.system.module.filter.LoginValidFilter;
public class DTOBuilder {
//private static Logger logger = Logger.getLogger(LoginValidFilter.class.getName());
/**
* 方法入口,得到Dto
*
* @param request
* @param dtoClass
* 传入的实体类
* @return
*/
public static Object getDTO(HttpServletRequest request, Class<?> dtoClass) {
Object dtoObj = null;
if ((dtoClass == null) || (request == null))
return dtoObj;
try {
// 实例化对象
dtoObj = dtoClass.newInstance();
setDTOValue(request, dtoObj);
} catch (Exception ex) {
ex.printStackTrace();
}
return dtoObj;
}
/**
* 方法入口,更新Dto
* @throws Exception
*/
public static void updateDTO(HttpServletRequest request, Object obj) throws Exception{
setDTOValue(request, obj);
}
/**
* 保存数据
*
* @param request
* @param dto
* @throws InvocationTargetException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws SecurityException
* @throws NoSuchMethodException
* @throws Exception
*/
private static void setDTOValue(HttpServletRequest request, Object dto) throws Exception{
if ((dto == null) || (request == null))
return;
// 得到类中所有的方法 基本上都是set和get方法
Method[] methods = dto.getClass().getMethods();
for (int i = 0; i < methods.length; i++) {
// 方法名
String methodName = methods[i].getName();
// 方法参数的类型
Class<?>[] type = methods[i].getParameterTypes();
// 当时set方法时,判断依据:setXxxx类型
if ((methodName.length() > 3) && (methodName.startsWith("set")) && (type.length == 1)) {
// 将set后面的大写字母转成小写并截取出来
String name = methodName.substring(3, 4).toLowerCase() + methodName.substring(4);
Object objValue = getBindValue(request, name, type[0]);
//Boolean flag = name.endsWith("_id");//设置_id及xxx_id的属性不在此外自动设置,在编写程序时,手动设置.
if (objValue != null) {
Object[] value = { objValue };
invokeMothod(dto, methodName, type, value);
}
}
}
}
/**
* 通过request得到相应的值
*
* @param request
* HttpServletRequest
* @param bindName
* 属性名
* @param bindType
* 属性的类型
* @return
*/
private static Object getBindValue(HttpServletRequest request, String bindName, Class<?> bindType) {
// 得到request中的值
String value = request.getParameter(bindName);
//logger.info(bindName+"--->>"+value);
if (value != null) {
value = value.trim().replace("\t", "").replace("\n", "");
}
return getBindValue(value, bindType);
}
/**
* 通过调用方法名(setXxxx)将值设置到属性中
*
* @param classObject
* 实体类对象
* @param strMethodName
* 方法名(一般都是setXxxx)
* @param argsType
* 属性类型数组
* @param args
* 属性值数组
* @return
* @throws NoSuchMethodException
* @throws SecurityException
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws InvocationTargetException
*/
private static Object invokeMothod(Object classObject, String strMethodName, Class<?>[] argsType, Object[] args)
throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException,
InvocationTargetException {
// 得到classObject这个类的方法
Method concatMethod = classObject.getClass().getMethod(strMethodName, argsType);
// 调用方法将classObject赋值到相应的属性
return concatMethod.invoke(classObject, args);
}
/**
* 根据bindType类型的不同转成相应的类型值
*
* @param value
* String类型的值,要根据bindType类型的不同转成相应的类型值
* @param bindType
* 属性的类型
* @return
*/
private static Object getBindValue(String value, Class<?> bindType) {
if(value == null)
return null;
String typeName = bindType.getName();
//System.out.println("---->"+typeName);
// 依次判断各种类型并转换相应的值
if (typeName.equals("java.lang.String"))
return value.trim();
if (typeName.equals("int"))
return new Integer(value);
if (typeName.equals("java.lang.Integer"))
return new Integer(value);
if (typeName.equals("boolean") || typeName.equals("java.lang.Boolean")){
return new Boolean(value);
}
return value;
}
/**
if ((value == null) || (value.trim().length() == 0))
return null;
String typeName = bindType.getName();
//依次判断各种类型并转换相应的值
if (typeName.equals("java.lang.String"))
return value;
if (typeName.equals("int"))
return new Integer(value);
if (typeName.equals("long"))
return new Long(value);
if (typeName.equals("boolean"))
return new Boolean(value);
if (typeName.equals("float"))
return new Float(value);
if (typeName.equals("double"))
return new Double(value);
if (typeName.equals("java.math.BigDecimal")) {
if ("NaN.00".equals(value))
return new BigDecimal("0");
return new BigDecimal(value.trim());
}
if (typeName.equals("java.util.Date"))
//参考DateUtil.parseDateDayFormat方法,value如果是时间类型,必须是yyyy-MM-dd格式才能被识别
//请参考我的另一篇博客http://blog.csdn.net/bq1073100909/article/details/49472615
return DateUtil.parseDateDayFormat(value);
if (typeName.equals("java.lang.Integer"))
return new Integer(value);
if (typeName.equals("java.lang.Long")) {
return new Long(value);
}
if (typeName.equals("java.lang.Boolean")) {
return new Boolean(value);
}
return value;
*/
public static void main(String[] args) {
}
}
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public ActionResult edit(HttpServletRequest request) throws Exception{
ActionResult result = new ActionResult();
String _id = res.getParameter(request, "_id", "");
_TUser obj = null;
try {
if("".equals(_id)){
//工号存在判断
obj = (_TUser) systemBaseDAO.findObjectByHql("from _TUser where _workId = '"+ res.getParameter(request, "_workId", "") +"'");
if(obj != null) {
result.setMsg("工号已存在!");
result.setSuccess(false);
}else {
//添加
obj = (_TUser) DTOBuilder.getDTO(request, _TUser.class);
obj.set_id(UUIDUtils.getUUID());
obj.set_is_hidden(false);
obj.set_type(true);
obj.set_inputdate(TimeUtils.getYearMonthDayHourMinuteStr());
obj.set_inputmen(SessionUtils.getUser(request).get_name());
obj.set_password(CommonFunction.fu_GenPasswordWith(res.getParameter(request, "_workId", ""), StringConfig.DEFAULTPWD));
systemBaseDAO.save(obj);
result.setSuccess(true);
}
}else{
//修改
obj = (_TUser) systemBaseDAO.findById(_TUser.class, _id);
DTOBuilder.updateDTO(request, obj);
obj.set_inputdate(TimeUtils.getYearMonthDayHourMinuteStr());
obj.set_inputmen(SessionUtils.getUser(request).get_name());
systemBaseDAO.attachDirty(obj, null);
result.setSuccess(true);
}
} catch (Exception e) {
result.setSuccess(false);
result.setMsg(e.getMessage());
}
return result;
}
《程序是怎样跑起来的《网络是怎样连接的》《计算机是怎样跑起来的的》
《计算机网络:自顶向下》《图解http》《大话数据结构》《阿哈算法》《算法第四版》《编程珠玑》《Java核心技术卷1》《编程思想》、《深入理解Java虚拟机》、《effective Java》、《Java并发编程的艺术》《mysql必知必会》、《MySQL技术内幕:InnoDB存储引擎》