• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

Help! problem while using logic:iterate with HashMap

 
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:
//////////////////////////////////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:
 
Greenhorn
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.
Matt.
[ October 25, 2003: Message edited by: Matt Read ]
 
Can't .... do .... plaid .... So I did this tiny ad instead:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!