You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Howard Miller <me...@howardmiller.co.uk> on 2002/10/20 12:51:14 UTC

How to add a pre-action

Hi,

Can somebody kindly help me with the following...

I want to add a path and an Action class. The action class will then preload data into 
a bean and then forward (do I mean forward) to the jsp that displays the bean. That's 
it. I want this action to be in *addition* to the *normal* jsp/formbean/action setup.

I can't see how to do it.... in particular

1. I don't think I fully understand how/why an action is associated with a bean in 
struts-config. What does the action servlet do with this information.

2. How do I properly forward from an action to a jsp with the bean information intact. 
It seems to be very similar to an action returning its "input form" to display errors, but 
doing this doesn't work.  Whatever I do I seem to get a blank page.

Any help very much appreciated.

Howard Miller

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


Re: How to add a pre-action

Posted by Phil Steitz <ph...@steitz.com>.
Howard Miller wrote:
> That helps a lot... just one question if you don't mind... can I check how you would 
> forward from the prepareListBookAction to the bookSearch.jsp. I assume you are just 
> using mapping.findForward( "success" ). That didn't work for me... which is really why 
> I started thinking that I was doing totally the wrong thing. Could (of course) be some 
> stupid error!
> 
Yes, on successful completion,  PrepareListBookAction.perform() ends with

return (mapping.findForward("success"));

If you are getting a blank page, there is most likely something wrong 
with your jsp.  Check your container logs.

> I'm off to try harder... thanks!
> 
> HM
> 
> On 20 Oct 2002 at 10:10, Phil Steitz wrote:
> 
> 
>>Howard Miller wrote:
>>
>>>I think I'm seriously missing the point here! I have looked at the example application 
>>>in some detail and it appears to add a parameter (eg, ?action=new) to tell the action 
>>>for a form to perform an initialising action. I don't see it using a "pre-" action.
>>>
>>>I am incredibly stuck with the concept of how to code an action that is not 
>>>encumbered with *special* parameters to pre-load the bean data. My particular 
>>>problem is that I have pages where the previous page cannot be predicted, so the jsp 
>>>must have a means of loading its own data.
>>>
>>>I would really appreciated some pointers as to how to set this up. *** I particularly 
>>>don't understand how to get the action to call the following JSP *** (ie, I would really 
>>>appreciate some specific - example - help on this!! ). At least every method I have 
>>>tried doesn't work.
>>
>>Here is a simple example showing one way to deal with this type of 
>>thing. There are no doubt lots of other ways to handle this.
>>
>>Use case: library search.  User page flow: main menu -> search input 
>>page (bookSearch.jsp) -> search results page (listBook.jsp).  The search 
>>input page needs to be pre-populated with a list of currently defined 
>>subjects.
>>
>>Struts processing flow: main menu -> search input pre-populate action 
>>(PrepareListBookAction) -> search input page (bookSearch.jsp) -> search 
>>processing action (ListBookAction) -> search results page (booklist.jsp).
>>
>>In struts-config:
>>
>><!-- Prepare Book Search (populates subject list) -->
>>     <action path="/a/prepareListBook"
>>	    name="bookSearchForm"
>>	    type="com.steitz.library.PrepareListBookAction"
>>             validate="false"
>>	    scope="request">
>>	    <forward name="success" path="/a/bookSearch.jsp"/>
>>     </action>
>>
>>     <!-- Book Search -->
>>     <action path="/a/listBook"
>>	    name="bookSearchForm"
>>	    type="com.steitz.library.ListBookAction"
>>             scope="request"
>>	    input="/a/bookSearch.jsp"
>>	    validate="true">
>>	    <forward name="success" path="/a/booklist.jsp"/>
>>     </action>
>>
>>In the main menu:
>>
>><a href="prepareListBook.do">
>>     <bean:message key="index.find"/></a>
>>(here "index.find" = "Find a Book".)
>>
>>PrepareListBookAction retrieves the list of currently defined subjects 
>>and sets a property of the (automatically created) BookSearchForm for 
>>the book search input form (bookSearch.jsp) to use.
>>
>>The key concept is that instead of linking/forwarding to the input jsp 
>>directly, you link/forward to the pre-population action and then have 
>>the pre-population action set up the form bean and then forward to the 
>>input jsp.
>>
>>Hope this helps,
>>
>>Phil
>>
>>
>>
>>
>>
>>
>>
>>
>>>I do understand how to actually create a bean manually and put in in the scope. I 
>>>don't understand how to set up a generic preload action for a given JSP/ActionForm, 
>>>and what the config should look like.
>>>
>>>Again, any help much appreciated.
>>>
>>>HM
>>>
>>>On 20 Oct 2002 at 8:43, David M. Karr wrote:
>>>
>>>
>>>
>>>>>>>>>"Howard" == Howard Miller <me...@howardmiller.co.uk> writes:
>>>>>>>>
>>>>   Howard> Hi,
>>>>   Howard> Can somebody kindly help me with the following...
>>>>
>>>>   Howard> I want to add a path and an Action class. The action class will then preload data into 
>>>>   Howard> a bean and then forward (do I mean forward) to the jsp that displays the bean. That's 
>>>>   Howard> it. I want this action to be in *addition* to the *normal* jsp/formbean/action setup.
>>>>
>>>>   Howard> I can't see how to do it.... in particular
>>>>
>>>>   Howard> 1. I don't think I fully understand how/why an action is associated with a bean in 
>>>>   Howard> struts-config. What does the action servlet do with this information.
>>>>
>>>>   Howard> 2. How do I properly forward from an action to a jsp with the bean information intact. 
>>>>   Howard> It seems to be very similar to an action returning its "input form" to display errors, but 
>>>>   Howard> doing this doesn't work.  Whatever I do I seem to get a blank page.
>>>>
>>>>In fact, the best practice is to always preface a page with an action.  You end
>>>>up with actions that prepare information for a page, and actions which process
>>>>information for a page.  The actions which process the page information will
>>>>always have had the form bean created before you get to the action.  However,
>>>>the actions which prepare the page information may have to manually create the
>>>>form bean and put it into the appropriate scope.
>>>>
>>>>Note that the welcome page for an application would normally be something like
>>>>"index.jsp", which does nothing but forward to an action to start the
>>>>application.  You can't preface the welcome page with an action, but you can
>>>>make it do nothing but go to an action to prepare the real first page.
>>>>
>>>>Look at the source code for the "example" application for some examples of
>>>>using the form bean.  The user guide also talks about the control flow with
>>>>respect to form beans.
>>>>
>>>>-- 
>>>>===================================================================
>>>>David M. Karr          ; Java/J2EE/XML/Unix/C++
>>>>dmkarr@earthlink.net
>>>>
>>>>
>>>>--
>>>>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>
> 




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


Re: How to add a pre-action

Posted by Howard Miller <me...@howardmiller.co.uk>.
That helps a lot... just one question if you don't mind... can I check how you would 
forward from the prepareListBookAction to the bookSearch.jsp. I assume you are just 
using mapping.findForward( "success" ). That didn't work for me... which is really why 
I started thinking that I was doing totally the wrong thing. Could (of course) be some 
stupid error!

I'm off to try harder... thanks!

HM

On 20 Oct 2002 at 10:10, Phil Steitz wrote:

> Howard Miller wrote:
> > I think I'm seriously missing the point here! I have looked at the example application 
> > in some detail and it appears to add a parameter (eg, ?action=new) to tell the action 
> > for a form to perform an initialising action. I don't see it using a "pre-" action.
> > 
> > I am incredibly stuck with the concept of how to code an action that is not 
> > encumbered with *special* parameters to pre-load the bean data. My particular 
> > problem is that I have pages where the previous page cannot be predicted, so the jsp 
> > must have a means of loading its own data.
> > 
> > I would really appreciated some pointers as to how to set this up. *** I particularly 
> > don't understand how to get the action to call the following JSP *** (ie, I would really 
> > appreciate some specific - example - help on this!! ). At least every method I have 
> > tried doesn't work.
> 
> Here is a simple example showing one way to deal with this type of 
> thing. There are no doubt lots of other ways to handle this.
> 
> Use case: library search.  User page flow: main menu -> search input 
> page (bookSearch.jsp) -> search results page (listBook.jsp).  The search 
> input page needs to be pre-populated with a list of currently defined 
> subjects.
> 
> Struts processing flow: main menu -> search input pre-populate action 
> (PrepareListBookAction) -> search input page (bookSearch.jsp) -> search 
> processing action (ListBookAction) -> search results page (booklist.jsp).
> 
> In struts-config:
> 
> <!-- Prepare Book Search (populates subject list) -->
>      <action path="/a/prepareListBook"
> 	    name="bookSearchForm"
> 	    type="com.steitz.library.PrepareListBookAction"
>              validate="false"
> 	    scope="request">
> 	    <forward name="success" path="/a/bookSearch.jsp"/>
>      </action>
> 
>      <!-- Book Search -->
>      <action path="/a/listBook"
> 	    name="bookSearchForm"
> 	    type="com.steitz.library.ListBookAction"
>              scope="request"
> 	    input="/a/bookSearch.jsp"
> 	    validate="true">
> 	    <forward name="success" path="/a/booklist.jsp"/>
>      </action>
> 
> In the main menu:
> 
> <a href="prepareListBook.do">
>      <bean:message key="index.find"/></a>
> (here "index.find" = "Find a Book".)
> 
> PrepareListBookAction retrieves the list of currently defined subjects 
> and sets a property of the (automatically created) BookSearchForm for 
> the book search input form (bookSearch.jsp) to use.
> 
> The key concept is that instead of linking/forwarding to the input jsp 
> directly, you link/forward to the pre-population action and then have 
> the pre-population action set up the form bean and then forward to the 
> input jsp.
> 
> Hope this helps,
> 
> Phil
> 
> 
> 
> 
> 
> 
> 
> > 
> > I do understand how to actually create a bean manually and put in in the scope. I 
> > don't understand how to set up a generic preload action for a given JSP/ActionForm, 
> > and what the config should look like.
> > 
> > Again, any help much appreciated.
> > 
> > HM
> > 
> > On 20 Oct 2002 at 8:43, David M. Karr wrote:
> > 
> > 
> >>>>>>>"Howard" == Howard Miller <me...@howardmiller.co.uk> writes:
> >>>>>>
> >>    Howard> Hi,
> >>    Howard> Can somebody kindly help me with the following...
> >>
> >>    Howard> I want to add a path and an Action class. The action class will then preload data into 
> >>    Howard> a bean and then forward (do I mean forward) to the jsp that displays the bean. That's 
> >>    Howard> it. I want this action to be in *addition* to the *normal* jsp/formbean/action setup.
> >>
> >>    Howard> I can't see how to do it.... in particular
> >>
> >>    Howard> 1. I don't think I fully understand how/why an action is associated with a bean in 
> >>    Howard> struts-config. What does the action servlet do with this information.
> >>
> >>    Howard> 2. How do I properly forward from an action to a jsp with the bean information intact. 
> >>    Howard> It seems to be very similar to an action returning its "input form" to display errors, but 
> >>    Howard> doing this doesn't work.  Whatever I do I seem to get a blank page.
> >>
> >>In fact, the best practice is to always preface a page with an action.  You end
> >>up with actions that prepare information for a page, and actions which process
> >>information for a page.  The actions which process the page information will
> >>always have had the form bean created before you get to the action.  However,
> >>the actions which prepare the page information may have to manually create the
> >>form bean and put it into the appropriate scope.
> >>
> >>Note that the welcome page for an application would normally be something like
> >>"index.jsp", which does nothing but forward to an action to start the
> >>application.  You can't preface the welcome page with an action, but you can
> >>make it do nothing but go to an action to prepare the real first page.
> >>
> >>Look at the source code for the "example" application for some examples of
> >>using the form bean.  The user guide also talks about the control flow with
> >>respect to form beans.
> >>
> >>-- 
> >>===================================================================
> >>David M. Karr          ; Java/J2EE/XML/Unix/C++
> >>dmkarr@earthlink.net
> >>
> >>
> >>--
> >>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: How to add a pre-action

Posted by Phil Steitz <ph...@steitz.com>.
Howard Miller wrote:
> I think I'm seriously missing the point here! I have looked at the example application 
> in some detail and it appears to add a parameter (eg, ?action=new) to tell the action 
> for a form to perform an initialising action. I don't see it using a "pre-" action.
> 
> I am incredibly stuck with the concept of how to code an action that is not 
> encumbered with *special* parameters to pre-load the bean data. My particular 
> problem is that I have pages where the previous page cannot be predicted, so the jsp 
> must have a means of loading its own data.
> 
> I would really appreciated some pointers as to how to set this up. *** I particularly 
> don't understand how to get the action to call the following JSP *** (ie, I would really 
> appreciate some specific - example - help on this!! ). At least every method I have 
> tried doesn't work.

Here is a simple example showing one way to deal with this type of 
thing. There are no doubt lots of other ways to handle this.

Use case: library search.  User page flow: main menu -> search input 
page (bookSearch.jsp) -> search results page (listBook.jsp).  The search 
input page needs to be pre-populated with a list of currently defined 
subjects.

Struts processing flow: main menu -> search input pre-populate action 
(PrepareListBookAction) -> search input page (bookSearch.jsp) -> search 
processing action (ListBookAction) -> search results page (booklist.jsp).

In struts-config:

<!-- Prepare Book Search (populates subject list) -->
     <action path="/a/prepareListBook"
	    name="bookSearchForm"
	    type="com.steitz.library.PrepareListBookAction"
             validate="false"
	    scope="request">
	    <forward name="success" path="/a/bookSearch.jsp"/>
     </action>

     <!-- Book Search -->
     <action path="/a/listBook"
	    name="bookSearchForm"
	    type="com.steitz.library.ListBookAction"
             scope="request"
	    input="/a/bookSearch.jsp"
	    validate="true">
	    <forward name="success" path="/a/booklist.jsp"/>
     </action>

In the main menu:

<a href="prepareListBook.do">
     <bean:message key="index.find"/></a>
(here "index.find" = "Find a Book".)

PrepareListBookAction retrieves the list of currently defined subjects 
and sets a property of the (automatically created) BookSearchForm for 
the book search input form (bookSearch.jsp) to use.

The key concept is that instead of linking/forwarding to the input jsp 
directly, you link/forward to the pre-population action and then have 
the pre-population action set up the form bean and then forward to the 
input jsp.

Hope this helps,

Phil







> 
> I do understand how to actually create a bean manually and put in in the scope. I 
> don't understand how to set up a generic preload action for a given JSP/ActionForm, 
> and what the config should look like.
> 
> Again, any help much appreciated.
> 
> HM
> 
> On 20 Oct 2002 at 8:43, David M. Karr wrote:
> 
> 
>>>>>>>"Howard" == Howard Miller <me...@howardmiller.co.uk> writes:
>>>>>>
>>    Howard> Hi,
>>    Howard> Can somebody kindly help me with the following...
>>
>>    Howard> I want to add a path and an Action class. The action class will then preload data into 
>>    Howard> a bean and then forward (do I mean forward) to the jsp that displays the bean. That's 
>>    Howard> it. I want this action to be in *addition* to the *normal* jsp/formbean/action setup.
>>
>>    Howard> I can't see how to do it.... in particular
>>
>>    Howard> 1. I don't think I fully understand how/why an action is associated with a bean in 
>>    Howard> struts-config. What does the action servlet do with this information.
>>
>>    Howard> 2. How do I properly forward from an action to a jsp with the bean information intact. 
>>    Howard> It seems to be very similar to an action returning its "input form" to display errors, but 
>>    Howard> doing this doesn't work.  Whatever I do I seem to get a blank page.
>>
>>In fact, the best practice is to always preface a page with an action.  You end
>>up with actions that prepare information for a page, and actions which process
>>information for a page.  The actions which process the page information will
>>always have had the form bean created before you get to the action.  However,
>>the actions which prepare the page information may have to manually create the
>>form bean and put it into the appropriate scope.
>>
>>Note that the welcome page for an application would normally be something like
>>"index.jsp", which does nothing but forward to an action to start the
>>application.  You can't preface the welcome page with an action, but you can
>>make it do nothing but go to an action to prepare the real first page.
>>
>>Look at the source code for the "example" application for some examples of
>>using the form bean.  The user guide also talks about the control flow with
>>respect to form beans.
>>
>>-- 
>>===================================================================
>>David M. Karr          ; Java/J2EE/XML/Unix/C++
>>dmkarr@earthlink.net
>>
>>
>>--
>>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: How to add a pre-action

Posted by Howard Miller <me...@howardmiller.co.uk>.
I think I'm seriously missing the point here! I have looked at the example application 
in some detail and it appears to add a parameter (eg, ?action=new) to tell the action 
for a form to perform an initialising action. I don't see it using a "pre-" action.

I am incredibly stuck with the concept of how to code an action that is not 
encumbered with *special* parameters to pre-load the bean data. My particular 
problem is that I have pages where the previous page cannot be predicted, so the jsp 
must have a means of loading its own data.

I would really appreciated some pointers as to how to set this up. *** I particularly 
don't understand how to get the action to call the following JSP *** (ie, I would really 
appreciate some specific - example - help on this!! ). At least every method I have 
tried doesn't work.

I do understand how to actually create a bean manually and put in in the scope. I 
don't understand how to set up a generic preload action for a given JSP/ActionForm, 
and what the config should look like.

Again, any help much appreciated.

HM

On 20 Oct 2002 at 8:43, David M. Karr wrote:

> >>>>> "Howard" == Howard Miller <me...@howardmiller.co.uk> writes:
> 
>     Howard> Hi,
>     Howard> Can somebody kindly help me with the following...
> 
>     Howard> I want to add a path and an Action class. The action class will then preload data into 
>     Howard> a bean and then forward (do I mean forward) to the jsp that displays the bean. That's 
>     Howard> it. I want this action to be in *addition* to the *normal* jsp/formbean/action setup.
> 
>     Howard> I can't see how to do it.... in particular
> 
>     Howard> 1. I don't think I fully understand how/why an action is associated with a bean in 
>     Howard> struts-config. What does the action servlet do with this information.
> 
>     Howard> 2. How do I properly forward from an action to a jsp with the bean information intact. 
>     Howard> It seems to be very similar to an action returning its "input form" to display errors, but 
>     Howard> doing this doesn't work.  Whatever I do I seem to get a blank page.
> 
> In fact, the best practice is to always preface a page with an action.  You end
> up with actions that prepare information for a page, and actions which process
> information for a page.  The actions which process the page information will
> always have had the form bean created before you get to the action.  However,
> the actions which prepare the page information may have to manually create the
> form bean and put it into the appropriate scope.
> 
> Note that the welcome page for an application would normally be something like
> "index.jsp", which does nothing but forward to an action to start the
> application.  You can't preface the welcome page with an action, but you can
> make it do nothing but go to an action to prepare the real first page.
> 
> Look at the source code for the "example" application for some examples of
> using the form bean.  The user guide also talks about the control flow with
> respect to form beans.
> 
> -- 
> ===================================================================
> David M. Karr          ; Java/J2EE/XML/Unix/C++
> dmkarr@earthlink.net
> 
> 
> --
> 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: How to add a pre-action

Posted by "David M. Karr" <dm...@earthlink.net>.
>>>>> "Howard" == Howard Miller <me...@howardmiller.co.uk> writes:

    Howard> Hi,
    Howard> Can somebody kindly help me with the following...

    Howard> I want to add a path and an Action class. The action class will then preload data into 
    Howard> a bean and then forward (do I mean forward) to the jsp that displays the bean. That's 
    Howard> it. I want this action to be in *addition* to the *normal* jsp/formbean/action setup.

    Howard> I can't see how to do it.... in particular

    Howard> 1. I don't think I fully understand how/why an action is associated with a bean in 
    Howard> struts-config. What does the action servlet do with this information.

    Howard> 2. How do I properly forward from an action to a jsp with the bean information intact. 
    Howard> It seems to be very similar to an action returning its "input form" to display errors, but 
    Howard> doing this doesn't work.  Whatever I do I seem to get a blank page.

In fact, the best practice is to always preface a page with an action.  You end
up with actions that prepare information for a page, and actions which process
information for a page.  The actions which process the page information will
always have had the form bean created before you get to the action.  However,
the actions which prepare the page information may have to manually create the
form bean and put it into the appropriate scope.

Note that the welcome page for an application would normally be something like
"index.jsp", which does nothing but forward to an action to start the
application.  You can't preface the welcome page with an action, but you can
make it do nothing but go to an action to prepare the real first page.

Look at the source code for the "example" application for some examples of
using the form bean.  The user guide also talks about the control flow with
respect to form beans.

-- 
===================================================================
David M. Karr          ; Java/J2EE/XML/Unix/C++
dmkarr@earthlink.net


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