2016-08-23 36 views
1

這裏,請求帕拉姆字符串是:如何使用BeanUtils轉換器來列表?

的firstName =插孔&的lastName =百合&性別= 1種&食物=牛排&食物=比薩&報價=輸入你+ +收藏+報價! &教育= Jr.High & TOFD =日

,並映射類是:

public class Student { 
    private String firstName; 
    private String lastName; 
    private Integer gender; 
    private List<String> foods; 
    private String quote; 
    private String education; 
    private String tOfD; 

    getXxx()....; 
    setXxx()....; 
} 

而現在,我想編寫一個通用的Util類的字符串轉換爲一個bean。

public final class InjectUtil<T> { 
private static final Logger LOGGER = LoggerFactory.getLogger(InjectUtil.class); 

public static <T> T converter2Obj(String source, Class<T> tClass) { 
    T t = null; 
    try { 
     t = tClass.newInstance(); 

     if(source != null && source.length() > 0) { 
      String[] fields = source.split("&"); 
      for(String field : fields) { 
       String[] fieldKeyValue = field.split("\\="); 
       String fieldKey = fieldKeyValue[0]; 
       String fieldValue = fieldKeyValue[1]; 
       // help 
      } 
     } 
    } catch(InstantiationException | IllegalAccessException | InvocationTargetException e) { 
     e.printStackTrace(); 
    } 
    return t; 
} 

}

照顧的幫助下,我該如何使用BeanUtils的轉換器中的「食品=牛排&食物=比薩」的列表屬性。

回答

0

這裏是做這件事的方式:

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.Map; 

import org.apache.commons.beanutils.BeanUtils; 

public final class InjectUtil<T> { 
    // private static final Logger LOGGER = 
    // LoggerFactory.getLogger(InjectUtil.class); 

    public static <T> T converter2Obj(String source, Class<T> tClass) { 
     T t = null; 

     try { 
      t = tClass.newInstance(); 
      Map<String, Object> params = new HashMap<String, Object>(); 
      if (source != null && source.length() > 0) { 
       String[] fields = source.split("&"); 
       for (String field : fields) { 
        String[] fieldKeyValue = field.split("\\="); 
        String fieldKey = fieldKeyValue[0]; 
        String fieldValue = fieldKeyValue[1]; 

        if (params.containsKey(fieldKey)) { 

         //the key does not exist as yet 
         Object keyValueRetrieved = params.get(fieldKey); 
         if (keyValueRetrieved instanceof String) { 
          //key exists , it is single value that has been added now. 
          //change that now to a list to add more values 
          ArrayList<String> values = new ArrayList<String>(); 
          values.add(keyValueRetrieved.toString()); 
          values.add(fieldValue); 
          params.put(fieldKey, values); 
         } else { 
          //key exists , it is a list already. Add more values 
          ((ArrayList<String>) keyValueRetrieved).add(fieldValue); 
         } 
        } else { 
         params.put(fieldKey, fieldValue); 
        } 

       } 
      } 
      BeanUtils.populate(t, params); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     System.out.println("--------------------------------------------"); 
     System.out.println(t.toString()); 
     System.out.println("--------------------------------------------"); 
     return t; 
    } 
} 

有優化,你可以做,希望這給你一個想法

0

您可以使用以下方式,您需要填充foodList(值爲[Steak,Pizza]),然後使用以下方法設置爲bean。

PropertyUtils.setProperty(studentBean, "foods", foodList); 

BeanUtils.setProperty(studentBean, "foods", foodList);