[Ljava.lang.String; cannot be cast to java.lang.String
2018-02-17 18:51
323 查看
试图通过resquest对象获取前端请求键值对,即request.getParamMap()
本来代码如下:
Map<String,String> map=request.getParamMap();
Map<String,String> map= request.getParameterMap();
Set<String> set=map.keySet();
Iterator<String> it=set.iterator();
while(it.hasNext())
{
String next= it.next();
System.out.println("key:"+next+"->value:"+map.get(next));
}
/** Returns a java.util.Map of the parameters of this request.
* Request parameters
* are extra information sent with the request. For HTTP servlets,
* parameters are contained in the query string or posted form data.
*
* @return an immutable java.util.Map containing parameter names as
* keys and parameter values as map values. The keys in the parameter
* map are of type String. The values in the parameter map are of type
* String array.
*
*/返回一个request的请求参数map
重点看划线部分的of type String ,of type String array
就是说key是String
value是String[]
解决后代码:Map<String,String[]> map= request.getParameterMap();
Set<String> set=map.keySet();
Iterator<String> it=set.iterator();
while(it.hasNext())
{
String next= it.next();
System.out.println("key:"+next+"->");
for(String s:map.get(next))
{
System.out.println("value:"+s);
}
}
运行结果就正常了,
这告诉我们,多
ad6e
看源码,抠单词
本来代码如下:
Map<String,String> map=request.getParamMap();
Map<String,String> map= request.getParameterMap();
Set<String> set=map.keySet();
Iterator<String> it=set.iterator();
while(it.hasNext())
{
String next= it.next();
System.out.println("key:"+next+"->value:"+map.get(next));
}
然而报错,这是为什么呢?看源码,map来自request对象,查看源码即可知道:
/** Returns a java.util.Map of the parameters of this request.
* Request parameters
* are extra information sent with the request. For HTTP servlets,
* parameters are contained in the query string or posted form data.
*
* @return an immutable java.util.Map containing parameter names as
* keys and parameter values as map values. The keys in the parameter
* map are of type String. The values in the parameter map are of type
* String array.
*
*/返回一个request的请求参数map
重点看划线部分的of type String ,of type String array
就是说key是String
value是String[]
解决后代码:Map<String,String[]> map= request.getParameterMap();
Set<String> set=map.keySet();
Iterator<String> it=set.iterator();
while(it.hasNext())
{
String next= it.next();
System.out.println("key:"+next+"->");
for(String s:map.get(next))
{
System.out.println("value:"+s);
}
}
运行结果就正常了,
这告诉我们,多
ad6e
看源码,抠单词
相关文章推荐
- [Ljava.lang.String; cannot be cast to java.lang...
- hibernate:java.lang.String cannot be cast to [Ljava.lang.Object
- java.lang.ClassCastException: java.lang.String cannot be cast to [Ljava.lang.Object;
- 使用Struts2获取请求参数:Ljava.lang.String; cannot be cast to java.lang.String
- [Ljava.lang.String; cannot be cast to java.lang.String
- java.lang.String cannot be cast to [Ljava.lang.Object;
- java.lang.String cannot be cast to [Ljava.lang.Object
- java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;
- 关于java.lang.ClassCastException: java.lang.String cannot be cast to [Ljava.lang.Object的错误报告
- java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to java.lang.String
- [Ljava.lang.String; cannot be cast to java.lang.String错误 [转]
- [Ljava.lang.String; cannot be cast to java.lang.String错误
- 【ERROR】java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.String
- java.lang.String cannot be cast to [Ljava.lang.Object
- java.lang.String cannot be cast to [Ljava.lang.Object;错误的原因很邪恶
- [Ljava.lang.String; cannot be cast to java.lang.String错误 [转]
- Ljava.lang.String cannot to be cast java.lang.String错误原因
- java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.String
- [Ljava.lang.String; cannot be cast to java.lang.String错误 [转]
- [置顶] java.lang.String cannot be cast to [Ljava.lang.Object;错误的原因很邪恶