request封裝到Bean對象
public static <T> T request2Bean(HttpServletRequest httpServletRequest, Class<T> aClass) {
try {
//對於日期而言,是須要日期轉換器的
ConvertUtils.register(new DateLocaleConverter(), Date.class);
//獲取Bean的對象
T bean = aClass.newInstance();
httpServletRequest.setCharacterEncoding("UTF-8");
//獲取表單中全部的名字
Enumeration enumeration = httpServletRequest.getParameterNames();
//遍歷表單提交過來的名字
while (enumeration.hasMoreElements()) {
//每一個名字
String name = (String) enumeration.nextElement();
//獲取獲得值
String value = httpServletRequest.getParameter(name);
//若是用戶提交的數據不爲空,那麼將數據封裝到Bean中
if (!value.equals("") && value != null) {
BeanUtils.setProperty(bean, name, value);
}
}
return bean;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("封裝數據到Bean中,失敗了!");
}
}
UUID工具方法
public static String makeId() {
return UUID.randomUUID().toString();
}