日期:2014-05-17 浏览次数:20770 次
package cn.customer.utils;
import java.sql.Date;
import java.util.Enumeration;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
public class WebUtils {
//把request中的数据封装到bean中
public static <T>T request2Bean(HttpServletRequest request,Class<T> clazz){
try {
T t = clazz.newInstance();
ConvertUtils.register(new DateLocaleConverter(), Date.class);
Enumeration e = request.getParameterNames();
while(e.hasMoreElements()){
String name = (String) e.nextElement();
String value = request.getParameter(name);
BeanUtils.setProperty(t,name,value);
}
return t;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static String makeId(){
return UUID.randomUUID().toString();
}
}