Struts - how to prepopulate a Form Bean with data

Discussion in 'Java' started by matthewmacchia@hotmail.com, Jan 20, 2005.

  1. Guest

    Hello,
    So here is my goal.
    I have a form that I want to prepopulate with data. For example, Let's
    say I have a loginForm and I want it to prepopulate with information
    about what page it is so that my nav can build itself.
    I want the form to preopolate with information about that.
    kind of like this:

    <form-bean
    name="logonForm"
    type="com.whatever.LogonForm">
    <form-property
    name="navigation"
    type="java.lang.String" value="loginpage" />
    </form-bean>

    It sounds dumb but I need it constructed with the data prepopulating.
    (this is the definition for the dynactionform,but I need an element
    like form-property)...

    any ideas?
    thanks
    matt
     
    , Jan 20, 2005
    #1
    1. Advertising

  2. Wendy S Guest

    <> wrote

    > It sounds dumb but I need it constructed with the data prepopulating.
    > (this is the definition for the dynactionform,but I need an element
    > like form-property)...


    It doesn't sound dumb at all... it's why <form-property> has an 'initial'
    attribute. :) Assuming your loginForm _is_ a dynamic form, that is. Or are
    you trying to prepopulate a "regular" ActionForm?

    The DTD is well commented-- it's worth a read even if you don't understand
    the syntax:
    http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd

    For <form-property> it says:
    initial String representation of the initial value for this
    property.
    If not specified, primitives will be initialized to
    zero and
    objects initialized to the zero-argument instantiation
    of that
    object class. For example, Strings will be initialized
    to ""

    --
    Wendy
     
    Wendy S, Jan 20, 2005
    #2
    1. Advertising

  3. Guest

    Actually, yes.
    It's with plain ol' Action Forms.
    We need to keep our forms very stable so we can have a clear idea what
    inputs are needed for each action.

    It is my understanding that DynaActionForms would give us the ability
    to mix and match forms more easily. Is that correct?

    thanks.
    -matt


    Wendy S wrote:
    > <> wrote
    >
    > > It sounds dumb but I need it constructed with the data

    prepopulating.
    > > (this is the definition for the dynactionform,but I need an element
    > > like form-property)...

    >
    > It doesn't sound dumb at all... it's why <form-property> has an

    'initial'
    > attribute. :) Assuming your loginForm _is_ a dynamic form, that is.

    Or are
    > you trying to prepopulate a "regular" ActionForm?
    >
    > The DTD is well commented-- it's worth a read even if you don't

    understand
    > the syntax:
    > http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd
    >
    > For <form-property> it says:
    > initial String representation of the initial value for this
    > property.
    > If not specified, primitives will be initialized

    to
    > zero and
    > objects initialized to the zero-argument

    instantiation
    > of that
    > object class. For example, Strings will be

    initialized
    > to ""
    >
    > --
    > Wendy
     
    , Jan 21, 2005
    #3
  4. Wendy S Guest

    <> wrote:
    > Actually, yes.
    > It's with plain ol' Action Forms.
    > We need to keep our forms very stable so we can have a clear idea what
    > inputs are needed for each action.
    > It is my understanding that DynaActionForms would give us the ability
    > to mix and match forms more easily. Is that correct?


    No, dynamic forms just get you out of writing the get/set methods. (I guess
    you could argue that ActionForms contribute to more stable code... if you
    write getName() and the name property doesn't exist, it'll fail at compile
    time, where with a dynamic form, you wouldn't know that
    form.getString("name") was not going to work.

    Pre-populate your ActionForms in the Action before you forward to the view
    [usually JSP].

    Here's one way:
    http://struts.apache.org/faqs/newbie.html#prepopulate

    Another is to use some variant of DispatchAction that will 'know' what it
    needs to do based on the presence of various things either in the request or
    as part of the URL.

    --
    Wendy
     
    Wendy S, Jan 21, 2005
    #4
  5. nooobody Guest

    wrote:
    > Hello,
    > So here is my goal.
    > I have a form that I want to prepopulate with data. For example,

    Let's
    > say I have a loginForm and I want it to prepopulate with information
    > about what page it is so that my nav can build itself.
    > I want the form to preopolate with information about that.
    > kind of like this:
    >
    > <form-bean
    > name="logonForm"
    > type="com.whatever.LogonForm">
    > <form-property
    > name="navigation"
    > type="java.lang.String" value="loginpage" />
    > </form-bean>
    >
    > It sounds dumb but I need it constructed with the data

    prepopulating.
    > (this is the definition for the dynactionform,but I need an element
    > like form-property)...
    >
    > any ideas?
    > thanks
    > matt



    If you building a nav menu based on what page you are on, another
    approach, but only if you are using tiles, is to set a property for
    each page in the tile definition, retrieve it in one of the page's
    jsps and build the menu accordingly. A custom tag might be handy for
    building the menu.
     
    nooobody, Jan 21, 2005
    #5
  6. Guest

    Hmmmm...
    I'm actually using tiles but have never tried using tiles definitions.
    Could you tell me more about it?

    That seems perfect.
    Specifically, I am looking for a way to externalize the navigation
    information, I have currently built it so that each action tells the
    page where it. But, I would prefer to seperate that logic so the
    actions aren't so tied to the view. That was why putting it into the
    struts config and then prepopulating a form would work great. But it
    doesn't seem possible.

    Thanks
    -matt

    nooobody wrote:
    > wrote:
    > > Hello,
    > > So here is my goal.
    > > I have a form that I want to prepopulate with data. For example,

    > Let's
    > > say I have a loginForm and I want it to prepopulate with

    information
    > > about what page it is so that my nav can build itself.
    > > I want the form to preopolate with information about that.
    > > kind of like this:
    > >
    > > <form-bean
    > > name="logonForm"
    > > type="com.whatever.LogonForm">
    > > <form-property
    > > name="navigation"
    > > type="java.lang.String" value="loginpage" />
    > > </form-bean>
    > >
    > > It sounds dumb but I need it constructed with the data

    > prepopulating.
    > > (this is the definition for the dynactionform,but I need an element
    > > like form-property)...
    > >
    > > any ideas?
    > > thanks
    > > matt

    >
    >
    > If you building a nav menu based on what page you are on, another
    > approach, but only if you are using tiles, is to set a property for
    > each page in the tile definition, retrieve it in one of the page's
    > jsps and build the menu accordingly. A custom tag might be handy

    for
    > building the menu.
     
    , Jan 21, 2005
    #6
  7. nooobody Guest

    wrote:
    > Hmmmm...
    > I'm actually using tiles but have never tried using tiles

    definitions.
    > Could you tell me more about it?
    >
    > That seems perfect.
    > Specifically, I am looking for a way to externalize the navigation
    > information, I have currently built it so that each action tells the
    > page where it. But, I would prefer to seperate that logic so the
    > actions aren't so tied to the view. That was why putting it into the
    > struts config and then prepopulating a form would work great. But it
    > doesn't seem possible.


    The technique we use makes it unnecessary to set any values in
    form-beans or actions.

    what I meant by tiles definitions is to have an entry in your
    struts-config for the tiles plugin something like this:

    <plug-in className="org.apache.struts.tiles.TilesPlugin">
    <set-property property="definitions-config"
    value="/WEB-INF/tiles-defs.xml"/>
    </plug-in>

    then in the tile-defs.xml (or whatever you choose to name it) you have
    an entry for each page:

    <definition name="logonpage" extends="main.layout">
    <put name="title" value="This is the logon page" />
    <put name="body" value="/jsp/body/logon.jsp" />
    <put name="nav" value="/jsp/nav.jsp"/>
    <put name="page" value="page1"/>
    </definition>

    Of course there would be other page definitions and a definition for
    main.layout in the definitions-config file

    Part of the trick is how to get at the 'page' value from your the
    nav.jsp. I don't recall exactly how we did it - I know you need to get
    it into page scope - we just started using this approach on this
    project - but I can post it when I have a chance to look at it again,
    maybe sunday or Monday, unless someone else is familiar with this
    approach and can post the rest of the details.

    Tim B
     
    nooobody, Jan 22, 2005
    #7
  8. nooobody Guest


    >
    > Part of the trick is how to get at the 'page' value from your the
    > nav.jsp. I don't recall exactly how we did it - I know you need to

    get
    > it into page scope - we just started using this approach on this
    > project - but I can post it when I have a chance to look at it

    again,
    > maybe sunday or Monday, unless someone else is familiar with this
    > approach and can post the rest of the details.
    >
    > Tim B


    here it is.

    in the jsp used for the base layout:
    <tiles:useAttribute name="currentPage" scope="request" />

    then in your jsp or custom struts tag, just get the "currentPage"
    attribute that will now be found in the request and build your menu
    acordingly
     
    nooobody, Jan 24, 2005
    #8
    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. Ariel Jakobovits
    Replies:
    3
    Views:
    605
    Ariel Jakobovits
    Sep 26, 2003
  2. Daniel Wang

    struts form bean

    Daniel Wang, Jun 9, 2004, in forum: Java
    Replies:
    1
    Views:
    729
    Tim Slattery
    Jun 9, 2004
  3. Thomas P. Fuller
    Replies:
    1
    Views:
    4,576
    bitbucket43
    Aug 17, 2004
  4. jill
    Replies:
    5
    Views:
    15,710
  5. Replies:
    1
    Views:
    3,918
    Tim B
    Jan 7, 2006
Loading...

Share This Page