Struts forms design question

Discussion in 'Java' started by Joerg Gippert, Jul 25, 2003.

  1. Hi everyone!

    I´m using Struts 1.1 and I encountered a problem, which I either didn´t find
    a solution for in the Struts documentation or there actually is no solution
    for this issue. Just wanted to know, if my "work around" isn´t too messy or
    maybe someone has a cleaner solution for this. Ok, here´s the problem:
    In my webapp I´d like the user to edit his own data record. Like changing
    address, email etc.. As I understand it, Actionforms are designed for
    capturing input from HTML forms only but not to present data in HTML forms.
    And when I had a look at the html:text tag, I noticed that it doesn´t
    provide a value="" field for setting the value of the input field. One can
    set initial values for the imput elemts in the config files but how does
    that work at runtime? E.g. I pass a bean within the session to the JSP form
    and want to set values of input fields from this bean. Actually, that´s my
    workaround. In my JSP form I did
    <bean:define id="item" name="ITEM" scope="session" />
    <input type="text" name="itemname" value="<c:eek:ut
    value="${item.itemname}"/>">
    to set the value. Doesn´t look really clean to me but it works ;)

    Does anyone have a nicer solution to populate a form at runtime? Maybe I
    only missed something :)

    Any hint is appreciated!

    Thanks,

    Joerg
     
    Joerg Gippert, Jul 25, 2003
    #1
    1. Advertising

  2. Joerg Gippert

    Sudsy Guest

    Joerg Gippert wrote:
    > Hi everyone!
    >
    > I´m using Struts 1.1 and I encountered a problem, which I either didn´t find
    > a solution for in the Struts documentation or there actually is no solution
    > for this issue. Just wanted to know, if my "work around" isn´t too messy or
    > maybe someone has a cleaner solution for this. Ok, here´s the problem:
    > In my webapp I´d like the user to edit his own data record. Like changing
    > address, email etc.. As I understand it, Actionforms are designed for
    > capturing input from HTML forms only but not to present data in HTML forms.
    > And when I had a look at the html:text tag, I noticed that it doesn´t
    > provide a value="" field for setting the value of the input field. One can
    > set initial values for the imput elemts in the config files but how does
    > that work at runtime? E.g. I pass a bean within the session to the JSP form
    > and want to set values of input fields from this bean. Actually, that´s my
    > workaround. In my JSP form I did
    > <bean:define id="item" name="ITEM" scope="session" />
    > <input type="text" name="itemname" value="<c:eek:ut
    > value="${item.itemname}"/>">


    Use this:

    <html:text name="ITEM" property="itemname"/>

    This will attempt to populate the field by invoking getItemname on
    the bean named ITEM, similar your example code. NOTE: The accessor
    and mutator method names have to obey the naming conventions for
    beans, i.e. getItemname as opposed to just itemname.
    It will also expect there to be a corresponding attribute in the
    ActionForm as it will try to invoke setItemname on that object.
     
    Sudsy, Jul 25, 2003
    #2
    1. Advertising

  3. Joerg Gippert

    Wendy S Guest

    "Joerg Gippert" <> wrote:
    > I´m using Struts 1.1 and I encountered a problem, which I either didn´t

    find
    > a solution for in the Struts documentation or there actually is no

    solution
    > for this issue. Just wanted to know, if my "work around" isn´t too messy

    or
    > maybe someone has a cleaner solution for this. Ok, here´s the problem:
    > In my webapp I´d like the user to edit his own data record. Like changing
    > address, email etc.. As I understand it, Actionforms are designed for
    > capturing input from HTML forms only but not to present data in HTML

    forms.

    Not true. One of the things Struts does is pre-populate the HTML form with
    the user's selections if the form fails validation, or if you load the
    values from a database before displaying the form, etc.

    > And when I had a look at the html:text tag, I noticed that it doesn´t
    > provide a value="" field for setting the value of the input field.\


    That's because the framework will automatically call the corresponding
    'getter' of the Form bean and use what it gets as the "value" of the HTML
    input tag.

    > One can set initial values for the imput elemts in the config files but

    how does
    > that work at runtime? E.g. I pass a bean within the session to the JSP

    form
    > and want to set values of input fields from this bean. Actually, that´s my
    > workaround. In my JSP form I did
    > <bean:define id="item" name="ITEM" scope="session" />
    > <input type="text" name="itemname" value="<c:eek:ut
    > value="${item.itemname}"/>">
    > to set the value. Doesn´t look really clean to me but it works ;)
    > Does anyone have a nicer solution to populate a form at runtime?


    All you need is: <html:text property="itemname" />

    This will call the getItemname() method on the form bean. It's your
    responsibility to get the proper values into the form bean before forwarding
    to the JSP.

    Look at the struts-example webapp. You'll find a line that looks like this:
    BeanUtils.copyProperties( modelBean, formBean );

    That will copy the matching properties back and forth between the info you
    have in your ITEM bean, and the form bean so that Struts can prepopulate the
    form.

    As you noted, you can also use the "initial" attribute in struts-config.xml
    to provide default values.

    Since you're already using JSTL, consider switching to the Struts-EL tags
    found in the 'contrib' directory. Then you can use expressions with the
    Struts tags when needed.

    <html-el:text property="itemname" />

    --
    Wendy in Chandler, AZ
     
    Wendy S, Jul 25, 2003
    #3
  4. Thanks! ->Struts forms design question

    Thanks to Wendy and Sudsy for the hints!
    Took me a while to understand what the webexample application comming along
    with Struts is doing in detail but after some playing around, I think I got
    it ;) Now my JSP code looks tidy again :)

    Thanks again.
    Regards,
    Joerg
     
    Joerg Gippert, Jul 26, 2003
    #4
    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. Katie Wright
    Replies:
    8
    Views:
    4,263
    Andrea Desole
    Jan 7, 2005
  2. Jason Us
    Replies:
    0
    Views:
    995
    Jason Us
    Oct 4, 2003
  3. jc1771
    Replies:
    0
    Views:
    409
    jc1771
    Dec 28, 2003
  4. sin
    Replies:
    3
    Views:
    729
    Sudsy
    Feb 22, 2004
  5. Eric
    Replies:
    2
    Views:
    561
Loading...

Share This Page