Struts forms design question

J

Joerg Gippert

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
 
S

Sudsy

Joerg said:
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.
 
W

Wendy S

Joerg Gippert said:
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" />
 
J

Joerg Gippert

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top