I can't imagine you having this problem. I do this just fine. Here's
what I do:
<struts-config>
<form-beans>
<form-bean dynamic="true" name="userForm"
type="org.apache.struts.action.DynaActionForm"
>
<form-property name="alias" type="java.lang.String"/>
<form-property name="password" type="java.lang.String"/>
...
</form-bean>
</form-beans>
...
<action-mappings>
<action path="/editUser"
name="userForm"
scope="request"
type="package.containing.my.EditUserAction">
<forward name="success" path="/editUser.jsp"/>
</action>
...
</action-mappings>
</struts-config>
Basically .. well, here's a really straightforward code-example inspired
by EditUserAction:
public class EditUserAction extends Action
{
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws
java.lang.Exception
{
...
DynaActionForm dynaForm = (DynaActionForm) form;
dynaForm.set("alias", new String("alias"));
dynaForm.set("password", new String("password"));
...
return mapping.findForward("success");
}
}
If that doesn't work for you ... I'd say something is broke :-) It
works fine for me using the 1.1b2 binary (TC 4.0.4). I know it's kind
of stripped down, but it should provide a good example.
additional comments below ...
Peterkofsky, Don wrote:
>We are having trouble understanding how to accomplish what is a very common
>web application use case, using Struts.
>
>The problem is around pre-populating an "Update/Edit" JSP form, from data
>retrieved and stored in a model bean. We are using
>org.apache.struts.validator.DynaValidatorForm for the form-bean. The
>specific use case is:
>
>1. Select an item from a list displayed on a JSP.
>2. JSP calls a Struts action, which retrieves a data object based on a key
>in the list, and creates a populated model bean. Copy the model bean data
>to a form-bean, for use in a Struts JSP.
>3. The Struts action forwards to the Update JSP, based on the
>action-mappings.
>4. The Update JSP should display pre-populated values based on the model
>bean data in the form-bean, using the Struts <html:text.../> tags. If the
>form is submitted and then re-displayed due to a validation error, the
>pre-populated values should come from the form bean, which would now reflect
>the form/request values.
>
>We have tried a number of approaches, none of which have worked.
>
>In #1 above, we have set the action for the List page to use the same
>form-bean as the Update action, and in #2 we have used the BeanUtils methods
>in the action to copy the data from the model bean to the form bean that was
>created. We have verified that the data is properly copied to the form
>bean. We have set the form-bean into the request, using the form name from
>the struts-config. However, when the action forwards control to the Update
>JSP, the form-bean that we populated doesn't appear to be in the request any
>longer, so the bean isn't available to the JSP.
>
You have set the form-bean into the request? No ... this is not your
responsibility :-) Nor should you take it upon yourself to "help Struts
out". You should *expect* the form-bean to be there. If it is not, let
it NPE and fail fast -- then track it down and submit a bug ;-)
>We have also tried forwarding to another action from #2, instead of to a
>JSP, thinking that this would allow the form-bean to remain in the request.
>This also appears to remove the form-bean from the request, when control is
>ultimately forwarded to the JSP.
>
If your actions specify a scope (scope="request" for example) you should
be aware what you are stating by including that attribute. You're
telling Struts: "Hey, put my form-bean in this scope." So, if both the
actions don't specify the same scope, ... you won't be looking at the
same one in both of them :-) ... and that could manifest itself in this
exact way :-)
>This seems to be one of the most basic use cases of most applications, but
>is not described in any Struts documentation I've seen. What we want to do
>is:
>
>JSP item link ->
>action to retrieve model data (and populate form-bean) ->
>JSP update form (displaying form-bean from above, populated with model data)
>
Yeah, click a link to execute an action that populates a form-bean and
forwards to a JSP. Precisely. You *can* find many examples of this :-)
They are out there. I was just telling a fellow about the one in
Chapter 5 (page 20, if I recall) of Chuck Cavaness' struts book review
on theserverside.com. He lays it out quite plain.
>Any advice or solutions or best practices on this would be most appreciated.
>We have spent a couple of days on this, researching the jakarta site, and
>other resources and mail lists, without finding a definitive answer to this.
>
I don't know that I can claim best-practice ... but, there are only so
many ways you can populate a form-bean ;-) Assuming you do have a DTO
from your model that has the same property names as the action-form, you
should be able to use the BeanUtils (different types) or PropertyUtils
(same types). I caught a clip from Craig earlier saying you should use
PropertyUtils when both beans are typed the same (all properties' types
match up -- cost on this bean is a double and same on the other bean,
for example), and BeanUtils otherwise. PropertyUtils is faster, but
does not introspect types or do conversions.
>
>
>Thanks.
>
--
Eddie Bush
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>