本文整理汇总了Java中org.apache.commons.beanutils.BeanUtils类的典型用法代码示例。如果您正苦于以下问题:Java BeanUtils类的具体用法?Java BeanUtils怎么用?Java BeanUtils使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
BeanUtils类属于org.apache.commons.beanutils包,在下文中一共展示了BeanUtils类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: mapToBean
import org.apache.commons.beanutils.BeanUtils; //导入依赖的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: doGet
import org.apache.commons.beanutils.BeanUtils; //导入依赖的package包/类
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{
User user = new User();
HashMap map = new HashMap();
Enumeration names = request.getParameterNames();
while (names.hasMoreElements()) {
String name = (String) names.nextElement();
map.put(name, request.getParameterValues(name));
}
try{
BeanUtils.populate(user, map); //BAD
BeanUtilsBean beanUtl = BeanUtilsBean.getInstance();
beanUtl.populate(user, map); //BAD
}catch(Exception e){
e.printStackTrace();
}
}
示例3: loadUserByUsername
import org.apache.commons.beanutils.BeanUtils; //导入依赖的package包/类
@Override
public UserDetails loadUserByUsername(String name) throws UsernameNotFoundException {
User user = this.userService.findByName(name);
TzUserDetails userDetails = new TzUserDetails();
if(user == null){
throw new UsernameNotFoundException("用户不存在");
}
try {
BeanUtils.copyProperties(userDetails,user);
} catch (Exception e) {
e.printStackTrace();
}
logger.info("---->身份验证:"+userDetails.getUsername()+":"+userDetails.getPassword());
return userDetails;
}
示例4: getConditions
import org.apache.commons.beanutils.BeanUtils; //导入依赖的package包/类
protected Map getConditions(Object param) throws IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
// 静态条件部分
Map props = BeanUtils.describe(param);
// new 动态条件部分 add by hekun
if (param instanceof DBQueryParam) {
DBQueryParam listVO = (DBQueryParam) param;
Map queryConditions = listVO.getQueryConditions();
if (queryConditions != null && queryConditions.size() > 0) {
// 将静态条件加入动态条件中,重复的动态条件及其值将被覆盖。
for (Iterator keys = props.keySet().iterator(); keys.hasNext(); ) {
String key = (String) keys.next();
Object value = props.get(key);
if (key.startsWith("_") && value != null)
queryConditions.put(key, value);
}
props = queryConditions;
}
}
return props;
}
示例5: transformMapListToBeanList
import org.apache.commons.beanutils.BeanUtils; //导入依赖的package包/类
/**
* 将所有记录包装成T类对象,并将所有T类对象放入一个数组中并返回,传入的T类的定义必须符合JavaBean规范,
* 因为本函数是调用T类对象的setter器来给对象赋值的
*
* @param clazz T类的类对象
* @param listProperties 所有记录
* @return 返回所有被实例化的对象的数组,若没有对象,返回一个空数组
* @throws InstantiationException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
private static <T> List<T> transformMapListToBeanList(Class<T> clazz, List<Map<String, Object>> listProperties)
throws InstantiationException, IllegalAccessException, InvocationTargetException {
// 存放被实例化的T类对象
T bean = null;
// 存放所有被实例化的对象
List<T> list = new ArrayList<T>();
// 将实例化的对象放入List数组中
if (listProperties.size() != 0) {
Iterator<Map<String, Object>> iter = listProperties.iterator();
while (iter.hasNext()) {
// 实例化T类对象
bean = clazz.newInstance();
// 遍历每条记录的字段,并给T类实例化对象属性赋值
for (Map.Entry<String, Object> entry : iter.next().entrySet()) {
BeanUtils.setProperty(bean, entry.getKey(), entry.getValue());
}
// 将赋过值的T类对象放入数组中
list.add(bean);
}
}
return list;
}
示例6: getProtectedOxTrustApplicationConfiguration
import org.apache.commons.beanutils.BeanUtils; //导入依赖的package包/类
private String getProtectedOxTrustApplicationConfiguration(ApplicationConfiguration oxTrustApplicationConfiguration) {
try {
ApplicationConfiguration resultOxTrustApplicationConfiguration = (ApplicationConfiguration) BeanUtils.cloneBean(oxTrustApplicationConfiguration);
resultOxTrustApplicationConfiguration.setSvnConfigurationStorePassword(HIDDEN_PASSWORD_TEXT);
resultOxTrustApplicationConfiguration.setKeystorePassword(HIDDEN_PASSWORD_TEXT);
resultOxTrustApplicationConfiguration.setIdpSecurityKeyPassword(HIDDEN_PASSWORD_TEXT);
resultOxTrustApplicationConfiguration.setIdpBindPassword(HIDDEN_PASSWORD_TEXT);
resultOxTrustApplicationConfiguration.setMysqlPassword(HIDDEN_PASSWORD_TEXT);
resultOxTrustApplicationConfiguration.setCaCertsPassphrase(HIDDEN_PASSWORD_TEXT);
resultOxTrustApplicationConfiguration.setOxAuthClientPassword(HIDDEN_PASSWORD_TEXT);
return jsonService.objectToJson(resultOxTrustApplicationConfiguration);
} catch (Exception ex) {
log.error("Failed to prepare JSON from ApplicationConfiguration: '{0}'", ex, oxTrustApplicationConfiguration);
}
return null;
}
示例7: convert
import org.apache.commons.beanutils.BeanUtils; //导入依赖的package包/类
public static List<ShopProductDto> convert(List<ShopProduct> products,ObjectMapper objectMapper) throws InvocationTargetException, IllegalAccessException {
if (products == null) {
return null;
}
List<ShopProductDto> productDtos = new ArrayList();
for (int i = 0; i < products.size(); i++) {
ShopProductDto tmp = new ShopProductDto();
BeanUtils.copyProperties(tmp, products.get(i));
try {
/*处理图片信息转换*/
tmp.setImgs(tmp.parseImageList(objectMapper)) ;
/*处理基础信息转换*/
tmp.parseBaseProperty(objectMapper);
/*其他转换操作*/
} catch (IOException e) {
e.printStackTrace();
}
productDtos.add(tmp);
}
return productDtos;
}
示例8: createProxyEntity
import org.apache.commons.beanutils.BeanUtils; //导入依赖的package包/类
@SuppressWarnings("unchecked")
public <T extends IEntity> T createProxyEntity(T entity) throws Exception {
EntityProxy entityProxy = createProxy(entity);
if (entityProxy != null) {
EntityProxyWrapper entityProxyWrapper = new EntityProxyWrapper(entityProxy);
AbstractEntity proxyEntity = createProxyEntity(entityProxy);
if(proxyEntity!=null) {
// 注入对象 数值
BeanUtils.copyProperties(proxyEntity, entity);
entityProxy.setCollectFlag(true);
if(entityProxyWrapper != null) {
proxyEntity.setEntityProxyWrapper(entityProxyWrapper);
return (T) proxyEntity;
}
}
}
return null;
}
示例9: setProperty
import org.apache.commons.beanutils.BeanUtils; //导入依赖的package包/类
/**
*
* @param retval
* @param key
* @param value
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
private void setProperty(T retval, String key, Object value) throws IllegalAccessException, InvocationTargetException {
String property = key2property(key);
for (final Field f : this.type.getDeclaredFields()) {
final SerializedName annotation = f.getAnnotation(SerializedName.class);
if (annotation != null && property.equalsIgnoreCase(annotation.value())) {
property = f.getName();
break;
}
}
if (propertyExists(retval, property)) {
BeanUtils.setProperty(retval, property, value);
} else {
LOG.warn(retval.getClass() + " does not support public setter for existing property [" + property + "]");
}
}
示例10: checkProperties
import org.apache.commons.beanutils.BeanUtils; //导入依赖的package包/类
/**
* Checks if the Property Values of the item are valid for the Request.
*
* @param item
* @throws AirtableException
* @throws IllegalAccessException
* @throws InvocationTargetException
* @throws NoSuchMethodException
*/
private void checkProperties(T item) throws AirtableException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
if (propertyExists(item, FIELD_ID) || propertyExists(item, FIELD_CREATED_TIME)) {
Field[] attributes = item.getClass().getDeclaredFields();
for (Field attribute : attributes) {
String attrName = attribute.getName();
if (FIELD_ID.equals(attrName) || FIELD_CREATED_TIME.equals(attrName)) {
if (BeanUtils.getProperty(item, attribute.getName()) != null) {
throw new AirtableException("Property " + attrName + " should be null!");
}
} else if ("photos".equals(attrName)) {
List<Attachment> obj = (List<Attachment>) BeanUtilsBean.getInstance().getPropertyUtils().getProperty(item, "photos");
checkPropertiesOfAttachement(obj);
}
}
}
}
示例11: checkPropertiesOfAttachement
import org.apache.commons.beanutils.BeanUtils; //导入依赖的package包/类
/**
* Check properties of Attachement objects.
*
* @param attachements
* @throws AirtableException
* @throws IllegalAccessException
* @throws InvocationTargetException
* @throws NoSuchMethodException
*/
private void checkPropertiesOfAttachement(List<Attachment> attachements) throws AirtableException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
if (attachements != null) {
for (int i = 0; i < attachements.size(); i++) {
if (propertyExists(attachements.get(i), FIELD_ID) || propertyExists(attachements.get(i), "size")
|| propertyExists(attachements.get(i), "type") || propertyExists(attachements.get(i), "filename")) {
final Field[] attributesPhotos = attachements.getClass().getDeclaredFields();
for (Field attributePhoto : attributesPhotos) {
final String namePhotoAttribute = attributePhoto.getName();
if (FIELD_ID.equals(namePhotoAttribute) || "size".equals(namePhotoAttribute)
|| "type".equals(namePhotoAttribute) || "filename".equals(namePhotoAttribute)) {
if (BeanUtils.getProperty(attachements.get(i), namePhotoAttribute) != null) {
throw new AirtableException("Property " + namePhotoAttribute + " should be null!");
}
}
}
}
}
}
}
示例12: _get
import org.apache.commons.beanutils.BeanUtils; //导入依赖的package包/类
/********************************** get *************************************/
@Override
public <T> T _get(Class<T> clazz, Long pv) throws Exception {
Map<String, Object> param = new HashMap<>();
String tableName = GeneralMapperReflectUtil.getTableName(clazz);
String primaryKey = GeneralMapperReflectUtil.getPrimaryKey(clazz);
List<String> queryColumn = GeneralMapperReflectUtil.getAllColumns(clazz, false);
param.put("_table_", tableName);
param.put("_pk_", primaryKey);
param.put("_pv_", pv);
param.put("_query_column_", queryColumn);
Map map = sqlSession.selectOne(Crudable.GET, param);
T t = clazz.newInstance();
BeanUtils.populate(t, map);
return t;
}
示例13: setProperties
import org.apache.commons.beanutils.BeanUtils; //导入依赖的package包/类
public static void setProperties(Object obj, Map map){
Field[] fields = obj.getClass().getDeclaredFields();
for(Field field : fields) {
Column column = field.getAnnotation(Column.class);
String dbColumnname = null;
if ("".equals(column.name()))
dbColumnname = field.getName();
else dbColumnname = column.name();
try {
Object value = map.get(dbColumnname);
if (value != null) {
BeanUtils.setProperty(obj, field.getName(), value);
}
} catch (Exception e) {
log.catching(e);
}
}
}
示例14: getOrderedKeyset
import org.apache.commons.beanutils.BeanUtils; //导入依赖的package包/类
protected List getOrderedKeyset(Set keys, Object param) throws Exception {
List orderedKeyset = new ArrayList();
if (keys.size() > 0) {
String firstitems = BeanUtils.getProperty(param,
"_firstitems");
String firstitemname = null;
if (firstitems != null) {
for (StringTokenizer st = new StringTokenizer(firstitems, ","); st
.hasMoreTokens(); ) {
firstitemname = st.nextToken();
if (keys.contains(firstitemname)) {
orderedKeyset.add(firstitemname);
}
}
}
for (Iterator it = keys.iterator(); it.hasNext(); ) {
String key = (String) it.next();
if (!orderedKeyset.contains(key))
orderedKeyset.add(key);
}
}
return orderedKeyset;
}
示例15: edit
import org.apache.commons.beanutils.BeanUtils; //导入依赖的package包/类
/**
* Edits specified LTI tool consumer
*/
public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
initServices();
DynaActionForm ltiConsumerForm = (DynaActionForm) form;
Integer sid = WebUtil.readIntParam(request, "sid", true);
// editing a tool consumer
if (sid != null) {
ExtServer ltiConsumer = integrationService.getExtServer(sid);
BeanUtils.copyProperties(ltiConsumerForm, ltiConsumer);
String lessonFinishUrl = ltiConsumer.getLessonFinishUrl() == null ? "-" : ltiConsumer.getLessonFinishUrl();
request.setAttribute("lessonFinishUrl", lessonFinishUrl);
// create a tool consumer
} else {
//do nothing
}
return mapping.findForward("ltiConsumer");
}