【问题标题】:how do I get a List of Key values in ParameterMap (Spring)如何获取 ParameterMap (Spring) 中的键值列表
【发布时间】:2013-06-21 06:31:33
【问题描述】:

我有一个 ParameterMap 集,不同场景下的参数完全不同

我需要的是 ParameterMap 中的“键”字符串数组

我得到的最接近的是使用 reportParams.toString(); 这是我用来获得以下输出的内容

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

// 输出

map['username' -> 'user', 'decorate' -> 'no', 'decorator' -> 'empty', 'ajax' -> 'true', '_eventId' -> 'refreshReport', 'VEFactorSelection'-> '1', 'campusAndFaculty' -> 数组['111', '113', '115', '118', '112', '114', '116', '117', '21907 ', '21908', '99040', '99010', '99100', '99230', '99240'], '_flowExecutionKey' -> 'e4s1', 'reportLanguage' -> '3', '日期' -> '2013/06/20', 'nameType2' -> '1', '确认' -> 'true']

所以我想要的最终结果是

用户名、装饰、装饰器、ajax、_ecentId、VEFactorSelection、campusAndFaculty、_flowExecutionKey、reportLanguage、日期、nameType2、确认

我试过了

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]); // this part is just to see if i get output

以上代码的最终结果:

==================================== org.springframework.beans.ConversionNotSupportedException:转换属性失败属性“readOnlyConfiguredExporters”的“java.util.LinkedHashMap”类型的值到所需的“org.hibernate.mapping.Map”类型;嵌套异常是 java.lang.IllegalStateException:无法将类型 [java.util.LinkedHashMap] 的值转换为属性“readOnlyConfiguredExporters”所需的类型 [org.hibernate.mapping.Map]:找不到匹配的编辑器或转换策略

================================ 一些额外的

这里是“ParameterMap”的APIhttp://static.springsource.org/spring-webflow/docs/1.0.x/api/org/springframework/webflow/core/collection/ParameterMap.html

【问题讨论】:

  • 从您链接的 API 文档中,ParamterMap 似乎没有 asMap() 方法。它是MapAdaptable 接口的一部分,由LocalParameterMapMockParameterMap 实现。但是代码甚至不应该编译。

标签: java spring map


【解决方案1】:

这样就可以了

     Map<String, String> result = new LinkedHashMap<String, String>(parameterMap.size());
            for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
                if (entry.getValue().length > 0) {
                    result.put(entry.getKey(), entry.getValue()[0]);
                }
            }

Set<String> nameSet = result.keySet();
String[] namesHolder= new String[nameSet.size()];
nameSet.toArray(namesHolder);

你有它在namesHolder

已编辑答案。试试这个

【讨论】:

  • org.springframework.beans.ConversionNotSupportedException:无法将类型“java.util.LinkedHashMap”的属性值转换为属性“readOnlyConfiguredExporters”所需的类型“org.hibernate.mapping.Map”;嵌套异常是 java.lang.IllegalStateException:无法将类型 [java.util.LinkedHashMap] 的值转换为属性“readOnlyConfiguredExporters”所需的类型 [org.hibernate.mapping.Map]:找不到匹配的编辑器或转换策略
  • 已经改变了答案,我认为它应该解决,如果它不是多值映射
  • @ line 1 我得到这个编译错误:__ 类型映射不是通用的;它不能用参数 参数化
  • @ line 2 我得到这些编译错误:__ -Map.Entry 无法解析为类型 - ParameterMap 类型的方法 entrySet() 未定义
  • 谢谢它的工作,错误不知何故起源于其他地方
【解决方案2】:

你试过了吗:

ParameterMap reportParams = context.getRequestParameters();
Set<String> keys = reportParams.asMap().keySet();

【讨论】:

  • Yes____ org.springframework.beans.ConversionNotSupportedException:无法将类型“java.util.LinkedHashMap”的属性值转换为属性“readOnlyConfiguredExporters”所需的类型“org.hibernate.mapping.Map”;嵌套异常是 java.lang.IllegalStateException:无法将类型 [java.util.LinkedHashMap] 的值转换为属性“readOnlyConfiguredExporters”所需的类型 [org.hibernate.mapping.Map]:找不到匹配的编辑器或转换策略
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-26
  • 2013-08-29
  • 2021-06-10
  • 1970-01-01
相关资源
最近更新 更多