Child pages
  • Tabular inputs
Skip to end of metadata
Go to start of metadata

Sometimes you need a way to enter tabular data such as list of quantity for products in a shopping cart, marks from a list of examination candiates, etc. If you just have one input value per line item, you can use a HashMap to store the value. This can be expanded to support multiple input values by having multiple HashMap. This describes a number of alternatives using some of more advanced features of WebWork. Assume you want to capture the quantity and a gift note for a list of products in a shopping cart (i.e Amazon).

1. When the number of line items is known

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

the cart.jsp file (non altSyntax)

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

the UpdateCartAction.class

the Cart.class

the CartItem.class

Explanation

The resulting html code is rendered as

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

for the first shown line in the rendered result.

2. When the number of line items is unknown

For example, you want to allow the user to enter any number of ISBN, quanty and a note. You can replace ArrayList with XWorkList, which will automatically create new items if the index is greater than the size of the list.

3. Use Type Conversion

If you want more advanced way to do this, check out Type Conversion documentation.

  • No labels

1 Comment

  1. It's a good tutorial. thank you.

    There are errors with the jsp file. There should be another "/" to close the <s:hidden> tag.

    and the UpdateCartAction class:

    Cart result = ActionContext.getContext().getSession.get("cart.key");

    it should be casted to Cart:
    Cart result = *(Cart)*ActionContext.getContext().getSession.get("cart.key");