标签:use null ram 支持 public class font AC string
Map<String,String[]> properties=request.getParameterMap();
User user=new User();
//自己指定一个类型转换器(将String转成Date)
ConvertUtils.register(new Converter(){
@Override
public Object convert(Class clazz,Object value){
//将string转成date
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
Date parse=null;
try {
parse=format.parse(value.toString());
}catch (ParseException e){
e.printStackTrace();
}
return parse;
}
},Date.class);
//映射封装
BeanUtils.populate(user,properties);
上面三行代码是用来将一些 key-value 的值(例如 hashmap)映射到 bean 中的属性。映射的过程就是将页面中的内容先用request获得,
然后再将之转换为Map(这里用request.getParameterMap())最后使用BeanUtils.populate(info,map)方法将页面各个属性映射到bean中。
之后我们就可以这样使用bean.getXxxx()来取值了。但是BeanUtils的populate、copyProperties方法或者getProperty,setProperty方法
其实都会调用convert进行转换但Converter只支持一些基本的类型,甚至连java.util.Date类型也不支持。这个时候就需要给类型注册转换器。对于Date类型,我在此实现了一个Converter。
而SimpleDateFormat是对时间日期处理的格式化工具类
标签:use null ram 支持 public class font AC string
原文地址:https://www.cnblogs.com/l-s-l/p/8810281.html