aspose file tools*
The moose likes Struts and the fly likes Help! problem while using logic:iterate with HashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Help! problem while using logic:iterate with HashMap" Watch "Help! problem while using logic:iterate with HashMap" New topic
Author

Help! problem while using logic:iterate with HashMap

lyo Yashnoo
Ranch Hand

Joined: Sep 15, 2003
Posts: 50
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:
//////////////////////////////////Cartbean.java///////////////////////////////////
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){
this.id=id;
}
public String getId(){
return this.id;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
public void setNumber(String number){
this.number=number;
}
public String getNumber(){
return this.number;
}
public void setCateg(String categ){
this.categ=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()));
if(null==nam){
this.setName(name);
h.put(this,new Integer(1));
}else{
Integer num=(Integer)(h.get(name));
int numb=num.intValue()+1;
Integer inum=new Integer(numb);
h.put(this,inum);
}
session.setAttribute("cart",this);
}
public void update(String name,String number,HttpSession session){
this.updateitem(name,number);
session.setAttribute("cart",this);
}
public void updateitem(String name,String number){
h.put(name,number);
}
public Map getCart(){
Map hash=new HashMap(h);
return hash;
}
How to get the key and value correctly? Thks! :roll:
Matt Read
Greenhorn

Joined: Jul 27, 2002
Posts: 4
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.
Matt.
[ October 25, 2003: Message edited by: Matt Read ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help! problem while using logic:iterate with HashMap
 
Similar Threads
How to use enhanced loop
.equals()-Regarding collections
equals()
HashMap
Programming style for compound compositions