Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • 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 ...
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • salvin francis
  • fred rosenberger

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/">
<bean:write name="cartid" property="key"/>
<html:hidden name="cartid" property="key"/>
<html:text name="cartid" property="value"/>
But the key I get is:" " The value I get is "1".
Why? 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(){
public void setName(String name){;
public String getName(){
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:
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 ]
I would challenge you to a battle of wits, but I see you are unarmed - shakespear. Unarmed tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
    Bookmark Topic Watch Topic
  • New Topic