beanUtils webUtils UUID使用

来源:互联网 发布:淘宝网最热销的产品 编辑:程序博客网 时间:2024/04/30 11:35

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);
  }
  
 }
}