Kevin HaleBoyes wrote:
>
> In order to set my bean properties based on an incoming (POST'ed) HTML form,
> I'm using the following construct in a JSP page:
>
> <jsp:useBean id="ord" class="ord.OrderBean" scope="request">
>   <jsp:setProperty name="ord" property="*"/>
> </jsp:useBean>
>
> It works fine except now I want to process something a little different.  I'm
> working on an Order entry system and the Order Items are a repeating group of
> fields.  In my HTML form I have <input> fields named like "partNumber0,
> quantity0, price0" and "partNumber1, quantity1, price1" that repeat for each
> potential item in an order.
>
> Is there any way to use the <jsp:setProperty> construct to automatically set
> the properties in an OrderItems bean.  I'm guessing it might have something to
> do with indexed properties but I really can't see how this would work.  If I
> have my bean as
>
> public class OrderItemBean
> {
>    String[] partNumber;
>    ...
>
>    public void setPartNumber( int index, String value ) {...}
>    ...
> }
> [...]

No, indexed properties will not work here. They work only for a set of
parameters with the same name, for instance for a group of check boxes or
radio buttons.

In your case, all parameters have unique names ending with a sequence
number, as they should since you're group multiple pieces of info (part
number, quantity and price) that's all related by the sequence number.

I suggest that you use a custom action to handle this. It can read all
request parameters, create a bean for each order, and maybe make it
available to the rest of the application as a Vector of beans. Something
like this:

  public class MyTag extends TagSupport {
    ...
    public void doStartTag() {
      Vector orders = new Vector();
      int seq = 0;
      ServletRequest request = pageContext.getRequest();
      String partNo = request.getParameter("partNumber" + seq);
      while (partNo != null) {
         OrderItemBean b = new OrderItemBean(partNo,
           request.getParameter("quantity" + seq),
           request.getParameter("price" + seq))
         orders.addElement(b);
        seq++;
        String partNo = request.getParameter("partNumber" + seq);
      }
      pageContext.setAttribute("orders", beans);
    }
  }

Hans
--
Hans Bergsten           [EMAIL PROTECTED]
Gefion Software         http://www.gefionsoftware.com
Author of JavaServer Pages (O'Reilly), http://TheJSPBook.com

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to