File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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

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/">
<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:
Matt Read

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.
[ October 25, 2003: Message edited by: Matt Read ]
I agree. Here's the link:
subject: Help! problem while using logic:iterate with HashMap
jQuery in Action, 3rd edition