Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

1. When the number of line items is known

If you are using JSP:
the cart.jsp file in altSyntax

Code Block

<ww:iterator value="cart.items">
  <ww:hidden name="cart.items[%{#rowstatus.index}].productId" value="%{productId}">
  <ww:textfield name="cart.items[%{#rowstatus.index}].qty" value="%{qty}" />
  <ww:textfield name="cart.items[%{#rowstatus.index}].note" value="%{note}" />
</ww:iterator>

the cart.jsp file (non altSyntax)

Code Block

<ww:iterator value="cart.items">
  <ww:hidden name="'cart.items[' + #rowstatus.index + '].productId'" value="productId">
  <ww:textfield name="'cart.items[' + #rowstatus.index + '].qty'" value="qty" />
  <ww:textfield name="'cart.items[' + #rowstatus.index + '].note'" value="note" />
</ww:iterator>

Alternatively, if you use Velocity as your view technology of choice:
the cart.vm file

Code Block
#foreach ( $item in $cart.items )
  #set($index = $velocityCount - 1)
  <input type="hidden" name="cart.items[$index].productId" value="$item.productId">
  <input type="text" name="cart.items[$index].qty" value="$item.qty">
  <input type="text" name="cart.items[$index].note" value="$item.note">
#end

...

Code Block
public class UpdateCartAction extends ActionSupport {

	public Cart getCart() {
                // Lazy initialization
                Cart result = ActionContext.getContext().getSession.get("cart.key");
                if ( result == null ) {
		return
                        result = new Cart();
                        ActionContext.getContext().getSession.put("cart.key", result);
                }
		return result;
	}

	public String execute() throws Exception {
                // Just ensuring our cart is initialized...
		Cart cart = getCart();

		// loop through a
	}
}

...

Code Block
public class CartItem implements Serializable {
  private int qty;
  private int productId;
  private String note;

  // getters/setters...
}

Explanation

The resulting vm file html code is rendered as

Code Block
<input type="hidden" name="cart.items[0].qty.productId" value="1">
<input type="text" name="cart.items[0].qty" value="2">
<input type="text" name="cart.items[0].note" value="This is a fun book!">

<input type="hidden" name="cart.items[1].qty.productId" value="2">
<input type="text" name="cart.items[1].qty" value="2">
<input type="text" name="cart.items[1].note" value="You love this one">

<input type="hidden" name="cart.items[2].qty.productId" value="3">
<input type="text" name="cart.items[2].qty" value="$item.qty">
<input type="text" name="cart.items[2].note" value="">

Webwork will populate all the entries in Cart with the correct values.
In depth, the ParametersInterceptor would apply the form results to our model, leading to the call similar like

Code Block

        ((CartItem) updateCartAction.getCart().getItems().get(0)).setProductId(1);

for the first shown line in the rendered result.

2. When the number of line items is unknown

...