RE: form initialization using session
> Instead, I am experimenting with overriding the struts > processActionForm() to notice when the form is created and call a > form.create() method on my ActionForm subclass, passing the > request/session in at that time. This seems to be working nicely - i > still have to have all requests for the page containing the form > accessed by an action to force initialization before the form is > displayed, but I have only one action class for all forms and the > initialization logic stays where it belongs - inside the form. > > I'm curious if others think having a form.create() method would be > useful, and if so, if there is a possibiliy to incorporate it into the > struts core? > > I guess the signature would look like > > void create(HttpServletRequest request) > > and it would be called each time a new action form bean instance is > created, on the instance created. How about a hook in the Action class instead of the ActionForm. I think you bring up a good idea, but I also think that initializing a form could be complex at times, forcing the ActionForm to be aware (coupled) to those components that are used for initialization. It is my understanding that an ActionForm is supposed to be a simple data structure that has a one-to-one mapping to each field on the form. It is also supposed to perform _simple_ validation on those fields. The Action class, on the other hand, is responsible for processing ActionForm. As suggested on this email list, I don't think that the ActionForm itself should actually implement all the processing (instead use SessionBeans, EntityBeans, JavaBeans, etc...). So, if there were a hook in the Action class, call it Action.initForm(ActionForm form) throws SomeException, that could be guaranteed to be called only once (when the form was created), then IMHO I think that would be an appropriate solution. The Action class is already coupled to the form through the various perform methods and in that respect I don't think any OO concepts are bent. My $0.02 worth. Robert
RE: form initialization using session
>The way this is typically handled is to use an Action to populate the >bean. So instead of linking your user directly to the JSP page, you link >them to an Action. Thanks for your response - i guess I arrived at the conclusion you propose. However I'm still unhappy with the notion that lots of forms are going to need their own dedicated action just to initialize them. Instead, I am experimenting with overriding the struts processActionForm() to notice when the form is created and call a form.create() method on my ActionForm subclass, passing the request/session in at that time. This seems to be working nicely - i still have to have all requests for the page containing the form accessed by an action to force initialization before the form is displayed, but I have only one action class for all forms and the initialization logic stays where it belongs - inside the form. I'm curious if others think having a form.create() method would be useful, and if so, if there is a possibiliy to incorporate it into the struts core? I guess the signature would look like void create(HttpServletRequest request) and it would be called each time a new action form bean instance is created, on the instance created. Cheers, Simon. -Original Message- From: Michael McCallister [mailto:[EMAIL PROTECTED]] Sent: Tuesday, February 27, 2001 1:01 PM To: [EMAIL PROTECTED] Subject: Re: form initialization using session The way this is typically handled is to use an Action to populate the bean. So instead of linking your user directly to the JSP page, you link them to an Action. This Action typically loads the bean and then returns the success mapping, which, in struts-config.xml, you've mapped to the JSP that displays the values from the bean. The example application that comes with struts shows how to do this. The walking tour that accompanies the example explains it better than I have. Look at the relationship between EditSubscriptionAction.java, subscription.jsp, and SaveSubscriptionAction.java. Mike At 11:46 AM 2/27/2001, you wrote: >Hi all, > >I am working on an application where form fields need to default to >values that depend on the user's profile, which is stored in the >session. Unfortunately, it seems like struts forms are created >automatically without any hooks that allow access to the session to >initialize these fields. > >The only thing that I can think of is to have our application >proactively place the forms into the session with values initialized, >possibly to have the form page link through an action which creates and >initializes the form. However this abandons the utility of having >struts automatically create the forms for us - one of the many nice >things about struts! > >This seems to me like it would be a common problem. Does anyone have a >good solution for it? > >Cheers, > >Simon.
Re: form initialization using session
The way this is typically handled is to use an Action to populate the bean. So instead of linking your user directly to the JSP page, you link them to an Action. This Action typically loads the bean and then returns the success mapping, which, in struts-config.xml, you've mapped to the JSP that displays the values from the bean. The example application that comes with struts shows how to do this. The walking tour that accompanies the example explains it better than I have. Look at the relationship between EditSubscriptionAction.java, subscription.jsp, and SaveSubscriptionAction.java. Mike At 11:46 AM 2/27/2001, you wrote: >Hi all, > >I am working on an application where form fields need to default to >values that depend on the user's profile, which is stored in the >session. Unfortunately, it seems like struts forms are created >automatically without any hooks that allow access to the session to >initialize these fields. > >The only thing that I can think of is to have our application >proactively place the forms into the session with values initialized, >possibly to have the form page link through an action which creates and >initializes the form. However this abandons the utility of having >struts automatically create the forms for us - one of the many nice >things about struts! > >This seems to me like it would be a common problem. Does anyone have a >good solution for it? > >Cheers, > >Simon.