public class WebUtils {
private WebUtils(){}
// 将请求参数封装到bean
public static <T> T request2Bean(HttpServletRequest request, Class<T> clazz) {
try {
//创建请求类的对象
T bean = clazz.newInstance();
//添加id
if(request.getParameter("id") == null){
String id = UUID.randomUUID().toString();
BeanUtils.setProperty(bean,"id",id);
}
//注册转换器
ConvertUtils.register(new DateLocaleConverter(), Date.class);
// 不知道是什么样的表单 什么样的bean 不知道有哪些字段
Enumeration e = request.getParameterNames();
while (e.hasMoreElements()) {
String name = (String) e.nextElement();
String value = request.getParameter(name);
BeanUtils.setProperty(bean, name, value);
}
return bean;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}