File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes how to prepopulate FormBean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "how to prepopulate FormBean" Watch "how to prepopulate FormBean" New topic
Author

how to prepopulate FormBean

Jenny Kalinina
Ranch Hand

Joined: Nov 07, 2002
Posts: 52
Could you please answer the question:
How to access the session (or request) in FormBean.
I need to prepopulate form with values from database
which were saved in session object
Thanks in advance
Lalitha Chandran
Ranch Hand

Joined: Jul 03, 2003
Posts: 92
Hi
I think you should set all these parameters(prepopulate) in the action file rather than in the form bean. Form bean should have only getters and setters, which you could use in your action file.
Hope this helps
Lalitha
Jenny Kalinina
Ranch Hand

Joined: Nov 07, 2002
Posts: 52
Thanks Lalitha,
I tried to do that way, but then I have another problem.
I load data in ActionEdit using setters to EditForm and
display them on edit.jsp for editing. But next step is to save edited data.
I should do that in saveAction, but editForm is not passed to saveAction?
Should I save editForm in session object so that to be able to access it from
saveAction?
Thanks again
Lalitha Chandran
Ranch Hand

Joined: Jul 03, 2003
Posts: 92
Hi
I am not quite sure of your problem. But I think to save edited data you should again go to ActionEdit and not saveAction. Or you could have saveAction, but again the editForm would have to be passed. You might face other problems(problems with multiple submits) while doing this but try to solve them rather than trying to solve this one. This is just a suggestion.
The whole essence would be to have the editform passed to your action file.
I hope I am making sense.
Lalitha
Paul Duer
Ranch Hand

Joined: Oct 10, 2002
Posts: 98
Jenny,
Sounds to me as if you're missing the model a little bit. The way we do it is we have a VIEW and SAVE action.
When a page is about to be requested, the actionMapping is like /EditView.do, it goes first to an action that calls the session and the request, the ActionForm object passed to this action will be THE ONE sent to the jsp. So you load up this ActionForm with you prepops, the JSP page will display them.
Once the JSP is edited and changed, the form action on that page points to /EditSave, this action calls up the ActionForm first, then it throws them onto the database object, and finally sets the object on the session in it's updated form.
So that's the patern. Call the View action, get the database object from the session, load up the form, show the JSP, make edits, call the Save action, put the changes onto the session.
Jenny Kalinina
Ranch Hand

Joined: Nov 07, 2002
Posts: 52
Thanks everybody,
but it works well for simple flow. Really my flow is more complicated.
I have three pages to edit data for the same table
(I have 'continue' button on page1 and page2 and 'save' button on page3).
So I have 3 forms and because of that cannot use one action. I have to be able
to access two forms from one action. For example when I click 'continue' on page1,
I have to save in session object data of form1 and to populate with data the form2.
But since I cannot pass two forms to the same action, I do not how to make it working.
Thanks in advance
Paul Duer
Ranch Hand

Joined: Oct 10, 2002
Posts: 98
Jenny, I see your problem, but I still think the "simple flow" is what you need.
The path is, first a view is called to populate the 1st screen, the 1st is displayed and edited, it's form element calls the 1st SaveAction and it saves the 1st screen inputs to the session object or db. THEN, here's the kicker. The forward from the 1st SaveAction points to the ViewAction of the 2nd page. SO the view action for the 2nd page form runs and populates out the page elemetns. Then the page pops up with everything filled, including any changes on page 1 because the session object was read in the 2nd ViewAction.
So it's call 1st ViewAction, forward to page 1, submit to 1st SaveAction, forward to 2nd View Action, forward to page 2, submit to 2nd SaveAction, forward to 3rd ViewAction, forward to page 3, submit page 3, forward to exit or index.
Jenny Kalinina
Ranch Hand

Joined: Nov 07, 2002
Posts: 52
Hi Paul,
I try to follow the patern you suggested. After the 1st SaveAction's code is executed
(I checked output at the end: forward = ActionForward[success]), then error happens:
SystemOut O savecinfoAction forward=ActionForward[success]
[7/21/03 13:46:19:912 EDT] 2d493ae2 WebGroup I SRVE0180I: [EclientProfileWeb] [/EclientProfileWeb] [Servlet.LOG]: action: Processing a POST for /gotoassign
[7/21/03 13:46:19:912 EDT] 2d493ae2 WebGroup I SRVE0180I: [EclientProfileWeb] [/EclientProfileWeb] [Servlet.LOG]: action: Looking for ActionForm bean under attribute 'assignForm'
[7/21/03 13:46:19:912 EDT] 2d493ae2 WebGroup I SRVE0180I: [EclientProfileWeb] [/EclientProfileWeb] [Servlet.LOG]: action: Creating new ActionForm instance of class 'eclientprofile.web.forms.AssignForm'
[7/21/03 13:46:19:972 EDT] 2d493ae2 WebGroup I SRVE0181I: [EclientProfileWeb] [/EclientProfileWeb] [Servlet.LOG]: action: Error creating ActionForm instance of class 'eclientprofile.web.forms.AssignForm': java.lang.NullPointerException
where 'gotoassign' is my 2nd View Action and 'AssignForm' is a formBean of 2nd page that I pass to this 2nd View Action, i.e:
< action path="/edit" name="clientinfoForm" type="eclientprofile.web.actions.EditAction" scope="request">
< forward name="success" path="/clientinfoedit.jsp">
</forward>
</action>
< action name="clientinfoForm" path="/saveCinfo" scope="request" type="eclientprofile.web.actions.SaveCinfoAction">
< forward name="success" path="./gotoassign.do">
</forward>
</action>
< action path="/gotoassign" type="eclientprofile.web.actions.GotoassignAction"name="assignForm" scope="request">
< forward name="edit" path="/assignedit.jsp">
</forward>
</action>
Thanks a lot for your help
Paul Duer
Ranch Hand

Joined: Oct 10, 2002
Posts: 98
Jenny,
My first stab would be that you're missing a form tag for the AssignForm in your struts-info.xml file.
Remember that each form object must be defined in the forms section of the xml. You give each form a name for struts, and you say where the java class can be found.
It looks like it can't find a reference to that form, because there is no definition.
Jenny Kalinina
Ranch Hand

Joined: Nov 07, 2002
Posts: 52
Thanks Paul,
It works
 
Don't get me started about those stupid light bulbs.
 
subject: how to prepopulate FormBean
 
Similar Threads
preserving session formbeans
poplulate values of FormBean/reset/edit?
submitting the values of bean:write tag
collecting and validating dates
Validation in LazyDynaBean