Re: Help: How do I iterate and submit to the server? works with struts not

Rakon <[email protected]>
Newsgroups gmane.comp.java.open-symphony.webwork
Message-ID <30565578.1148251887277.JavaMail.os-j2ee@opensymphony01.managed.contegix.com>
Rainer, 

Here is a better example.  A lot of online shopping sites have a shopping cart where you can update the quantity of the items in the cart.  Basically, an iteratively list that needs to be updated and persisted.

Any help is much appreciatd. Thanks!

[u][b]JSP code:[/b][/u]
<%@ taglib uri="webwork" prefix="ww" %>

<ww:form name="save" namespace="sample" action="cart!save.action" validate="false" method="POST">
	<ww:iterator value="shoppingCart">
		<ww:property value="id"/>
		<ww:property value="name" />
		<ww:textfield label="Quantity" name="quantity" />
	</ww:iterator>
	<ww:submit value="Save" />
</ww:form>

[u][b]Action code:[/b][/u]
package test.demo;

import java.util.Map;
import java.util.Vector;

import com.opensymphony.xwork.Action;
import com.opensymphony.xwork.ActionContext;

public class CartAction {
	
	public static class Product {
		private Long id;
		private String name;
		private Integer quantity;
		public Product(Long id, String name, Integer quantity) {
			this.id =id;
			this.name = name;
			this.quantity = quantity;
		}
		public Long getId() {
			return id;
		}
		public String getName() {
			return name;
		}
		public Integer getQuantity() {
			return quantity;
		}
		public void setQuantity(Integer quantity) {
			this.quantity = quantity;
		}
	}
	
	public String execute() throws Exception {
		shoppingCart = new Vector(3);
		shoppingCart.add(new Product(new Long(1), "Ball", new Integer(1)));
		shoppingCart.add(new Product(new Long(2), "Bat", new Integer(3)));
		shoppingCart.add(new Product(new Long(3), "Glove", new Integer(3)));
		saveCart(shoppingCart);
		return Action.SUCCESS;
	}
	
	public String save() throws Exception {
		saveCart(shoppingCart);
		return Action.SUCCESS;
	}
	
	private Vector shoppingCart;

	private void saveCart(Vector cart) {
		Map session = (Map) ActionContext.getContext().get("session");
		session.put("cart", cart);		
	}
	private void loadCart() {
		Map session = (Map) ActionContext.getContext().get("session");
		shoppingCart = (Vector)session.get("cart");		
	}
	
	public Vector getShoppingCart() {
		if (shoppingCart == null) {
			loadCart();
		}
		return shoppingCart;
	}
}
---------------------------------------------------------------------
Posted via Jive Forums
http://forums.opensymphony.com/thread.jspa?threadID=31674&messageID=61412#61412

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.