This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Hi, Problem description: Issue with pre-populating a form before it is displayed. Background: The application flows from page 1 (displayUserForm) to page 2 (detailUserForm). Page 1 contains a list of username information and after I select one username, displayUserForm will display detail user information that was selected. I follow a similar pattern as described in struts-example. I have an action class (populateAction) that sits between displayUserForm and detailUserForm. This action class sole purpose is to populate detailuserForm properties so that when I get to page 2, the form has been populated. This is a straight forward approach, but I can't get the detailUserForm on page 2 populated. Struts throws an error saying that it can't find detailUserForm bean instance that I created and processed inside populateAction class. Any tips, hints is greatly appreciated. TIA -Vili
Have 2 different actions: one for submitting page 1 and another for populating page 2. Just chain the first to forward to the second. In struts-config, you will declare the first action to use page1 and the population action will use page2 as its name attribute. Let the controller manage the ActionForms rather than the Actions do the managing.
A good workman is known by his tools.
Joined: Mar 12, 2004
Thanks for the reply. That's exactly what my page flows is. Page 1 calls action1 and then action1 forwards to populate action class. I note from your message is "population action will use page2 as its name attribute." I don't quite follow this. Do you mean use the form defined for page 2 as the name attribute?
<action path="/page1" type="actions.Page1Action" name="page1Form"> <forward name="success" path="populatePage2.do"></forward> </action> <action path="/populatePage2" type="actions.PopulatePage2Action" name="page2Form"> <forward name="success" path="page2.jsp"></forward> </action> That's what I mean. If that doesn't help, you'll have to show what you've written.
From what I can tell: 1. You are creating an instance of ActionForm in Action1. You really don't need to do this. Instantiating the ActionForm is the responsibility of the framework. 2. You are expecting that whatever you did to the ActionForm instance you created in Action1 will be visible in Action2. As you have found, this is not so. When you forward to Action2, the Struts request-response cycle is repeated. That is, assuming you are using default mapping settings, when you forward to Action2 Struts will instantiate the ActionForm associated with Action2 and populate its fields from any matching request parameters. The changes you made to an instance of the ActionForm prior to forwarding to Action2 were made to a different instance. In other words, forwarding to Action2 has the same effect as making a direct request submission to Action2 from the web browser. Barring anything being done to the context collections in Action1, Action2 is really more or less unaware and unaffected by anything that happens to the ActionForm in Action1. It's often not necessary or advisable to forward from one action to another. It appears to me that in your case Action1 is being made to do something Action2 should be doing. Does Action2 do anything prior to displaying the details form? Why are you having Action1 prepopulate the form? If Action1 is supposed to be responsible for the list and Action2 is responsible for the details, shouldn't Action2 be responsible for prepopulating the details Form?
Hi See one thing when u forward to another action as a result of one action its ur duty to see such that the second form bean is populated.Struts wont do this for u .However the first formbean will be in scope(request/session) so u need to copy these things in ur action class.This can be done by using BeanUtil class. However i dont find the use in doing this just for displaying the details u can use the same formbean to display the results as that will be in the scope of the jsp
Joined: Mar 12, 2004
Marc, The form on the display page was populated after I change the scope from request to session. <action path="/populateModify" type="com.leonardo.oms.PopulateModifyAction" name="AdminModifyForm" scope="session"> <forward name="success" path="/adminModify.jsp"/> </action>
<action path="/modifyUser" type="com.leonardo.oms.AdminModifyAction" name="AdminModifyForm" scope="session"> <forward name="success" path="/userMaintenance.jsp"/> </action> Junilu, After seeing your post, I think I have to look at again how the pages flow. Thanks All
Vili, By changing the scope to session, you are now operating on the same instance of the ActionForm in both Actions. This may be a solution that you can live with but I think you should still evaluate how you have assigned responsibilities. Using session scoped ActionForms has its own set of gotchas. Here's something that you should be aware of regarding the session-scoped ActionForm. Action2 appears to be aware of the changes made to the ActionForm in Action1 because your request most likely only has the userId parameter. When you prepopulate the form in Action1, the values are not disturbed when the Struts request-response cycle is repeated when you forward to Action2 because Struts did not find any request parameters that matched the other detail fields; only userId was present and the same value that was pushed in to the form in Action1 was pushed in to the form for Action2. However, if there were any request parameters that matched other detail form fields, those parameter values would still overwrite any values you prepopulated in Action1. Here's an experiment you could try to see this happening. In the JSP where you select a user from a list, add a hidden field using one of the detail property names, say "firstName".
You will see that even though you have changed the scope to session, whatever value you populate firstName with in Action1 will be overwritten with "FROMREQUEST" in Action2. IMO, it would serve you well now and in the future to really understand what was going on in the first place and see if session-scoped forms are really the way to go or if a refactoring of responsibilities assigned to Action classes would be a better choice. [ April 16, 2004: Message edited by: Junilu Lacar ]