java后台把一个对象 放入map中 传到jsp页面 页面中怎么把这个对象取出来

代码:HashMap<String,Object> map = new HashMap();
map.put("user",user);
map.put("foods",foods);
页面:User user=((User)((Map<String,Object>)request.getParameter("map")).get("user")).get("user");
页面代码报错

把最后的一个get(“user”)去掉就行了。
最后你还是分步来吧,这样写以后不好维护。
1.获取你方法的map
HashMap<String,Object> map = (HashMap<String,Object>)request.getParameter("map"));
2.遍历map中的数据
User user = (User)map .get("user");
Food food = (Food)map.get("foods");
就可以了。追问

这样不行 报错 cannot cash from string to hashmap

追答

你在后面的代码把你的map放进去了吗?
request .setAttribute("map",map);
你不光把对象放到map中,你还要把map放到request中,这样你在jsp页面才能拿到map的。

追问

可以乐 谢谢诶
我还想问一个问题
我去到了 List foods

${food.foodName }

这样遍历 结果没反应

温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-05-11
你得把map用 request.setAttribute()放到request中
然后jsp里用 request.getAttribute()取出来
Parameter指的是客户端用get或者post方式提交到服务器的参数
服务器端要给客户端传递新的信息只能用attribute追问

我用spring左的 不用这样吧
页面上 我已经把user 里面的username用el表达式取到了
现在我想 在这里面)request.getParameter("map")).get("user")).get("user");
%>
把username 取出来 放session里面

追答

去掉一个get("user")试试

第2个回答  2012-05-11
首先看你页面能收到map吗 不能的话还得在你后台传出来,收到的话你就直接就可以get("user")取user了
第3个回答  2012-05-11
你后台用存了没。
request .setAttribute("map",map);

页面
request .getAttribute("map"); 然后就可以装换成Map了
第4个回答  2012-05-11
ModelAndView 把此方法如何调用的贴出来看看追问

这个问题 解决了 我用setAttribute就好了 。。。。。。用了ModelAndView还要setAttribute吗? 很费解

追答

不会是你的foods又没那样设置吧

追问

设置了 和 user 放一个map里 也取出来了
那个el表达式 不知道对不对。。

追答

items="${foods}" foods 怎么取得的
是从map中取出来然后保存到一个对象中的吗?

追问

可以了 el表达式写错了

追答

这样子items="${foods}" 这个就null了 你要么用<c:set 设置一个

本回答被提问者采纳
相似回答