Populating a List in a Jakarta Struts DynaActionForm

Discussion in 'Java' started by Thomas, Jul 24, 2003.

  1. Thomas

    Thomas Guest

    Hi,

    I have a question concerning DynaActionForms in Jakarta Struts 1.1

    In my struts-config.xml I declare the following DynaActionForm and I
    would like to know if it is possible to prepopulate the ArrayList
    "cities" in struts-config.xml with some String objects containing city
    names, is that possible at all and if yes, can someone send me the xml
    code that does it?

    I am not looking for an alternative solution with a custom ActionForm
    or something like this, it's just an example and I just want to know
    if this is possible to do with DynaActionForms, so if the answer is
    "no" that's I am satisfied too.

    Thanks in advance

    Thomas


    excerpt from my struts-config.xml
    =================================

    <form-bean name="personaldataForm"
    type="org.apache.struts.validator.DynaValidatorForm">
    <form-property name="firstname" type="java.lang.String"
    initial="someName" />
    <form-property name="lastname" type="java.lang.String"
    initial="someothername" />
    <form-property name="street" type="java.lang.String"
    initial="somestreet" />
    <form-property name="zip" type="java.lang.String" initial="12345"
    size="5" />
    <form-property name="city" type="java.lang.String"
    initial="somecity" />
    <form-property name="page" type="java.lang.Integer" />
    <form-property name="cities" type="java.util.ArrayList">
    </form-property>
    </form-bean>
    Thomas, Jul 24, 2003
    #1
    1. Advertising

  2. Thomas

    Thomas Guest

    > Probably. Look at the "initial" attribute of the <form-property> tag. I'm
    > using it to set initial values for single valued fields. I imagine you
    > could do it for a select list that allows multiple selections, so it should
    > also work for a String[] you intend to use as the options rather than the
    > selected values. At the point of constructing the Form bean, Struts has no
    > idea what you're going to do with it.


    Well, I also think that I have to do it with the initial attribute,
    but I just don't know the syntax. I couldn't find any example for this
    case.

    > The answer isn't so much "no" as "why would you want to"? The Form bean is
    > meant to carry the user's input back and forth from the browser to the
    > framework. The particular city the user has requested belongs in the Form
    > bean. The list of cities he can choose from does not. Especially since you
    > may have "city" on more than one form.


    Well the case, that I thought of is the initialization of comboboxes
    with a collection that is a property of the ActionForm using the
    "property" attribute of the options Tag in the Struts HTML Taglib.
    It's possible with custom ActionForms and I just wanted to know if I
    could do it with DynaActionForms too. If that makes sense or is good
    design is another question. I wouldn't do it like that in a real live
    application.

    Cheers,

    Thomas
    Thomas, Jul 25, 2003
    #2
    1. Advertising

  3. Thomas

    Joe Bloggs Guest

    I have the exact same issue as you do. My experience is that you cannot do
    that. Your value object can contain a List but you cannot define your List
    in your form bean inside struts-config.xml.

    Problem is then, how on earth do we create dynamic number of checkboxes
    (multiboxes) - so far I haven't found "recommended" answers. Perhaps you
    might want to send Ted Husted an email :)

    Anyway, my solution, albeit ugly, is to have a hidden field which register
    the checkboxes' values separated by commas. You would also need to add your
    custom javascript validations and the backend (Java) validation somewhere in
    the controller or inside some java classes.

    yaks!, some of you might think but unless someone can suggest a better
    solution, my workaround will have to stick for a while.

    cheers,

    "Thomas" <> wrote in message
    news:...
    > > Probably. Look at the "initial" attribute of the <form-property> tag.

    I'm
    > > using it to set initial values for single valued fields. I imagine you
    > > could do it for a select list that allows multiple selections, so it

    should
    > > also work for a String[] you intend to use as the options rather than

    the
    > > selected values. At the point of constructing the Form bean, Struts has

    no
    > > idea what you're going to do with it.

    >
    > Well, I also think that I have to do it with the initial attribute,
    > but I just don't know the syntax. I couldn't find any example for this
    > case.
    >
    > > The answer isn't so much "no" as "why would you want to"? The Form bean

    is
    > > meant to carry the user's input back and forth from the browser to the
    > > framework. The particular city the user has requested belongs in the

    Form
    > > bean. The list of cities he can choose from does not. Especially since

    you
    > > may have "city" on more than one form.

    >
    > Well the case, that I thought of is the initialization of comboboxes
    > with a collection that is a property of the ActionForm using the
    > "property" attribute of the options Tag in the Struts HTML Taglib.
    > It's possible with custom ActionForms and I just wanted to know if I
    > could do it with DynaActionForms too. If that makes sense or is good
    > design is another question. I wouldn't do it like that in a real live
    > application.
    >
    > Cheers,
    >
    > Thomas
    Joe Bloggs, Jul 26, 2003
    #3
  4. Thomas

    Joe Bloggs Guest

    Hi,
    This solution is only applicable for forms that uses Form beans but not
    DynaActionForm or DynaValidateForm.

    Secondly, it also works because there is only one property (the Select
    field) to pass back to the server upon a submit.

    In the case of multiple checkboxes where the number of checkboxes that will
    appear on a form is onlyn known during runtime, the solution wouldn't cut
    it.

    I suppose one can predefined an abitrary number of boolean/string properties
    (option1...optionN) in the value object to hold the checkboxes value, and
    check retrieves these properties in java classes.

    cheers,


    "Sudsy" <> wrote in message
    news:...
    > Joe Bloggs wrote:
    > > I have the exact same issue as you do. My experience is that you cannot

    do
    > > that. Your value object can contain a List but you cannot define your

    List
    > > in your form bean inside struts-config.xml.
    > >
    > > Problem is then, how on earth do we create dynamic number of checkboxes
    > > (multiboxes) - so far I haven't found "recommended" answers. Perhaps you
    > > might want to send Ted Husted an email :)

    >
    > You might find some helpful information here:
    > http://www.selbyinc.com/technology/struts-advanced.html
    > While it specifically addresses select options, the same mechanism
    > can be used for radio button groups.
    >
    Joe Bloggs, Jul 27, 2003
    #4
  5. Thomas

    Sudsy Guest

    Joe Bloggs wrote:
    > Hi,
    > This solution is only applicable for forms that uses Form beans but not
    > DynaActionForm or DynaValidateForm.
    >
    > Secondly, it also works because there is only one property (the Select
    > field) to pass back to the server upon a submit.
    >
    > In the case of multiple checkboxes where the number of checkboxes that will
    > appear on a form is onlyn known during runtime, the solution wouldn't cut
    > it.
    >
    > I suppose one can predefined an abitrary number of boolean/string properties
    > (option1...optionN) in the value object to hold the checkboxes value, and
    > check retrieves these properties in java classes.


    You're half right: it doesn't work with DynaActionForm and classes
    which extend it. It works just peachy with checkboxes, radio boxes,
    etc. I'm working on adding some additional examples to the documentation
    but I merely extended the mechanism to include checkboxes. It took me
    all of a half-hour to code and test.
    Sudsy, Jul 27, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sudsy
    Replies:
    0
    Views:
    2,329
    Sudsy
    Jul 16, 2003
  2. javaguy44
    Replies:
    1
    Views:
    4,595
    javaguy44
    Apr 22, 2004
  3. gk
    Replies:
    9
    Views:
    6,108
  4. gk
    Replies:
    1
    Views:
    459
  5. untung
    Replies:
    0
    Views:
    446
    untung
    Oct 7, 2009
Loading...

Share This Page