well, didn't know if this is what you're looking for, but recently i've discovered how to use indexed properties:

first, the jsp (sorry for the spanish comments):

<%@ taglib prefix="logic" uri="/WEB-INF/struts-logic.tld" %>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<head>
<title>Example of a Bean Based Purchase Order</title>
</head>
<h1>Example of a Bean Based Purchase Order</h1>

<html:form action="/generateBeanPO">
<TABLE WIDTH="80%" BORDER="1">
<TR><TH align="left">Part Number</TH>
<TH align="left">Quantity</TH>
<TH align="left">Price</TH></TR>
<!-- watch out with the id, en the form bean there must be a method related to it -->
<logic:iterate id="linea" name="purchaseOrderBeanForm" property="lines">
<TR><TD><html:text indexed="true" name="linea" property="partNumber"/></TD>
<TD><html:text indexed="true" name="linea" property="quantity"/></TD>
<TD><html:text indexed="true" name="linea" property="price"/></TD></TR>
</logic:iterate>
</table>
<html:hidden property="accion" value="capturado"/>
<html:submit/>
</html:form>



now in the ActionForm package org.hospital;



import org.apache.struts.action.*;

import java.util.ArrayList;



public class PurchaseOrderBeanForm extends ActionForm {

        /**
         * Propiedad Lines.
         * Almacena todo el conjunto de lineas
         */
        private ArrayList lines = null;


/** * Devuelve el valor de la propiedad lines . * [EMAIL PROTECTED] El valor actual de Lines */ public ArrayList getLines() { return this.lines; }


/** * Establece el nuevo valor para la propiedad lines . * [EMAIL PROTECTED] lines El nuevo valor para lines . */ public void setLines(ArrayList lines) { this.lines = lines; }


/** * Returns an linea object from the lines ArrayList. * It is related to the linea property in the jsp * <logic:iterate> * [EMAIL PROTECTED] El valor actual de Linea */

        public POLine getLinea(int index) {
                return (POLine)this.lines.get(index);
        }


/** * Establece el nuevo valor para la propiedad linea. * [EMAIL PROTECTED] lines El nuevo valor para linea. */ public void setLinea(int index, POLine valor) { this.lines.add(index, valor); }

        /**
         * Propiedad Accion.
         */
        private String accion = null;


/** * Devuelve el valor de la propiedad accion . * [EMAIL PROTECTED] El valor actual de Accion */ public String getAccion() { return this.accion; }


/** * Establece el nuevo valor para la propiedad accion . * [EMAIL PROTECTED] accion El nuevo valor para accion . */ public void setAccion(String accion) { this.accion = accion; } }


the POLine object:

package org.hospital;


public class POLine { private String partNumber; private String quantity; private String price; private double total;

        public String getPartNumber() {
                return partNumber;
        }

        public void setPartNumber(String partNumber) {
                this.partNumber = partNumber;
        }

        public String getQuantity() {
                return quantity;
        }

        public void setQuantity(String quantity) {
                this.quantity = quantity;
        }

        public String getPrice() {
                return price;
        }

        public void setPrice(String price) {
                this.price = price;
        }

        public double getTotal() {
                return total;
        }

        public void setTotal(double total) {
               this.total = total;
        }
}


The Action:

import org.apache.struts.action.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import java.util.ArrayList;

public class GenerateBeanPO extends Action {
  String mapeo = "inicializa";
  public ActionForward execute(ActionMapping mapping,
                               ActionForm form,
                               HttpServletRequest request,
                               HttpServletResponse response)
  throws ServletException, IOException {

    PurchaseOrderBeanForm poForm = (PurchaseOrderBeanForm) form;
    ArrayList lines = new ArrayList();
    System.out.println("**** Estoy en GenerateBeanPO ******");
    //llenamos el ArrayList
    if(poForm.getAccion() == null) {
      for(int i = 0; i < 10; i++) {
        POLine linea = new POLine();
        linea.setPartNumber(String.valueOf(i));
        lines.add(linea);
      }
      poForm.setLines(lines);
      mapeo = "inicializa";
    } else {
      try {
        //recover ArrayList
        ArrayList lineas = poForm.getLines();

        //and show it
        for (int i = 0; i < lineas.size(); i++) {
          POLine linea = (POLine)lineas.get(i);


System.out.println("***********************"); System.out.println("Articulo: " + linea.getPartNumber()); System.out.println("Cantidad: " + linea.getQuantity()); System.out.println("Precio: " + linea.getPrice()); System.out.println("***********************\n"); }

      }catch (Exception e) {
        System.out.println("***** Error >>>>>>>>");
        e.printStackTrace();
      }
      mapeo = "success";
    }
    return mapping.findForward(mapeo);
  }
}


and last, the relevant struts-config.xml parts: .... <form-bean name="purchaseOrderBeanForm" type="org.hospital.PurchaseOrderBeanForm"/> ....

<action path="/generateBeanPO"
        type="org.hospital.GenerateBeanPO"
        name="purchaseOrderBeanForm"
        input="/purchaseOrderBean.jsp">
  <forward name="success"
           path="/purchaseOrderBean.jsp"
           redirect="false"/>
  <forward name="inicializa"
           path="/purchaseOrderBean.jsp"
           redirect="false"/>
</action>



hope this helps

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



Reply via email to