You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Sarah Farrell <sf...@mindspring.com> on 2002/02/23 06:02:52 UTC

Re: ActionForm and default values

I was trying out Ted's code, slightly modified, in an Action class:

protected ActionForm createActionForm(String path) {
         ActionServlet as = this.getServlet();
        ActionMapping mapping = as.findMapping(path);
        String name = mapping.getName();
        ActionForm form = null;
        ActionFormBean formBean = as.findFormBean(name);
                
        if (formBean != null) {
            String className = null;
            className = formBean.getType();
            try {
                Class clazz = Class.forName(className);
                form = (ActionForm) clazz.newInstance();
            } catch (Throwable t) {
                                t.printStackTrace();
                form = null;
            }
        }
                
        return form;
}

And I noticed in my application that it made a brand new form bean with clean
values (of course).  What I really wanted to do was get the existing bean from
the controller.  I'm going to need to do this for several form beans so I like
the idea of having a helper method and using the mappings.  

Does anyone have some code/suggestions for doing this?

Thanks,
Sarah




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: ActionForm and default values

Posted by Sarah Farrell <sf...@mindspring.com>.
Thanks Ted.

I read about doing the one-form-bean method, but it would be one huge file.  My
forms have way too many elements on each page with too much validation logic. 
It would be a nightmare to maintain.  Smaller ActionForms for each part makes
it easier to "see" what's going on and easier to maintain - hopefully - in the
long run.  I think it would  be hard to support good module cohesion and
coupling in one huge bean.


At 01:34 PM 2/23/2002 -0500, you wrote:
>If they were all sessions scope form-beans, then they would all be
>stored in the session context under the name given in the Struts config. 
>
>Often, developers will use one ActionForm object under one form-bean
>name for something like this, and just expose part of the object on each
>page. The Struts Validator supports this idea directly with a page
>attribute. In your case, this would mean that you could start with the
>ReviewActionForm, and just fill that out page by page. 
>
>The form-bean is an element in the Struts configuration file that the
>ActionServlet uses to create and populate ActionForm objects. The
>ActionFormBean represents the properties in the configuration file, akin
>to the ActionForward and ActionMapping classes. So, the ActionFormBean
>object tells the ActionServlet what ActionForm object to instantite. 
>
>(And boy do we regret calling everything Action* =:o)
>
>-Ted.
>
>
>Sarah Farrell wrote:
>> 
>> Short story:
>> 
>> I need to retrieve existing ActionForms (ActionFormBeans?) from the 
>Controller
>> Servlet or from the current Session. (I also do not understand the
difference
>> between an ActionForm object and an ActionFormBean object).
>> 
>> Long story:
>> 
>> I have this web application with about 10 forms on it.  Each form has it's 
>own
>> Action and ActionForm associated with it.  When the user is done with
filling
>> out the last form, I'd like them to go to a "review" page that shows all the
>> fields from all 10 forms on it in text only.  Something that they can 
>print out
>> easily and keep for their records.  But I can't just use a bunch of
>> <jsp:usebean> or <bean:write> tags in the review.jsp because I've got to do
>> some text manipulation/conversion to nice English from database values, etc.
>> that ends up being way too much logic in the jsp.
>> 
>> So I thought I'd create a ReviewAction and a ReviewActionForm.  I wanted to
>> call the ReviewAction first, have it get all the properties from all of the
>> other ActionForm/Beans and set all the properties/values in the
>> ReviewActionForm. Then have the review.jsp just display the properties of
the
>> ReviewActionForm.
>> 
>> ?
>> 
>> At 12:34 PM 2/23/2002 -0500, you wrote:
>> >I'm not sure if we know what you are trying to do =:o)
>> >
>> >Sarah Farrell wrote:
>> >>
>> >> All I can say is that I was really tired last night.  Because this 
>certainly
>> >> does not work.  So I still could use some help.....
>> >
>> >
>> >-- Ted Husted, Husted dot Com, Fairport NY US
>> >-- Developing Java Web Applications with Struts
>> >-- Tel: +1 585 737-3463
>> >-- Web: http://husted.com/about/services
>
>--
>To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
>For additional commands, e-mail: <ma...@jakarta.apache.org>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: ActionForm and default values

Posted by Sarah Farrell <sf...@mindspring.com>.
I was trying to access a form that wasn't directly associated with that
particular Action in the config file.  I'm going to want to access about 10 of
them all together inside one Action class.

Normally, you would do it the way you mentioned when you have one Action mapped
with one ActionForm in the config file.




At 09:48 PM 2/23/2002 -0500, you wrote:
>I've missed most of the thread on this, but if the struts-config.xml was 
>as you listed, wouldn't an easier way of getting the form bean be:
>
>   ContactForm cForm = (ContactForm)form;
>
>where form is one of the parameters passed to the perform method?
>
>This is the way we do it in our applications.
>
>Thanks
>Jay
>
>On Sat, 23 Feb 2002, Sarah Farrell wrote:
>
>> So the answer, in code, from within my ReviewAction class is the line:
>> 
>>         ContactForm cForm = (ContactForm)
>> request.getSession(false).getAttribute("contactForm");
>> 
>> where "contactForm" is the name in the struts-config.xml file for the
>> form-bean:
>> 
>>         <form-bean name="contactForm"
>>                 type="edu.cccs.energizer.ContactForm" />
>> 
>> Can't believe it was that simple.
>> 
>> 
>> 
>> At 01:34 PM 2/23/2002 -0500, you wrote:
>> >If they were all sessions scope form-beans, then they would all be
>> >stored in the session context under the name given in the Struts config. 
>> >
>> >Often, developers will use one ActionForm object under one form-bean
>> >name for something like this, and just expose part of the object on each
>> >page. The Struts Validator supports this idea directly with a page
>> >attribute. In your case, this would mean that you could start with the
>> >ReviewActionForm, and just fill that out page by page. 
>> >
>> >The form-bean is an element in the Struts configuration file that the
>> >ActionServlet uses to create and populate ActionForm objects. The
>> >ActionFormBean represents the properties in the configuration file, akin
>> >to the ActionForward and ActionMapping classes. So, the ActionFormBean
>> >object tells the ActionServlet what ActionForm object to instantite. 
>> >
>> >(And boy do we regret calling everything Action* =:o)
>> >
>> >-Ted.
>> >
>> >
>> >Sarah Farrell wrote:
>> >> 
>> >> Short story:
>> >> 
>> >> I need to retrieve existing ActionForms (ActionFormBeans?) from the 
>> >Controller
>> >> Servlet or from the current Session. (I also do not understand the
>> difference
>> >> between an ActionForm object and an ActionFormBean object).
>> >> 
>> >> Long story:
>> >> 
>> >> I have this web application with about 10 forms on it.  Each form has 
>it's 
>> >own
>> >> Action and ActionForm associated with it.  When the user is done with
>> filling
>> >> out the last form, I'd like them to go to a "review" page that shows 
>all the
>> >> fields from all 10 forms on it in text only.  Something that they can 
>> >print out
>> >> easily and keep for their records.  But I can't just use a bunch of
>> >> <jsp:usebean> or <bean:write> tags in the review.jsp because I've got 
>to do
>> >> some text manipulation/conversion to nice English from database values, 
>etc.
>> >> that ends up being way too much logic in the jsp.
>> >> 
>> >> So I thought I'd create a ReviewAction and a ReviewActionForm.  I 
>wanted to
>> >> call the ReviewAction first, have it get all the properties from all of 
>the
>> >> other ActionForm/Beans and set all the properties/values in the
>> >> ReviewActionForm. Then have the review.jsp just display the properties of
>> the
>> >> ReviewActionForm.
>> >> 
>> >> ?
>> >> 
>> >> At 12:34 PM 2/23/2002 -0500, you wrote:
>> >> >I'm not sure if we know what you are trying to do =:o)
>> >> >
>> >> >Sarah Farrell wrote:
>> >> >>
>> >> >> All I can say is that I was really tired last night.  Because this 
>> >certainly
>> >> >> does not work.  So I still could use some help.....
>> >> >
>> >> >
>> >> >-- Ted Husted, Husted dot Com, Fairport NY US
>> >> >-- Developing Java Web Applications with Struts
>> >> >-- Tel: +1 585 737-3463
>> >> >-- Web: http://husted.com/about/services
>> >
>> >--
>> >To unsubscribe, e-mail:   
><ma...@jakarta.apache.org>
>> >For additional commands, e-mail: 
><ma...@jakarta.apache.org>
>> 
>> 
>> --
>> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
>> For additional commands, e-mail:
<ma...@jakarta.apache.org>
>> 
>
>
>--
>To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
>For additional commands, e-mail: <ma...@jakarta.apache.org>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: ActionForm and default values

Posted by Jay sissom <js...@toybox.uits.indiana.edu>.
I've missed most of the thread on this, but if the struts-config.xml was 
as you listed, wouldn't an easier way of getting the form bean be:

   ContactForm cForm = (ContactForm)form;

where form is one of the parameters passed to the perform method?

This is the way we do it in our applications.

Thanks
Jay

On Sat, 23 Feb 2002, Sarah Farrell wrote:

> So the answer, in code, from within my ReviewAction class is the line:
> 
>         ContactForm cForm = (ContactForm)
> request.getSession(false).getAttribute("contactForm");
> 
> where "contactForm" is the name in the struts-config.xml file for the
> form-bean:
> 
>         <form-bean name="contactForm"
>                 type="edu.cccs.energizer.ContactForm" />
> 
> Can't believe it was that simple.
> 
> 
> 
> At 01:34 PM 2/23/2002 -0500, you wrote:
> >If they were all sessions scope form-beans, then they would all be
> >stored in the session context under the name given in the Struts config. 
> >
> >Often, developers will use one ActionForm object under one form-bean
> >name for something like this, and just expose part of the object on each
> >page. The Struts Validator supports this idea directly with a page
> >attribute. In your case, this would mean that you could start with the
> >ReviewActionForm, and just fill that out page by page. 
> >
> >The form-bean is an element in the Struts configuration file that the
> >ActionServlet uses to create and populate ActionForm objects. The
> >ActionFormBean represents the properties in the configuration file, akin
> >to the ActionForward and ActionMapping classes. So, the ActionFormBean
> >object tells the ActionServlet what ActionForm object to instantite. 
> >
> >(And boy do we regret calling everything Action* =:o)
> >
> >-Ted.
> >
> >
> >Sarah Farrell wrote:
> >> 
> >> Short story:
> >> 
> >> I need to retrieve existing ActionForms (ActionFormBeans?) from the 
> >Controller
> >> Servlet or from the current Session. (I also do not understand the
> difference
> >> between an ActionForm object and an ActionFormBean object).
> >> 
> >> Long story:
> >> 
> >> I have this web application with about 10 forms on it.  Each form has it's 
> >own
> >> Action and ActionForm associated with it.  When the user is done with
> filling
> >> out the last form, I'd like them to go to a "review" page that shows all the
> >> fields from all 10 forms on it in text only.  Something that they can 
> >print out
> >> easily and keep for their records.  But I can't just use a bunch of
> >> <jsp:usebean> or <bean:write> tags in the review.jsp because I've got to do
> >> some text manipulation/conversion to nice English from database values, etc.
> >> that ends up being way too much logic in the jsp.
> >> 
> >> So I thought I'd create a ReviewAction and a ReviewActionForm.  I wanted to
> >> call the ReviewAction first, have it get all the properties from all of the
> >> other ActionForm/Beans and set all the properties/values in the
> >> ReviewActionForm. Then have the review.jsp just display the properties of
> the
> >> ReviewActionForm.
> >> 
> >> ?
> >> 
> >> At 12:34 PM 2/23/2002 -0500, you wrote:
> >> >I'm not sure if we know what you are trying to do =:o)
> >> >
> >> >Sarah Farrell wrote:
> >> >>
> >> >> All I can say is that I was really tired last night.  Because this 
> >certainly
> >> >> does not work.  So I still could use some help.....
> >> >
> >> >
> >> >-- Ted Husted, Husted dot Com, Fairport NY US
> >> >-- Developing Java Web Applications with Struts
> >> >-- Tel: +1 585 737-3463
> >> >-- Web: http://husted.com/about/services
> >
> >--
> >To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> >For additional commands, e-mail: <ma...@jakarta.apache.org>
> 
> 
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
> 


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: ActionForm and default values

Posted by Sarah Farrell <sf...@mindspring.com>.
So the answer, in code, from within my ReviewAction class is the line:

        ContactForm cForm = (ContactForm)
request.getSession(false).getAttribute("contactForm");

where "contactForm" is the name in the struts-config.xml file for the
form-bean:

        <form-bean name="contactForm"
                type="edu.cccs.energizer.ContactForm" />

Can't believe it was that simple.



At 01:34 PM 2/23/2002 -0500, you wrote:
>If they were all sessions scope form-beans, then they would all be
>stored in the session context under the name given in the Struts config. 
>
>Often, developers will use one ActionForm object under one form-bean
>name for something like this, and just expose part of the object on each
>page. The Struts Validator supports this idea directly with a page
>attribute. In your case, this would mean that you could start with the
>ReviewActionForm, and just fill that out page by page. 
>
>The form-bean is an element in the Struts configuration file that the
>ActionServlet uses to create and populate ActionForm objects. The
>ActionFormBean represents the properties in the configuration file, akin
>to the ActionForward and ActionMapping classes. So, the ActionFormBean
>object tells the ActionServlet what ActionForm object to instantite. 
>
>(And boy do we regret calling everything Action* =:o)
>
>-Ted.
>
>
>Sarah Farrell wrote:
>> 
>> Short story:
>> 
>> I need to retrieve existing ActionForms (ActionFormBeans?) from the 
>Controller
>> Servlet or from the current Session. (I also do not understand the
difference
>> between an ActionForm object and an ActionFormBean object).
>> 
>> Long story:
>> 
>> I have this web application with about 10 forms on it.  Each form has it's 
>own
>> Action and ActionForm associated with it.  When the user is done with
filling
>> out the last form, I'd like them to go to a "review" page that shows all the
>> fields from all 10 forms on it in text only.  Something that they can 
>print out
>> easily and keep for their records.  But I can't just use a bunch of
>> <jsp:usebean> or <bean:write> tags in the review.jsp because I've got to do
>> some text manipulation/conversion to nice English from database values, etc.
>> that ends up being way too much logic in the jsp.
>> 
>> So I thought I'd create a ReviewAction and a ReviewActionForm.  I wanted to
>> call the ReviewAction first, have it get all the properties from all of the
>> other ActionForm/Beans and set all the properties/values in the
>> ReviewActionForm. Then have the review.jsp just display the properties of
the
>> ReviewActionForm.
>> 
>> ?
>> 
>> At 12:34 PM 2/23/2002 -0500, you wrote:
>> >I'm not sure if we know what you are trying to do =:o)
>> >
>> >Sarah Farrell wrote:
>> >>
>> >> All I can say is that I was really tired last night.  Because this 
>certainly
>> >> does not work.  So I still could use some help.....
>> >
>> >
>> >-- Ted Husted, Husted dot Com, Fairport NY US
>> >-- Developing Java Web Applications with Struts
>> >-- Tel: +1 585 737-3463
>> >-- Web: http://husted.com/about/services
>
>--
>To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
>For additional commands, e-mail: <ma...@jakarta.apache.org>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: ActionForm and default values

Posted by Ted Husted <hu...@apache.org>.
If they were all sessions scope form-beans, then they would all be
stored in the session context under the name given in the Struts config. 

Often, developers will use one ActionForm object under one form-bean
name for something like this, and just expose part of the object on each
page. The Struts Validator supports this idea directly with a page
attribute. In your case, this would mean that you could start with the
ReviewActionForm, and just fill that out page by page. 

The form-bean is an element in the Struts configuration file that the
ActionServlet uses to create and populate ActionForm objects. The
ActionFormBean represents the properties in the configuration file, akin
to the ActionForward and ActionMapping classes. So, the ActionFormBean
object tells the ActionServlet what ActionForm object to instantite. 

(And boy do we regret calling everything Action* =:o)

-Ted.


Sarah Farrell wrote:
> 
> Short story:
> 
> I need to retrieve existing ActionForms (ActionFormBeans?) from the Controller
> Servlet or from the current Session. (I also do not understand the difference
> between an ActionForm object and an ActionFormBean object).
> 
> Long story:
> 
> I have this web application with about 10 forms on it.  Each form has it's own
> Action and ActionForm associated with it.  When the user is done with filling
> out the last form, I'd like them to go to a "review" page that shows all the
> fields from all 10 forms on it in text only.  Something that they can print out
> easily and keep for their records.  But I can't just use a bunch of
> <jsp:usebean> or <bean:write> tags in the review.jsp because I've got to do
> some text manipulation/conversion to nice English from database values, etc.
> that ends up being way too much logic in the jsp.
> 
> So I thought I'd create a ReviewAction and a ReviewActionForm.  I wanted to
> call the ReviewAction first, have it get all the properties from all of the
> other ActionForm/Beans and set all the properties/values in the
> ReviewActionForm. Then have the review.jsp just display the properties of the
> ReviewActionForm.
> 
> ?
> 
> At 12:34 PM 2/23/2002 -0500, you wrote:
> >I'm not sure if we know what you are trying to do =:o)
> >
> >Sarah Farrell wrote:
> >>
> >> All I can say is that I was really tired last night.  Because this certainly
> >> does not work.  So I still could use some help.....
> >
> >
> >-- Ted Husted, Husted dot Com, Fairport NY US
> >-- Developing Java Web Applications with Struts
> >-- Tel: +1 585 737-3463
> >-- Web: http://husted.com/about/services

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: ActionForm and default values

Posted by Sarah Farrell <sf...@mindspring.com>.
Short story:  

I need to retrieve existing ActionForms (ActionFormBeans?) from the Controller
Servlet or from the current Session. (I also do not understand the difference
between an ActionForm object and an ActionFormBean object).

Long story:

I have this web application with about 10 forms on it.  Each form has it's own
Action and ActionForm associated with it.  When the user is done with filling
out the last form, I'd like them to go to a "review" page that shows all the
fields from all 10 forms on it in text only.  Something that they can print out
easily and keep for their records.  But I can't just use a bunch of
<jsp:usebean> or <bean:write> tags in the review.jsp because I've got to do
some text manipulation/conversion to nice English from database values, etc.
that ends up being way too much logic in the jsp.

So I thought I'd create a ReviewAction and a ReviewActionForm.  I wanted to
call the ReviewAction first, have it get all the properties from all of the
other ActionForm/Beans and set all the properties/values in the
ReviewActionForm. Then have the review.jsp just display the properties of the
ReviewActionForm.

?




At 12:34 PM 2/23/2002 -0500, you wrote:
>I'm not sure if we know what you are trying to do =:o)
>
>Sarah Farrell wrote:
>> 
>> All I can say is that I was really tired last night.  Because this certainly
>> does not work.  So I still could use some help.....
>
>
>-- Ted Husted, Husted dot Com, Fairport NY US
>-- Developing Java Web Applications with Struts
>-- Tel: +1 585 737-3463
>-- Web: http://husted.com/about/services
>
>--
>To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
>For additional commands, e-mail: <ma...@jakarta.apache.org>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: ActionForm and default values

Posted by Ted Husted <hu...@apache.org>.
I'm not sure if we know what you are trying to do =:o)

Sarah Farrell wrote:
> 
> All I can say is that I was really tired last night.  Because this certainly
> does not work.  So I still could use some help.....


-- Ted Husted, Husted dot Com, Fairport NY US
-- Developing Java Web Applications with Struts
-- Tel: +1 585 737-3463
-- Web: http://husted.com/about/services

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: ActionForm and default values

Posted by Sarah Farrell <sf...@mindspring.com>.
All I can say is that I was really tired last night.  Because this certainly
does not work.  So I still could use some help.....


At 10:34 PM 2/22/2002 -0700, you wrote:
>It's Friday, it's late, and I'm answering my own posts....
>
>replacing "newInstance()" with "getInstance()" does the trick.
>
>
>At 10:02 PM 2/22/2002 -0700, you wrote:
>>I was trying out Ted's code, slightly modified, in an Action class:
>>
>>protected ActionForm createActionForm(String path) {
>>         ActionServlet as = this.getServlet();
>>        ActionMapping mapping = as.findMapping(path);
>>        String name = mapping.getName();
>>        ActionForm form = null;
>>        ActionFormBean formBean = as.findFormBean(name);
>>                
>>        if (formBean != null) {
>>            String className = null;
>>            className = formBean.getType();
>>            try {
>>                Class clazz = Class.forName(className);
>>                form = (ActionForm) clazz.newInstance();
>>            } catch (Throwable t) {
>>                                t.printStackTrace();
>>                form = null;
>>            }
>>        }
>>                
>>        return form;
>>}
>>
>>And I noticed in my application that it made a brand new form bean with clean
>>values (of course).  What I really wanted to do was get the existing bean
from
>>the controller.  I'm going to need to do this for several form beans so I
like
>>the idea of having a helper method and using the mappings.  
>>
>>Does anyone have some code/suggestions for doing this?
>>
>>Thanks,
>>Sarah
>>
>>
>>
>>
>>--
>>To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
>>For additional commands, e-mail: <ma...@jakarta.apache.org>
>
>
>--
>To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
>For additional commands, e-mail: <ma...@jakarta.apache.org>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: ActionForm and default values

Posted by Sarah Farrell <sf...@mindspring.com>.
It's Friday, it's late, and I'm answering my own posts....

replacing "newInstance()" with "getInstance()" does the trick.


At 10:02 PM 2/22/2002 -0700, you wrote:
>I was trying out Ted's code, slightly modified, in an Action class:
>
>protected ActionForm createActionForm(String path) {
>         ActionServlet as = this.getServlet();
>        ActionMapping mapping = as.findMapping(path);
>        String name = mapping.getName();
>        ActionForm form = null;
>        ActionFormBean formBean = as.findFormBean(name);
>                
>        if (formBean != null) {
>            String className = null;
>            className = formBean.getType();
>            try {
>                Class clazz = Class.forName(className);
>                form = (ActionForm) clazz.newInstance();
>            } catch (Throwable t) {
>                                t.printStackTrace();
>                form = null;
>            }
>        }
>                
>        return form;
>}
>
>And I noticed in my application that it made a brand new form bean with clean
>values (of course).  What I really wanted to do was get the existing bean from
>the controller.  I'm going to need to do this for several form beans so I like
>the idea of having a helper method and using the mappings.  
>
>Does anyone have some code/suggestions for doing this?
>
>Thanks,
>Sarah
>
>
>
>
>--
>To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
>For additional commands, e-mail: <ma...@jakarta.apache.org>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>