struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Rick Reumann" <>
Subject Re: how populate form from database ?
Date Fri, 21 Sep 2007 21:08:44 GMT
On 9/21/07, A. Lotfi <> wrote:
> Hi,
>   I need to populate a form with data from database, is there any example.
>   thank you your help is appreciated.

Probably are a ton of them. (Here's one for Struts1.x using iBATIS ).

In Struts1, I basically do this...

1) Struts action method calls a businessDelegate/serviceClass to get your
object. (Just something to abstract a way the dao call to the database - but
for rapid development you could even skip this class if you want.)

2) Delegate calls your DAO (data access object) to get back your object from
the database.

3) You then populate your ActionForm from this object. There are several
ways to do this. First off it's a good idea to make your ActionForm property
names match up with the value object names you returned in your object in
step 2. (ie an EmployeeActionForm has property firstName, and your Employee
value object has property name firstName.)  Typicall then in your Action
class you can use BeanUtils (commons package) to do:

Employee employee  = employeeDelegate.getEmployee(employeeID);
BeanUtils.copyProperties( employeeActionForm, employee );

You're done. (There are a few caveats to using BeanUtils which can be quite
annoying but you can worry about them later:)

A side note, I actually have changed where I populate my ActionForm and
return the value object. Instead of doing the above in the Action class, I
add two methods to my ActionForm...

populateForm(Employee emp ) {
    //takes care of some code like above

populateEmployee(Employee emp) {
    //populates an Employee object from the form data

I just found it cleaner to do there, especially when some odd cases come up
that require some special handling, such as when maybe you can't easily use
BeanUtils for everything.)


  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message