At 1:36 PM +0100 6/28/04, Research labs wrote:
//***Please note that I am using DispatchAction not
Action***

Please tell me how to populate fields in a form with
data retrieved from a database table.
Please note that I am particular about how to do this
using Struts tags etc.

Are you saying that you want to show a user a form with data pre-filled based on the database data?


The way to do this is to have an instance of a form bean in the action before you forward to the view, populate its values, and put it in the request or session under the attribute name where the html:form tag looks for it.

In a formal sense, Struts doesn't make this very easy, because the API for requesting an instance of a form bean is not quite engineered for it. However, if you just want it to work, you could simply instantiate your own form bean using a constructor and then put it into scope. (More on the "right way" to do this--one which also supports DynaForms-- in a moment.) The attribute name is the same name you give the form bean in the action mapping and form-bean configurations in the struts-config.xml file, and of course, you should use the same scope that you use in your action-mapping. That is, for something like this:

<action path="/UpdatePublicationDetails"
        type="....webui.PreferencesController"
        parameter="updatePublicationDetails"
        name="updatePublicationDetailsForm"
        scope="request"
        validate="true">
    <forward name="page" path=".EditYourPublications" />
</action>

in your action, you would do:

MyActionForm viewForm = new MyActionForm();
// populate bean here...
request.setAttribute("updatePublicationDetailsForm", viewForm);

If you need a DynaForm, then you need Struts to give it to you, because there's no simple constructor. Depending on the version of Struts you're using, there are one or two static methods in org.apache.struts.util.RequestUtils called "createActionForm(...)" If you look at the JavaDoc for RequestUtils and Action, you should be able to "chart a path" by which you can use the Action to get the objects you need to pass in as parameters to createActionForm(...) Once you get the form bean, you'd do the same -- populate its properties and put it in request or session scope.

Of course, if you happen to be using session-scoped forms, you may find that the ActionForm which is passed into your execute method is the same type that you want in the next view, in which case you can set properties and even take it for granted that it's already in session scope.

There are various ideas about making this more straightforward in the future which have been discussed thoroughly on both the dev and user lists, but hopefully this addresses your immediate needs.

Joe

--
Joe Germuska [EMAIL PROTECTED] http://blog.germuska.com "In fact, when I die, if I don't hear 'A Love Supreme,' I'll turn back; I'll know I'm in the wrong place."
- Carlos Santana


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



Reply via email to