Lately I've been using the DependencyFilter in Java Web Parts for such
things...

http://javawebparts.sourceforge.net

...or more specifically:...

http://javawebparts.sourceforge.net/javadocs/javawebparts/filter/DependencyFilter.html

(Admittedly I need to clean up those docs a bit, but you'll get the gist
of it).

What I do is I have a helper class that calls through to my business
facade and gets whatever data I need and puts it in some DTO, which is
then "injected" into the appropriate scope (this is done by the
DependencyFilter via config file).  My Action can then grab the DTO and
populate the ActionForm.  This eliminates the need for setup Actions and
such.

As an example, let's say I have an ActionForm with fields:

String firstName;
String lastName;

...and likewise I have a PersonDTO:

public class PersonDTO {
  private String firstName;
  private String lastName;
  public void setFirstName(String fn) { firstName=fn; }
  public String getFirstName() { return firstName; }
  public void setLastName(String ln) { lastName=ln; }
  public String getLastName() { return lastName; }
}

The DependencyFilter has its own config file, and here's a simple example:

<dependencies>
  <dependency scope="request" name="MyProfile"
createForPaths="/editprofile.do">
    <className>my.app.PersonDTO</className>
    <initClass name="ProfileHelper" mathod="getProfile" />
  </dependency>
</dependencies>

Note that the createForPaths attribute is a comma-separated list which
supports wildcards, so you can use the same dependency in multiple places
with one config entry.

Finally, I have a class named ProfileHelper, something like so:

public class ProfileHelper {
  public void getProfile(PersonDTO p) {
    // Code to populate firstName and lastName in p from database
  }
}

So, what happens is:

(1) When a request comes in for /editProfile.do, the filter kicks in.
(2) The filter instantiates an instance of PersonDTO, as well as an
instance of ProfileHelpers.
(3) The PersonDTO is passed to the getProfile() method of the
ProfileHelpers class.
(4) getProfile() does whatever it needs to do to get the firstName and
lastName for the current user from the database and populate those fields
in the DTO.
(5) The PersonDTO instance, being specified as having request scope, is
now "injected" into the current request object as an attribute under the
key "MyProfile".
(6) Now, in my Action, I can get at the PersonDTO by calling:

  PersonDTO p = (PersonDTO)DependencyFilter.getDependency("MyProfile",
request);

Note that you don't need to specify the scope the bean is in... if you
change it later, the same call will still get it.

Usually there is some logic there in the Action to determine whether I
should do this or not (i.e., is this the first time /editProfile.do has
been called, or am I dealing with perhaps a form submission, in which case
I want to use the ActionForm as-is?).  But, if applicable, I just populate
the ActionForm from the PersonDTO at this point.

Note that you can do even better things if you put the ActionForm in
session scope because then the filter can in essence take the place of
Struts and create it, populate it, and stick it in session, and then you
can cut out a lot of this because you (and the filter) deal with the
ActionForm directly.  What you have is a nice, clean way to prepopulate an
ActionForm without having to have a bunch of prepopulation mappings and
Actions.

The DependencyFilter has a ton of options for initializing objects, as
well as a facility to give them a lifetime.  So, if you want to for
instance have an object in a users' session that is updating from the
database every 10 minutes, you can set a maxAge attribute on the
<dependency> element, and then with every request the age of the object is
checked.  If it exceeds maxAge, it is initialized again.  This can come in
*very* handy.

I've been meaning to put together a cookbook recipe for doing things like
this, but I haven't gotten around to it yet.

-- 
Frank W. Zammetti
Founder and Chief Software Architect
Omnytex Technologies
http://www.omnytex.com
AIM: fzammetti
Yahoo: fzammetti
MSN: [EMAIL PROTECTED]

On Tue, November 29, 2005 2:20 pm, Koen Jans said:
> Suppose for instance you want to prepopulate a form where users
> can edit their profile;
>
> the way i do it is:
> I have an action PrepareEditProfileAction that
> a) gets the data from storage
> b) creates a editProfileFormBean and puts the data in it
> c) saves this editProfileFormBean in request-scope
> d) forwards to the edit profile jsp page, which then
>    displays the data from the editProfileFormBean in request scope..
>
> so your struts-config.xml looks like:
>
> <!-- prepopulate -->
> <action path="/actions/prepareeditprofile"
>   type="actions.PrepareEditProfileAction">
> <forward name="success" path="/editprofileform.jsp" />
> </action>
>
> <action path="/actions/editprofile"
>   type="actions.EditProfileAction"
>   name="editProfileFormBean"
>   scope="request"
>   validate="true"
>   input="/actions/prepareeditprofile.do">
> <forward name="success" path="/success.jsp" />
> </action>
>
>
> Is this a common reproach?
> I've seen people using the "input" attribute point to an prepopulate
> action?
>
> Thanks for your time,
> Koen
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to