2013-06-20 28 views
1

我哈瓦具有在不同場景如何獲取參數名稱的字符串數組在「parameterMap的」(在Java中)

完全不同參數的parameterMap的一套我需要的是「鑰匙」的字符串數組在參數圖

最近我得到的是使用reportParams.toString();

這裏是我用於獲取如下因素輸出

//The Code 
ParameterMap reportParams = context.getRequestParameters(); 
System.out.println(reportParams.toString()); 

//輸出

地圖[ '用戶名' - > '用戶', '裝飾' - > '無',「 decorator' - >'empty','ajax' - >'true','_eventId' - >'refreshReport','VEFactorSelection' - >'1','campusAndFaculty' - > array ['111','113',' '115','118','112','114','116','117','21907','21908','99040','99010','99100','99230','99240 '],'_flowExecutionKey' - >'e4s1','reportLanguage' - >'3','date' - >'2013/06/20','nameType2' - >'1','confirm' - >'true ']

所以我想作爲最終的結果是

用戶名, 裝修, 裝飾, 阿賈克斯, _ecentId, VEFactorSelection, campusAndFaculty, _flowExecutionKey, reportLanguage, 日期, nameType2, 確認

as a String in a Array

============================== = 所以代碼現在看起來像:

ParameterMap reportParams = context.getRequestParameters(); 

final List<String> names = new ArrayList<String>(); 

for (final Object o: reportParams.asMap().keySet()) 
names.add((String) o); 

final String[] array = names.toArray(new String[names.size()]); 

System.out.println(array[0]); 

最終結果:

============================ ======= org.springframework.beans.ConversionNotSupportedException:未能將類型'java.util.LinkedHashMap'的屬性值轉換爲屬性'readOnlyConfiguredExporters'所需的類型'org.hibernate.mapping.Map';嵌套異常是java.lang.IllegalStateException:不能類型的值轉換[java.util.LinkedHashMap中]至所需的類型[org.hibernate.mapping.Map用於屬性「readOnlyConfiguredExporters」:沒有匹配的編輯器或轉換策略發現

=============================== 一些額外的的

這裏是 「parameterMap的」 http://static.springsource.org/spring-webflow/docs/1.0.x/api/org/springframework/webflow/core/collection/ParameterMap.html

的API
+0

「我不能使用.keySet()」 < - 是的,你可以 – fge

+0

哎呀對不起笑新來這個哈哈哈 – Andre

回答

3

A ParameterMap implements MapAdaptable,它有.asMap()返回Map。我不知道你的版本是否使用泛型;如果這樣做,很容易:

final List<String> names = new ArrayList<String>(map.asMap().keySet()); 

如果沒有:

final List<String> names = new ArrayList<String>(); 

for (final Object o: map.asMap().keySet()) 
    names.add((String) o); 

之後,如果你真的String[]代替List<String>,使用:

final String[] array = list.toArray(new String[list.size()]); 
+0

得到了一個錯誤 java.lang.ClassCastException:org.springframework.webflow.core.c ollection .LocalParameterMap不能轉換爲java.util.Map 不確定要導入什麼圖 – Andre

+0

Cast?呃。上面的代碼沒有轉換... – fge

+0

oops錯誤的表單,Thx人你的代碼工程:D! – Andre

0

如果它確實是Map,那就做map.keySet().toArray()

0

您可以使用:

String names[] = reportParams.keySet().toArray(new String[0]); 
相關問題