Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Struts forms design question

Reply
Thread Tools

Struts forms design question

 
 
Joerg Gippert
Guest
Posts: n/a
 
      07-25-2003
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="<cut
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


 
Reply With Quote
 
 
 
 
Sudsy
Guest
Posts: n/a
 
      07-25-2003
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="<cut
> 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.

 
Reply With Quote
 
 
 
 
Wendy S
Guest
Posts: n/a
 
      07-25-2003
"Joerg Gippert" <(E-Mail Removed)> 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="<cut
> 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


 
Reply With Quote
 
Joerg Gippert
Guest
Posts: n/a
 
      07-26-2003
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
forms authentication -- expired forms cookie vs. not provided forms cookie Eric ASP .Net Security 2 01-27-2006 10:09 PM
Is it safe to point to Internet for locating struts DTD files in struts TLDs and XML files? Katie Wright Java 8 01-07-2005 03:37 PM
[Struts]output javascript in struts perform() sin Java 3 02-22-2004 08:27 AM
Integrate a Struts app with a non-struts app jc1771 Java 0 12-28-2003 06:36 PM
Struts Installation - missing struts.tld??? Jason Us Java 0 10-03-2003 11:36 PM



Advertisments