On 11/9/05, Danny Lee <[EMAIL PROTECTED]> wrote:
> Hi Michael,
>
> Persistance is no issue, becouse I have implemented managers, which can
> be simply used anywhere. Something like:
>
> "On update PersistanceManager.UpdateProductCount(prodID, newCount)"
>
> Well the question was about multi row update. It means for every list
> line I have a "Delete" button (no problem), and a update text-field
> where I can input custom integers and then submit it. Then the only
> thing I want is to be able to access the submited values as a array of
> custom objects like in this small example:
>
> http://www.developer.com/java/ent/article.php/2233591
>
> But using Struts Dialogs and without needing for "preAction" and
> "postAction" like they do in the example above.

This is quite simple. Just do what is done in article, the only thing
you need to remember is to initialize your cart before a its JSP page
is shown, that is, to initialize cart before getDialogView() is
called, or in getDialogView() itself. For example, this creates a
sample cart out of three items:

    public ActionForward getDialogView(...) {

        DynaActionForm poForm = (DynaActionForm) form;
        CartItem[] lines = (CartItem[]) poForm.get("lines");

        // Initialize shopping cart with free gifts from our store
        if (lines == null || lines.length == 0) {
            lines = new CartItem[] {
                new CartItem(new ShoppingItem("BFG-666", "Vodoo Doll",
2000), 1),
                new CartItem(new ShoppingItem("aaa", "some aaa", 100), 2),
                new CartItem(new ShoppingItem("bbb", "some bbb", 350), 3)
            };
            poForm.set("lines", lines);
        }
        // Return standard mapping for action view. We can define this view
        // using "view" attribute of <component> property,
        // or a <forward name = "DIALOG-VIEW" .../> element of <action> element.
        return super.getDialogView(mapping, form, request, response);
    }

Here is how the mapping can be defined, using new <component> element
from Struts Dialogs 1.23:

        <component path = "/Cart"
                   view = "/shoppingpages/cart.jsp"
                   type = "net.jspcontrols.shoppingcart.CartAction"
                   form = "cartForm">
        </component>

Anytime you navigage to /Cart.do using GET requst, that is, by typing
in the browser or using a link, the action will display cart.jsp page.

The page can be as simple as this:

  <html:form action="/Cart.do">
  <TABLE WIDTH="80%" BORDER="1">
    <TR>
      <TH align="left">Art.No.</TH>
      <TH align="left">Name</TH>
      <TH align="left">Price</TH>
      <TH align="left">Quantity</TH>
      <TH align="left">Operations</TH>
    </TR>
    <c:forEach var="item" items="${cartForm.map.lines}" varStatus="status">
      <TR>
        <TD><c:out value="${item.articleNo}"/></TD>
        <TD><c:out value="${item.name}"/></TD>
        <TD><c:out value="${item.price}"/></TD>
        <TD><input type="text"
                   name="lines[<c:out value="${status.index}"/>].quantity"
                   value="<c:out value="${item.quantity}"/>"
                   size="3"></TD>
        <TD><html-el:link
            href="Cart.do?DIALOG-EVENT-DELETE&itemID=${status.index}">
            Remove Item
            </html-el:link></TD>

      </TR>
    </c:forEach>
  </table>
  <br/>
  <html:submit property="DIALOG-EVENT-CHANGE">Update quantity</html:submit>
  </html:form>

There are two commands: Delete Item and Update Quantity. These
commands should invoke corresponding handlers in the CartAction class.
You need to create the handlers and to map events to method names,
like this:

    protected Map getKeyMethodMap() {
        Map methodMap = new HashMap();
        // By default, getInitKey() returns "DIALOG-EVENT", the standard
        // event prefix for SelectAction and DialogAction
        methodMap.put("DIALOG-EVENT-CHANGE", "onUpdate");
        methodMap.put("DIALOG-EVENT-DELETE", "onDelete");
        return methodMap;
    }

Actually, you do not need to do much for update, since Struts will
update your nested array for you. For deletion, you need to define
"itemID" property in your form bean, which will hold the ID of item to
delete.

Write back if you have more questions.

Michael.

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to