• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help! problem while using logic:iterate with HashMap  RSS feed

lyo Yashnoo
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone:
I want to write a shopping cart.I place all the things in HashMap.Now I want to get all the things in jsp.So I use struts Tag:
//////////////////////////////////////////////////////////////////<logic:iterate id="cartid" name="cartMap">
<td><form method="GET" name="cartform" action="/myweb/cartaction.do">
<bean:write name="cartid" property="key"/>
<html:hidden name="cartid" property="key"/>
<html:text name="cartid" property="value"/>
But the key I get is:"lyo.hotmail.shopping.Cartbean@d6a0e0 " The value I get is "1".
Why? lyo.hotmail.shopping.Cartbean is my bean I use it control my session cart.
The Cartbean code is:
public class Cartbean{
private String id=null;
private String name=null;
private String number=null;
private String categ=null;
Map h=null;
public Cartbean(){
h=new HashMap();
public void setId(String id){
public String getId(){
return this.id;
public void setName(String name){
public String getName(){
return this.name;
public void setNumber(String number){
public String getNumber(){
return this.number;
public void setCateg(String categ){
public String getCateg(){
return this.categ;
public void addtoCart(String name,HttpSession session){
//Cartbean bean=(Cartbean)session.getAttribute("cart");
String nam=(String)(h.get(name.toString()));
h.put(this,new Integer(1));
Integer num=(Integer)(h.get(name));
int numb=num.intValue()+1;
Integer inum=new Integer(numb);
public void update(String name,String number,HttpSession session){
public void updateitem(String name,String number){
public Map getCart(){
Map hash=new HashMap(h);
return hash;
How to get the key and value correctly? Thks! :roll:
Matt Read
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The .put() method of Map takes a key and value as parameters. Your code reads:

which means you're putting "this", i.e. the instance of your CartBean into the Map as the key.
I'm not sure I can suggest exactly what you should be doing as your CartBean class seems to be trying to be both a Cart and a CartItem.
You might want to seperate out the Cart (backed by a Map) from items that go into a Cart (seperate class with the getters and setters that you're currently defining in CartBean). The Cart class would have an addtoCart(CartItem item) method which does something like:

Or maybe I'm just misunderstanding what you're trying to do. Hope this helps.
[ October 25, 2003: Message edited by: Matt Read ]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!