日期:2014-05-20  浏览次数:20991 次

关于java中类型转换的问题
request.getSession().getAttribute("cartList")怎么转成HashMap类型的呢,我用 HashMap hMap = (HashMap)request.getSession().getAttribute("cartList");然后用hMap.size()计算大小,出现类型转换异常了,各位高手帮帮忙,谢谢了

异常信息如下
java.lang.ClassCastException: java.util.HashMap$Values cannot be cast to java.util.HashMap
at org.apache.jsp.lookshopcart_jsp._jspService(lookshopcart_jsp.java:113)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

------解决方案--------------------
request.getSession().getAttribute("cartList")

cartList是List类型?迭代List,放入HashMap中,或者你一开始放进去就用map类型。