struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Kevin.Bed...@sunlife.com
Subject Re: Help with Struts + DB Access
Date Thu, 03 Oct 2002 13:55:03 GMT



Wendy -

One approach I've used is:

      -  Create (as it looks like you have) your form bean (that you refer
to as the ActionForm extention)
          so you can set/get the properties using a collection. (eg,
setDropdownValues(Collection values), etc.)

      - In your Action class, have it call what I'll refer to as a 'facade'
class that manages retrieving the values.

      - The 'facade' class should provide a simple method such as
facade.getValues() that returns the Collection as
         you need to set it in the form bean.

      - The Action class code would look like:
formBean.setDropdownValues( facade.getValues() );
         This simplifies maintenance of the Action class - not matter what
for the values take,
         the Action class is unaffected.

      -  Then in your facade class, have it coordinate the gathering of the
data. It's requirements may be:

            - perform a getAttribute against the ServletContext to see if
the values are stored there already
            - If the values aren't there, use the DAO to retrieve them
            - Potentially have it check time of day and periodically
refresh the values stored in
              the servlet context

      - In addition, have the 'facade' catch any database access exceptions
and 'rethrow' them as some sort of
        Application exception (eg, DropDownException would likely be an
awful name, but you get the idea). This
        ensures that the Action class (the Controller in the MVC
architecture of Struts) can manage the exception
        and redirect processing appropriately. Since the 'facade' hides the
database access, the Action class
                  shouldn't be catching JDBC exceptions.


This allows you to isolate any changes in business logic or data access
down in either the facade or the DAO. The facade simply presents the data
in the format that the form bean wants it. I've identified using a
Collection to pass values, but it could just as easily be a HashMap or
whatever you might need.

Best of luck -

Kevin

-------------------------
Kevin Bedell
author, "Struts Kickstart" - SAMS Publishing










<opensourceaz@cox.net> on 10/02/2002 07:56:54 PM

Please respond to "Struts Users Mailing List"
       <struts-user@jakarta.apache.org>

To:    struts-user@jakarta.apache.org
cc:     (bcc: Kevin Bedell/Systems/USHO/SunLife)
Subject:    Help with Struts + DB Access



I'm trying to put a select list on my page.  The contents of the list need
to come from the database.  I have a DAO layer, so I'm doing okay isolating
the database access, but I'm still not quite sure when to retrieve the
values and where to put them.

In most of the examples I look at, and the struts-example webapp is no
different, when you get down to the details, it'll say something like:

"In real life, these would be loaded from a database"

Well, that's in the 'subscription.jsp' page, and I *know* that "in real
life" it wouldn't be loaded from the database *there*!

I'm reading the docs for the <html:collection> and there are so many
combinations of attributes that I'm still a bit confused.  So far I've
worked out that I can:

1. Put a Collection in some scope, as the example does.

2. Put a method in my ActionForm extension that returns a Collection

There are probably more.  Can someone give me some guidance on which method
of supplying choices for a select list might work best for me?

I'd like to get them into application scope if possible-- once they're
loaded they're good "forever" and can be shared among sessions.  (They
change infrequently, and can wait for a restart on maintenance weekend to
load the new values.)

Thanks!

--
Wendy in Chandler, AZ



--
To unsubscribe, e-mail:   <
mailto:struts-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <
mailto:struts-user-help@jakarta.apache.org>







---------------------------------------------------------------------------
This e-mail message (including attachments, if any) is intended for the use
of the individual or entity to which it is addressed and may contain
information that is privileged, proprietary , confidential and exempt from
disclosure.  If you are not the intended recipient, you are notified that
any dissemination, distribution or copying of this communication is
strictly prohibited.  If you have received this communication in error,
please notify the sender and erase this e-mail message immediately.
---------------------------------------------------------------------------



--
To unsubscribe, e-mail:   <mailto:struts-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:struts-user-help@jakarta.apache.org>


Mime
View raw message