Results 1 to 6 of 6

Thread: Having multiple command parameters in a Collection

  1. #1
    Join Date
    Jun 2007
    Location
    Oslo, Norway
    Posts
    153

    Default Having multiple command parameters in a Collection

    Does spring have mechanisms to allow a command object that is an collection of a JavaBean containing data?

    My form has four elements for text input. I want to show multiple rows of these Each row containing the same sets of data only with different values. The number of rows is choosen by the user by a "add row" button.

    Code:
    <input type="text" name="input1">
    <input type="text" name="input2">
    <input type="text" name="input3">
    <input type="text" name="input4">
    The problem I see is that the name must contain the row number(name="input4row1") or otherwise I will not get multiple values for the inputs.

    Could I construct the command object as an ArrayList where each rows is stored in a JavaBean and put in the list. But I am not sure how to handle this in JSP.

  2. #2
    Join Date
    Apr 2005
    Location
    Hyderabad
    Posts
    21

    Default

    Quote Originally Posted by DJViking View Post
    The problem I see is that the name must contain the row number(name="input4row1") or otherwise I will not get multiple values for the inputs.
    Spring will return comma seperated values for the Form fields which are having same name.

    and if you want, you can create your own property editor which can convert this csv to List.

    Code Snippet of prorperty editor is

    public void setAsText(String text) throws IllegalArgumentException
    {
    if (StringUtil.isBlank(text))
    {
    this.setValue(null);
    return;
    }


    this.setValue(StringToList(text, ","));
    }
    Tanmay

  3. #3
    Join Date
    Jun 2007
    Location
    Oslo, Norway
    Posts
    153

    Default

    Thanx I didn't know that. Read two Spring books and either that fact was omitted or between the lines.

    That solves the submit problem, but then how can I use <spring:nestedPath> and <spring:bind> to populate the form with the a command object created in formBackingObject when I have multiple command objects/values?
    Last edited by DJViking; Aug 15th, 2007 at 06:36 AM.

  4. #4
    Join Date
    Apr 2005
    Location
    Hyderabad
    Posts
    21

    Default

    In my case, I have passed the data to view through XML and using javascript I have populated the values.
    So I am not sure how <spring:bind> will behave.
    But <c:forEach> may help you to iterate through values.
    Tanmay

  5. #5
    Join Date
    Nov 2005
    Location
    Reutlingen, Germany
    Posts
    2,098

    Default

    Quote Originally Posted by DJViking View Post
    Does spring have mechanisms to allow a command object that is an collection of a JavaBean containing data?
    Strictly speaking no. But you can create a dumb DTO with just one property of your collection type holding the objects.

    And another thing that I want to clarify: Better don't talk about multiple command objects. It's actually a composite object, the DTO is your command.
    Multiple command objects would be needed for multiple forms on a page, but there seem to be a problem with that one.

    The path is just created by using indexes: dto.collection[i].property. That's the standard property syntax for collections.

    Joerg
    This post can contain insufficient information.

  6. #6
    Join Date
    Nov 2005
    Location
    Reutlingen, Germany
    Posts
    2,098

    Default

    Quote Originally Posted by tanmay View Post
    Spring will return comma seperated values for the Form fields which are having same name.
    I don't see how this applies here!?

    Joerg
    This post can contain insufficient information.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •