This is something which is kind of awkward in Struts 1.2. I think
we've got a pretty decent approach in Struts 1.3, but I guess we'll
have to wait until more people start checking that out to find out.
Basically, the approach is to prepare a FormBean instance in an
action and populate its values with those you want to see in the
form. If the FormBean is in the right place, then when the page
draws, the form tag and its child elements will be populated based on
the FormBean instance.
The "right place" is dependent on the "name" and "scope" of the
action in the <html:form>'s action attribute. Specifically, the bean
should be in either request or session scope (accordingly) under an
attribute name which matches the Form Bean's name.
First, how it should be done in Struts 1.3, because it's more
straightforward; below that, the gist of how to do it in Struts 1.2.
Before midway through the Struts 1.2 development line, there was even
less API support, so I won't get into the details -- hopefully there
aren't a lot of people still using Struts 1.1, but if you needed, you
could probably work out the gist of it from what comes below.
So, in Struts 1.3, there is a generic command, "CopyFormToContext".
If you configure this command simply with the submission destination
(action path) of the form you want to prefill, Struts will create a
form bean (or reuse an existing one if possible) and put it where it
needs to be for the form to use it as a prefill source. You then
need to follow that command with another which would be customized to
your needs and which would perform the prefilling logic. You can
configure CopyFormToContext so that besides putting the form where
the <html:form> tag needs it, it can also put a reference under any
arbitrary Context key, so that your population command can find it
easily.
I won't get into the details of configuring chains and commands here,
except to note that you can hook a command to any action or forward,
so that this behavior is executed "en route" to your view. I'll be
happy to answer follow-on questions as necessary, but first, a bit
more about how this works in Struts 1.2 (which is what the above does
behind the scenes.)
The problem is that in Struts 1.2, you need to hard-code more than I
really like about the config files into your actions, and the API is
not entirely straightforward. There are methods in ModuleUtils and
RequestUtils to get a hold of the relevant objects and ultimately get
a form instance, but the responsibility for putting that form bean
back into the correct scope under the correct key is still not
centralized. (It's actually not universally centralized in Struts
1.3 either, but still, there's a enough support for this specific
task that it's not such a big deal.)
I recommend using the API for getting a form bean instance because
it's pretty cumbersome to instantiate dynaforms yourself. The steps
are: get the ModuleConfig (static, only requires an instance of
HttpServletRequest), then from the module config, get the
ActionMapping (aka ActionConfig) (just takes the action path of the
submit destination of the form you want to prepare), and then get the
Form instance using the static RequestUtils.createActionForm(request,
mapping, moduleConfig, servlet). Now you have the bean you wish to
populate, After you populate it, there is no specific Struts API
method, but you can use the ActionMapping (the one you looked up, not
the one passed in to your preparation action) to find the correct
scope and name.
ModuleUtils.getModuleConfig(request)
http://struts.apache.org/api/org/apache/struts/util/ModuleUtils.html#getModuleConfig(javax.servlet.http.HttpServletRequest)
ModuleConfig.findActionConfig
http://struts.apache.org/api/org/apache/struts/config/ModuleConfig.html#findActionConfig(java.lang.String)
RequestUtils.createActionForm(request, mapping, moduleConfig, servlet)
http://struts.apache.org/api/org/apache/struts/util/RequestUtils.html#createActionForm(javax.servlet.http.HttpServletRequest,%20org.apache.struts.action.ActionMapping,%20org.apache.struts.config.ModuleConfig,%20org.apache.struts.action.ActionServlet)
I hope this helps. The relative difficulty of achieving this
compared to the regularity with which it is needed has been bothering
me for years, and I hope that people will agree that it is getting
easier in newer versions of Struts.
Joe
At 2:44 PM +0200 8/29/05, Karin Schellner wrote:
Hi,
I just started implementing a webapplication using Struts and could
not find a good solution for the following (- not so uncommon as I
would guess -) problem:
Each authenticated user has a userprofile with personal data which
he/she can edit via a "PersdataForm". The initial values for this
form should be retrieved from the database, i.e. there is a BO
somewhere containing these data. There is a "PersdataAction" writing
the changed values back to the BO.
How would I best initiate the form? - the FormBeans "reset" method
seems not to be the right place because it is called too often -
should I use another Action? but how would the configuration look
like in such a scenario?
Thanks in advance for your ideas on this problem!
karin.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
--
Joe Germuska
[EMAIL PROTECTED]
http://blog.germuska.com
"Narrow minds are weapons made for mass destruction" -The Ex
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]