本文整理汇总了Java中org.apache.commons.beanutils.ConvertUtils类的典型用法代码示例。如果您正苦于以下问题:Java ConvertUtils类的具体用法?Java ConvertUtils怎么用?Java ConvertUtils使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ConvertUtils类属于org.apache.commons.beanutils包,在下文中一共展示了ConvertUtils类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: mapToBean
import org.apache.commons.beanutils.ConvertUtils; //导入依赖的package包/类
/**
* map to bean
* 转换过程中,由于属性的类型不同,需要分别转换。
* java 反射机制,转换过程中属性的类型默认都是 String 类型,否则会抛出异常,而 BeanUtils 项目,做了大量转换工作,比 java 反射机制好用
* BeanUtils 的 populate 方法,对 Date 属性转换,支持不好,需要自己编写转换器
*
* @param map 待转换的 map
* @param bean 满足 bean 格式,且需要有无参的构造方法; bean 属性的名字应该和 map 的 key 相同
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
private static void mapToBean(Map<String, Object> map, Object bean) throws IllegalAccessException, InvocationTargetException {
//注册几个转换器
ConvertUtils.register(new SqlDateConverter(null), java.sql.Date.class);
ConvertUtils.register(new SqlTimestampConverter(null), java.sql.Timestamp.class);
//注册一个类型转换器 解决 common-beanutils 为 Date 类型赋值问题
ConvertUtils.register(new Converter() {
// @Override
public Object convert(Class type, Object value) { // type : 目前所遇到的数据类型。 value :目前参数的值。
// System.out.println(String.format("value = %s", value));
if (value == null || value.equals("") || value.equals("null"))
return null;
Date date = null;
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
date = dateFormat.parse((String) value);
} catch (Exception e) {
e.printStackTrace();
}
return date;
}
}, Date.class);
BeanUtils.populate(bean, map);
}
示例2: check
import org.apache.commons.beanutils.ConvertUtils; //导入依赖的package包/类
public static void check()
{
if (classLoaders.putIfAbsent(Thread.currentThread().getContextClassLoader(), Boolean.TRUE) == null) {
loadDefaultConverters();
for (Map.Entry<Class<?>, Class<? extends StringCodec<?>>> entry : codecs.entrySet()) {
try {
final StringCodec<?> codecInstance = entry.getValue().newInstance();
ConvertUtils.register(new Converter()
{
@Override
public Object convert(Class type, Object value)
{
return value == null ? null : codecInstance.fromString(value.toString());
}
}, entry.getKey());
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
}
}
示例3: convertToString
import org.apache.commons.beanutils.ConvertUtils; //导入依赖的package包/类
public String[] convertToString(Object[] objects) {
String[] strings = new String[objects.length];
int i = 0;
for (Object object : objects) {
if (object != null) {
Converter converter = ConvertUtils.lookup(object.getClass());
if (converter != null) {
strings[i++] = converter.convert(object.getClass(), object).toString();
} else {
strings[i++] = object.toString();
}
} else {
strings[i++] = "";
}
}
return strings;
}
示例4: set
import org.apache.commons.beanutils.ConvertUtils; //导入依赖的package包/类
/**
* Set the value of a simple property with the specified name.
*
* @param name Name of the property whose value is to be set
* @param value Value to which this property is to be set
*/
public void set(String name, Object value) {
// Set the page number (for validator)
if ("page".equals(name)) {
if (value == null) {
page = 0;
} else if (value instanceof Integer) {
page = ((Integer)value).intValue();
} else {
try {
page = ((Integer)ConvertUtils.convert(value.toString(), Integer.class)).intValue();
}
catch (Exception ignore) {
page = 0;
}
}
}
dynaBean.set(name, value);
}
示例5: convertToObject
import org.apache.commons.beanutils.ConvertUtils; //导入依赖的package包/类
/**
* 基于Apache BeanUtils转换字符串到相应类型.
*
* @param value 待转换的字符串.
* @param toType 转换目标类型.
*/
public static Object convertToObject(String value, Class<?> toType) {
Object cvt_value=null;
try {
cvt_value=ConvertUtils.convert(value, toType);
// if(toType==Date.class){
// System.out.println("0----0");
// SimpleDateFormat dateFormat=null;
// try{
// dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// cvt_value=dateFormat.parse(value);
// }catch(Exception e){
// dateFormat=new SimpleDateFormat("yyyy-MM-dd");
// cvt_value=dateFormat.parse(value);
// }
// }
} catch (Exception e) {
throw ReflectionUtils.convertReflectionExceptionToUnchecked(e);
}
return cvt_value;
}
示例6: entity2Dto
import org.apache.commons.beanutils.ConvertUtils; //导入依赖的package包/类
private static void entity2Dto() {
EUser u = new EUser();
u.setId(1l);
u.setUsername("yoking");
u.setCreationDate(new Date(System.currentTimeMillis()));
UserDTO user = new UserDTO();
ConvertUtils.register(new DateStringConverter(), String.class);
try {
BeanUtils.copyProperties(user, u);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
System.out.println(user);
}
示例7: dto2Entity
import org.apache.commons.beanutils.ConvertUtils; //导入依赖的package包/类
private static void dto2Entity() {
UserDTO user = new UserDTO();
user.setId(1l);
user.setUsername("joking");
user.setCreationDate("2016-04-20");
EUser u = new EUser();
ConvertUtils.register(new DateStringConverter(), Date.class);
try {
BeanUtils.copyProperties(u, user);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
System.out.println(u);
}
示例8: toValue
import org.apache.commons.beanutils.ConvertUtils; //导入依赖的package包/类
private static Object toValue(String initial, Class clazz) {
Object value;
try {
if (initial != null) {
value = ConvertUtils.convert(initial, clazz);
} else {
if (clazz.isArray()) {
value = Array.newInstance(clazz.getComponentType(), 0);
} else {
value = clazz.newInstance();
}
}
} catch (Throwable t) {
log.warn("No es pot convertir '" + initial + "' a " + clazz.getName());
value = null;
}
return (value);
}
示例9: getList
import org.apache.commons.beanutils.ConvertUtils; //导入依赖的package包/类
@Override
public <T> List<T> getList(Object aKey, Class<T> aElementType) {
List list = get(aKey, List.class);
if(list == null) {
return null;
}
List<T> typedList = new ArrayList<>();
for(Object item : list) {
if(aElementType.equals(Accessor.class) || aElementType.equals(MapObject.class)) {
typedList.add((T)new MapObject((Map<String, Object>) item));
}
else {
typedList.add((T)ConvertUtils.convert(item,aElementType));
}
}
return Collections.unmodifiableList(typedList);
}
示例10: getVariableMap
import org.apache.commons.beanutils.ConvertUtils; //导入依赖的package包/类
@JsonIgnore
public Map<String, Object> getVariableMap() {
ConvertUtils.register(new DateConverter(), java.util.Date.class);
if (StringUtils.isBlank(keys)) {
return map;
}
String[] arrayKey = keys.split(",");
String[] arrayValue = values.split(",");
String[] arrayType = types.split(",");
for (int i = 0; i < arrayKey.length; i++) {
String key = arrayKey[i];
String value = arrayValue[i];
String type = arrayType[i];
Class<?> targetType = Enum.valueOf(PropertyType.class, type).getValue();
Object objectValue = ConvertUtils.convert(value, targetType);
map.put(key, objectValue);
}
return map;
}
示例11: set
import org.apache.commons.beanutils.ConvertUtils; //导入依赖的package包/类
/**
* Set the value of a simple property with the specified name.
*
* @param name Name of the property whose value is to be set
* @param value Value to which this property is to be set
*/
public void set(String name, Object value) {
// Set the page number (for validator)
if ("page".equals(name)) {
if (value == null) {
page = 0;
} else if (value instanceof Integer) {
page = ((Integer) value).intValue();
} else {
try {
page =
((Integer) ConvertUtils.convert(value.toString(),
Integer.class)).intValue();
} catch (Exception ignore) {
page = 0;
}
}
}
dynaBean.set(name, value);
}
示例12: fastPopulate
import org.apache.commons.beanutils.ConvertUtils; //导入依赖的package包/类
/**
* 将Map的值装入指定的对象中
*
* @param obj 对象
* @param properties map值
* @throws java.lang.reflect.InvocationTargetException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
public static void fastPopulate(Object obj, Map<String, Object> properties) {
Class<?> classz = obj.getClass();
Map<String, PropertiesEntry> map = getFastBeanCache().getPropertiesEntryMap(classz);
if (map.size() > 0) {
try {
for (Map.Entry<String, Object> entry : properties.entrySet()) {
PropertiesEntry pe = map.get(entry.getKey());
if (pe != null) {
if (pe.getSet() != null) {
if (entry.getValue() != null) {
Object v = ConvertUtils.convert(entry.getValue(), pe.getType());
pe.getSet().invoke(obj, v);
} else {
entry.setValue(null);
}
}
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
示例13: test4
import org.apache.commons.beanutils.ConvertUtils; //导入依赖的package包/类
@Test
public void test4() throws Exception{
String name="aaa";
String password="123";
String age="24";
String birthday="1980-09-09";
//ʹ��apache���Ѿ�ʵ��Convert�ӿڵ�ת������DateLocaleConverterʵ�ֽ�ǰ�˷��͵��������͵��ַ�������ת��Ϊbean�������Ե�������������
ConvertUtils.register(new DateLocaleConverter(), Date.class);
Person bean=new Person();
BeanUtils.setProperty(bean, "name", name);//��ʾ�����ĸ�bean���ĸ����ԣ���Ϊ������Ը�ֵΪvalue
BeanUtils.setProperty(bean, "password", password);
BeanUtils.setProperty(bean, "age", age);//ֻ֧���ַ�����8�ֻ������������Զ�ת��
BeanUtils.setProperty(bean, "birthday", birthday);
System.out.println(bean.getName());//aaa
System.out.println(bean.getPassword());//123
System.out.println(bean.getAge());//24�����Կ���ǰ̨���ݹ������ݶ������ַ������͵ģ�����bean����age������int�ͣ�Ҳ����beanUtils���֧�ֽ�ǰ̨���ַ��������Զ�ת��Ϊ���ֻ����������ݣ����Ǹ������ͣ��Ͳ������ˣ���Ҫ����Ϊ��������ע��һ��ת��������beanUtils�����ת����ȥ��ǰ̨string����ת��Ϊbean����ĸ����������ԣ�����������
System.out.println(bean.getBirthday().toLocaleString());//1980-9-9 0:00:00
}
示例14: test5
import org.apache.commons.beanutils.ConvertUtils; //导入依赖的package包/类
@Test
public void test5() throws Exception{
Map map=new HashMap();
map.put("name", "aaa");
map.put("password", "123");
map.put("age", "23");
map.put("birthday", "1980-09-09");
//ʹ��apache���Ѿ�ʵ��Convert�ӿڵ�ת������DateLocaleConverterʵ�ֽ�ǰ�˷��͵��������͵��ַ�������ת��Ϊbean�������Ե�������������
ConvertUtils.register(new DateLocaleConverter(), Date.class);
Person bean =new Person();
BeanUtils.populate(bean, map);//ʹ��map���ϵ�key-valueֵ���bean��������ԣ��ڲ��ǽ�map�ؼ���keyΪname������valueֵ��䵽bean�����Ӧ��name����ֵ�ϣ���map�Ĺؼ������Ʊ�����bean���Ե�����������һ��
System.out.println(bean.getName());//aaa
System.out.println(bean.getPassword());//123
System.out.println(bean.getAge());//24�����Կ���ǰ̨���ݹ������ݶ������ַ������͵ģ�����bean����age������int�ͣ�Ҳ����beanUtils���֧�ֽ�ǰ̨���ַ��������Զ�ת��Ϊ���ֻ����������ݣ����Ǹ������ͣ��Ͳ������ˣ���Ҫ����Ϊ��������ע��һ��ת��������beanUtils�����ת����ȥ��ǰ̨string����ת��Ϊbean����ĸ����������ԣ�����������
System.out.println(bean.getBirthday().toLocaleString());//1980-9-9 0:00:00
}
示例15: Map2Bean
import org.apache.commons.beanutils.ConvertUtils; //导入依赖的package包/类
public static Object Map2Bean(Class type, Map map) throws Exception {
ConvertUtils.register(new DateConvert(), Date.class);
BeanInfo beanInfo = Introspector.getBeanInfo(type);
Object obj = type.newInstance();
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor descriptor : propertyDescriptors) {
String propertyName = descriptor.getName();
if (!(map.containsKey(propertyName.toUpperCase())))
continue;
try {
Object value = map.get(propertyName.toUpperCase());
BeanUtils.setProperty(obj, propertyName, value);
} catch (Exception e) {
e.printStackTrace();
}
}
return obj;
}