本文整理匯總了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");
}