You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Kevin Duffey <kd...@buymedia.com> on 2000/06/21 08:09:14 UTC

Struts--question about bean/form name.

Hi,

is there any way in each of my action perform() methods to get the "name" of
the bean that was set in the action.xml, so that I can use that String name
for various purposes? For example, we don't use EJB yet, so we separate our
logic into "logic_session" classes. These session classes are stored in the
HttpSession, with a reference to our bean, and I would like to store them by
using the name of the bean (form passed in) and appending "Session" to that
name. In the JSP page I have to hardcode it though..which means I still have
to change the bean name in two places..if I change it for any reason.

Thanks.


Re: Struts--question about bean/form name.

Posted by "Craig R. McClanahan" <Cr...@Eng.Sun.COM>.
Kevin Duffey wrote:

> Hi,
>
> is there any way in each of my action perform() methods to get the "name" of
> the bean that was set in the action.xml, so that I can use that String name
> for various purposes? For example, we don't use EJB yet, so we separate our
> logic into "logic_session" classes. These session classes are stored in the
> HttpSession, with a reference to our bean, and I would like to store them by
> using the name of the bean (form passed in) and appending "Session" to that
> name. In the JSP page I have to hardcode it though..which means I still have
> to change the bean name in two places..if I change it for any reason.
>

The perform() method of an action receives a "mapping" parameter that is the
ActionMapping instance which caused this action to be invoked.  Therefore, you
can access any of the properties of that mapping -- for example, you can
retrieve the session key under which the form bean for this action is stored, by
calling

    mapping.getFormAttribute()

in your action class.  See the next response for how to extend this capability
with your own properties.

>
> Thanks.

Craig



Re: Struts--question about bean/form name.

Posted by "Craig R. McClanahan" <Cr...@Eng.Sun.COM>.
Kevin Duffey wrote:

> Hi,
>
> is there any way in each of my action perform() methods to get the "name" of
> the bean that was set in the action.xml, so that I can use that String name
> for various purposes? For example, we don't use EJB yet, so we separate our
> logic into "logic_session" classes. These session classes are stored in the
> HttpSession, with a reference to our bean, and I would like to store them by
> using the name of the bean (form passed in) and appending "Session" to that
> name. In the JSP page I have to hardcode it though..which means I still have
> to change the bean name in two places..if I change it for any reason.
>

The perform() method of an action receives a "mapping" parameter that is the
ActionMapping instance which caused this action to be invoked.  Therefore, you
can access any of the properties of that mapping -- for example, you can
retrieve the session key under which the form bean for this action is stored, by
calling

    mapping.getFormAttribute()

in your action class.  See the next response for how to extend this capability
with your own properties.

>
> Thanks.

Craig



RE: Struts--question about bean/form name.

Posted by David Chisholm <Da...@i2.com>.
Excellent point.
David


> -----Original Message-----
> From: Craig R. McClanahan [mailto:Craig.McClanahan@eng.sun.com]
> Sent: Wednesday, June 21, 2000 4:41 PM
> To: struts-dev@jakarta.apache.org
> Subject: Re: Struts--question about bean/form name.
>
>
> David Chisholm wrote:
>
> >
> > What I would like to do in the init method is configure static
> attributes of
> > the action that may come from a dynamic source like a database.
>  This could
> > be done in the constructor, but the action may need information from its
> > ActionMapping instance.
> >
> > I like the ability to define your own custom ActionMapping
> class, but it's
> > sorta like Java's single inheritance - you've only got one to
> use.  However,
> > you're instructions on adding custom rules like addForward()
> shows how to
> > add a lot flexibility to the ActionMapping.
> >
>
> You could also dispense with the idea of creating the
> ActionMapping collections
> from an XML file at all, if you wanted to.  After all, an ActionMapping
> implementation class (like ActionMappingBase) is just a JavaBean with some
> properties.  You could easily initialize them in whatever manner
> you like (such
> as loading them from a database) by overriding the initMapping()
> method, but
> still take advantage of the rest of the framework.  All you need
> to do is call
> the ActionServlet's addMapping() method so that it knows about
> the completed
> mapping beans.
>
> That is kind of why I called the thing "Struts" in the first
> place -- it is a
> bunch of small pieces that work together synergistically, but you
> should have
> the right to replace my standard yellow strut with your red one,
> without having
> to build the entire structure over again :-).
>
> >
> > David
> >
>
> Craig
>
>


Re: Struts--question about bean/form name.

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
David Chisholm wrote:

>
> What I would like to do in the init method is configure static attributes of
> the action that may come from a dynamic source like a database.  This could
> be done in the constructor, but the action may need information from its
> ActionMapping instance.
>
> I like the ability to define your own custom ActionMapping class, but it's
> sorta like Java's single inheritance - you've only got one to use.  However,
> you're instructions on adding custom rules like addForward() shows how to
> add a lot flexibility to the ActionMapping.
>

You could also dispense with the idea of creating the ActionMapping collections
from an XML file at all, if you wanted to.  After all, an ActionMapping
implementation class (like ActionMappingBase) is just a JavaBean with some
properties.  You could easily initialize them in whatever manner you like (such
as loading them from a database) by overriding the initMapping() method, but
still take advantage of the rest of the framework.  All you need to do is call
the ActionServlet's addMapping() method so that it knows about the completed
mapping beans.

That is kind of why I called the thing "Struts" in the first place -- it is a
bunch of small pieces that work together synergistically, but you should have
the right to replace my standard yellow strut with your red one, without having
to build the entire structure over again :-).

>
> David
>

Craig



RE: Struts--question about bean/form name.

Posted by David Chisholm <Da...@i2.com>.
comment at end...

> -----Original Message-----
> From: Craig R. McClanahan [mailto:Craig.McClanahan@Eng.Sun.COM]
> Sent: Wednesday, June 21, 2000 11:22 AM
> To: struts-user@jakarta.apache.org
> Cc: struts-dev@jakarta.apache.org
> Subject: Re: Struts--question about bean/form name.
>
>
> David Chisholm wrote:
>
> > On a similar note, can you associate non-standard attributes
> for an action
> > in the action.xml, and then retrieve those values in the
> Action.perform()
> > method?  For example, adding the useDefaultHomePage attribute
> shown below to
> > the example logon action:
> >
> >   <action path="/logon"
> >     actionClass="org.apache.struts.example.LogonAction"
> >     formAttribute="logonForm"
> >     formClass="org.apache.struts.example.LogonForm"
> >     inputForm="/logon.jsp">
> >     <forward name="logon"      path="/logon.jsp"/>
> >     <forward name="success"    path="/mainMenu.jsp"/>
> >     useDefaultHomePage="true"
> >   </action>
> >
> > One suggestion, add a 'public void init(ActionMapping mapping)'
> method to
> > Action that is called when the Action is instantiated to allow
> the Action to
> > perform any static initialization that it needs, and a 'public void
> > destroy()' to allow it to do any cleanup.  ActionBase would then provide
> > default implementations of these methods.
> >
>
> The approach to non-standard attributes in ActionMappings is
> actually pretty
> cool.  It works like this (documented in the User's Guide, by the way).
>
> * Create a new class that implements ActionMapping (or extends
>   ActionMappingBase, which is usually more convenient).
>
> * In this new class, add public getter and setter methods for the
>   new properties you want to define, following JavaBeans naming
>   conventions.  The property type can be String, int, boolean, float,
>   or double.
>
> * In the action.xml file, add new attributes in the <action> element to
>   initialize the new property values.
>
> * In the web.xml file for your application, declare a servlet
> initialization
>   parameter for the controller servlet that identifies the name of the
>   ActionMapping class to be used (i.e. your new one):
>
>         <init-param>
>             <param-name>mapping</param-name>
>             <param-value>com.mycompany.MyActionMapping</param-value>
>         </init-param>
>
> * When the controller servlet starts, it will auto-magically set all the
> standard
>   ActionMapping properties, plus all of your new ones.  It uses
> Java reflection
>   to match up the property names versus the setter methods in your
>   ActionMapping class.
>
> This would seem to avoid the need to build your own init() method in an
> ActionMapping simply to configure non-standard property values.
>

What I would like to do in the init method is configure static attributes of
the action that may come from a dynamic source like a database.  This could
be done in the constructor, but the action may need information from its
ActionMapping instance.

I like the ability to define your own custom ActionMapping class, but it's
sorta like Java's single inheritance - you've only got one to use.  However,
you're instructions on adding custom rules like addForward() shows how to
add a lot flexibility to the ActionMapping.

David









> >
> > David
> >
>
> Craig McClanahan
>
>


Re: Struts--question about bean/form name.

Posted by "Craig R. McClanahan" <Cr...@Eng.Sun.COM>.
David Chisholm wrote:

> On a similar note, can you associate non-standard attributes for an action
> in the action.xml, and then retrieve those values in the Action.perform()
> method?  For example, adding the useDefaultHomePage attribute shown below to
> the example logon action:
>
>   <action path="/logon"
>     actionClass="org.apache.struts.example.LogonAction"
>     formAttribute="logonForm"
>     formClass="org.apache.struts.example.LogonForm"
>     inputForm="/logon.jsp">
>     <forward name="logon"      path="/logon.jsp"/>
>     <forward name="success"    path="/mainMenu.jsp"/>
>     useDefaultHomePage="true"
>   </action>
>
> One suggestion, add a 'public void init(ActionMapping mapping)' method to
> Action that is called when the Action is instantiated to allow the Action to
> perform any static initialization that it needs, and a 'public void
> destroy()' to allow it to do any cleanup.  ActionBase would then provide
> default implementations of these methods.
>

The approach to non-standard attributes in ActionMappings is actually pretty
cool.  It works like this (documented in the User's Guide, by the way).

* Create a new class that implements ActionMapping (or extends
  ActionMappingBase, which is usually more convenient).

* In this new class, add public getter and setter methods for the
  new properties you want to define, following JavaBeans naming
  conventions.  The property type can be String, int, boolean, float,
  or double.

* In the action.xml file, add new attributes in the <action> element to
  initialize the new property values.

* In the web.xml file for your application, declare a servlet initialization
  parameter for the controller servlet that identifies the name of the
  ActionMapping class to be used (i.e. your new one):

        <init-param>
            <param-name>mapping</param-name>
            <param-value>com.mycompany.MyActionMapping</param-value>
        </init-param>

* When the controller servlet starts, it will auto-magically set all the
standard
  ActionMapping properties, plus all of your new ones.  It uses Java reflection
  to match up the property names versus the setter methods in your
  ActionMapping class.

This would seem to avoid the need to build your own init() method in an
ActionMapping simply to configure non-standard property values.

>
> David
>

Craig McClanahan



Re: Struts--question about bean/form name.

Posted by "Craig R. McClanahan" <Cr...@Eng.Sun.COM>.
David Chisholm wrote:

> On a similar note, can you associate non-standard attributes for an action
> in the action.xml, and then retrieve those values in the Action.perform()
> method?  For example, adding the useDefaultHomePage attribute shown below to
> the example logon action:
>
>   <action path="/logon"
>     actionClass="org.apache.struts.example.LogonAction"
>     formAttribute="logonForm"
>     formClass="org.apache.struts.example.LogonForm"
>     inputForm="/logon.jsp">
>     <forward name="logon"      path="/logon.jsp"/>
>     <forward name="success"    path="/mainMenu.jsp"/>
>     useDefaultHomePage="true"
>   </action>
>
> One suggestion, add a 'public void init(ActionMapping mapping)' method to
> Action that is called when the Action is instantiated to allow the Action to
> perform any static initialization that it needs, and a 'public void
> destroy()' to allow it to do any cleanup.  ActionBase would then provide
> default implementations of these methods.
>

The approach to non-standard attributes in ActionMappings is actually pretty
cool.  It works like this (documented in the User's Guide, by the way).

* Create a new class that implements ActionMapping (or extends
  ActionMappingBase, which is usually more convenient).

* In this new class, add public getter and setter methods for the
  new properties you want to define, following JavaBeans naming
  conventions.  The property type can be String, int, boolean, float,
  or double.

* In the action.xml file, add new attributes in the <action> element to
  initialize the new property values.

* In the web.xml file for your application, declare a servlet initialization
  parameter for the controller servlet that identifies the name of the
  ActionMapping class to be used (i.e. your new one):

        <init-param>
            <param-name>mapping</param-name>
            <param-value>com.mycompany.MyActionMapping</param-value>
        </init-param>

* When the controller servlet starts, it will auto-magically set all the
standard
  ActionMapping properties, plus all of your new ones.  It uses Java reflection
  to match up the property names versus the setter methods in your
  ActionMapping class.

This would seem to avoid the need to build your own init() method in an
ActionMapping simply to configure non-standard property values.

>
> David
>

Craig McClanahan



Re: Struts--question about bean/form name.

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
David Chisholm wrote:

> Thanks Craig.  I had looked at the initMapping() method, but didn't
> understand what it was doing.  Your response would make a nice addition to
> the struts documentation.
> David
>

Yah, the Digester package deserve's its own users guide with a bunch of
examples.  You know how it is ... the code always seems to come before the
documentation :-).

Craig



RE: Struts--question about bean/form name.

Posted by David Chisholm <Da...@i2.com>.
Thanks Craig.  I had looked at the initMapping() method, but didn't
understand what it was doing.  Your response would make a nice addition to
the struts documentation.
David


> -----Original Message-----
> From: Craig R. McClanahan [mailto:Craig.McClanahan@Eng.Sun.COM]
> Sent: Wednesday, June 21, 2000 12:04 PM
> To: struts-user@jakarta.apache.org
> Subject: Re: Struts--question about bean/form name.
>
>
> David Chisholm wrote:
>
> > Can you create a custom ActionMapping class that can store arbitrary
> > information?  (This doesn't appear possible.)
>
> You can in fact do this -- it becomes a little more work.  I
> followed these
> steps when I added the ability to define as many <forward>
> elements as you want
> underneath an <action>, so I will describe the steps in terms of
> that scenario:
>
> * Optionally, create a new class that represents the "thing" you are going
>   to be saving configuration in, if it's not just a simple
> string.  In my case,
>   I created a new ActionForward class to represent a logical name, a
>   corresponding path, and a redirect flag.
>
> * Create an extended ActionMapping class that includes a collection (a
>   Hashtable or HashMap, for example) for the new thing you want to add.
>   In my case, I added a Hashtable called "forwards" to ActionMappingBase.
>
> * In this custom ActionMapping method, create a new public method that
>   adds a new entry to this collection.  In my case, I added:
>         public void addForward(ActionForward forward) {
>             forwards.put(forward.getName(), forward);
>         }
>
> * Override the initMapping() method of ActionServlet with one
> that adds some
>   additional rules to the Digester that processes the XML file.
> The precise
>   details here get pretty intense -- suffice it to say that I added the
> following
>   rules:
>
>         digester.addObjectCreate("action-mappings/action/forward",
>             forwardClass);
>         digester.addSetProperties("action-mappings/action/forward");
>         digester.addSetNext("action-mappings/action/forward",
>             "addForward", "org.apache.struts.action.ActionForward");
>
>   that cause the following actions whenever a <forward> element is found
>   within an <action> element:
>
>     -   Create a new object of the class specified by the "forwardClass"
>         variable (normally "org.apache.struts.action.ActionForward")
>         and push it on the digester's stack.
>     -   On the new top stack element, map all the properties from
> the current
>         XML element (the <forward>) into corresponding setXxxx calls.
>     -   Call the addForward() method of the next-to-top element
> on the stack,
>         passing the top element of the stack as an argument.  The
> method expects
>
>         an argument of type org.apache.struts.action.ActionForward (or a
> subclass
>         of that class).
>     -   Pop the top element (the ActionForward object) off the
> digester's stack.
>
> In principle, you can do the same sort of thing for any
> arbitrarily nested set
> of XML elements to define pretty much any configuration
> information that can be
> statically represented as a tree of objects -- no loops or many-to-many
> relationships, but pretty much anything else.  For a more complex
> example of
> this same design approach, look at Tomcat's "conf/server.xml"
> file -- Tomcat
> uses a very similar approach to configure itself based on the
> contents of a very
> comprehensive configuration file.  Tomcat uses this to read the
> web.xml file
> describing your application as well.
>
> >  How about a new feature that
> > allows you to specify the custom ActionMapping on a per action
> basis in the
> > action.xml file.  For example:
> >
> >  <action path="/logon"
> >    actionClass="org.apache.struts.example.LogonAction"
> >    formAttribute="logonForm"
> >    formClass="org.apache.struts.example.LogonForm"
> >    inputForm="/logon.jsp"
> >    actionMapping="my.custom.LogonActionMapping”
> >    useDefaultHomePage=”true”>
> >      <forward name="logon"      path="/logon.jsp"/>
> >      <forward name="success"    path="/mainMenu.jsp"/>
> >  </action>
> >
> > When parsed, the custom action mapping would be instantiated and used to
> > store the custom ‘useDefaultHomePage’ attribute.
>
> > Is this feasible?
> >
>
> A custom ActionMapping per action entry?  That seems like it
> might be reasonable
> to make a standard feature of, falling back to the default one if
> you don't
> override it.  I'll look at that scenario.
>
> >
> > David
> >
>
> Craig
>
>


Re: Struts--question about bean/form name.

Posted by "Craig R. McClanahan" <Cr...@Eng.Sun.COM>.
David Chisholm wrote:

> Can you create a custom ActionMapping class that can store arbitrary
> information?  (This doesn't appear possible.)

You can in fact do this -- it becomes a little more work.  I followed these
steps when I added the ability to define as many <forward> elements as you want
underneath an <action>, so I will describe the steps in terms of that scenario:

* Optionally, create a new class that represents the "thing" you are going
  to be saving configuration in, if it's not just a simple string.  In my case,
  I created a new ActionForward class to represent a logical name, a
  corresponding path, and a redirect flag.

* Create an extended ActionMapping class that includes a collection (a
  Hashtable or HashMap, for example) for the new thing you want to add.
  In my case, I added a Hashtable called "forwards" to ActionMappingBase.

* In this custom ActionMapping method, create a new public method that
  adds a new entry to this collection.  In my case, I added:
        public void addForward(ActionForward forward) {
            forwards.put(forward.getName(), forward);
        }

* Override the initMapping() method of ActionServlet with one that adds some
  additional rules to the Digester that processes the XML file.  The precise
  details here get pretty intense -- suffice it to say that I added the
following
  rules:

        digester.addObjectCreate("action-mappings/action/forward",
            forwardClass);
        digester.addSetProperties("action-mappings/action/forward");
        digester.addSetNext("action-mappings/action/forward",
            "addForward", "org.apache.struts.action.ActionForward");

  that cause the following actions whenever a <forward> element is found
  within an <action> element:

    -   Create a new object of the class specified by the "forwardClass"
        variable (normally "org.apache.struts.action.ActionForward")
        and push it on the digester's stack.
    -   On the new top stack element, map all the properties from the current
        XML element (the <forward>) into corresponding setXxxx calls.
    -   Call the addForward() method of the next-to-top element on the stack,
        passing the top element of the stack as an argument.  The method expects

        an argument of type org.apache.struts.action.ActionForward (or a
subclass
        of that class).
    -   Pop the top element (the ActionForward object) off the digester's stack.

In principle, you can do the same sort of thing for any arbitrarily nested set
of XML elements to define pretty much any configuration information that can be
statically represented as a tree of objects -- no loops or many-to-many
relationships, but pretty much anything else.  For a more complex example of
this same design approach, look at Tomcat's "conf/server.xml" file -- Tomcat
uses a very similar approach to configure itself based on the contents of a very
comprehensive configuration file.  Tomcat uses this to read the web.xml file
describing your application as well.

>  How about a new feature that
> allows you to specify the custom ActionMapping on a per action basis in the
> action.xml file.  For example:
>
>  <action path="/logon"
>    actionClass="org.apache.struts.example.LogonAction"
>    formAttribute="logonForm"
>    formClass="org.apache.struts.example.LogonForm"
>    inputForm="/logon.jsp"
>    actionMapping="my.custom.LogonActionMapping”
>    useDefaultHomePage=”true”>
>      <forward name="logon"      path="/logon.jsp"/>
>      <forward name="success"    path="/mainMenu.jsp"/>
>  </action>
>
> When parsed, the custom action mapping would be instantiated and used to
> store the custom ‘useDefaultHomePage’ attribute.

> Is this feasible?
>

A custom ActionMapping per action entry?  That seems like it might be reasonable
to make a standard feature of, falling back to the default one if you don't
override it.  I'll look at that scenario.

>
> David
>

Craig



RE: Struts--question about bean/form name.

Posted by David Chisholm <Da...@i2.com>.
Thanks for the idea.  My alternative solution is to define a custom
attribute that specifies a properties file, and then create a custom
ActionMapping to load the properties file.  The benefit of this is that it
keeps the action’s custom properties in the ActionMapping, but at the cost
of lots of small configuration files.

Perhaps I will combine the two ideas: A single extended configuration file
and a custom ActionMapping that extracts only its Action’s custom properties
from this file and stores them in the ActionMapping instance.

David


> -----Original Message-----
> From: jhut@inst.strykercorp.com [mailto:jhut@inst.strykercorp.com]
> Sent: Wednesday, June 21, 2000 9:01 AM
> To: struts-user@jakarta.apache.org
> Subject: RE: Struts--question about bean/form name.
>
>
> On Wed, 21 Jun 2000, David Chisholm wrote:
>
> > In this case though I would like to avoid a custom mapping class
> > that adds set/get() accessors for new attributes because the
> > attributes of the actions may vary widely.
>
> > The Actions I'm using in my existing framework often need static
> > initialization parameters to customize their processing.  So what I
> > do is load all non-standard attributes into a HashMap stored in the
> > Action's ActionConfig instance (equivalent of ActionMapping).  The
> > Action can then add as many custom parameters as it wants to the
> > action configuration file and fetch them by name from it's
> > ActionConfig HashMap at runtime.
>
> > My from I've read so far of the code, struts only uses one type of
> > ActionMapping and so it would have to have set/get() methods for all
> > of the custom attributes of all Actions.  This isn't practical if
> > you have lots of custom attributes that are unique to each Action.
>
> This type of general purpose Action configuration data may be outside of
> the scope of what ActionMapping was designed for.  In any case, you could
> put an instance of the HashMap containing Action configuration information
> in the servlet context.  For the key, you could concatenate the action
> class name with the configuration parameter name to accomodate any number
> of actions.  Just load the hashmap (from something like a properties or
> xml file) at application initialization time with a load-on-startup
> servlet class (cf. DatabaseServlet in the example).  A simple approach,
> but it might serve your needs.
>
> >
> > David
>
> -jh
>
> --
> Jeff Hutchison <jh...@inst.strykercorp.com>
> Stryker Instruments, Kalamazoo, MI
>


RE: Struts--question about bean/form name.

Posted by jh...@inst.strykercorp.com.
On Wed, 21 Jun 2000, David Chisholm wrote:

> In this case though I would like to avoid a custom mapping class
> that adds set/get() accessors for new attributes because the
> attributes of the actions may vary widely.
 
> The Actions I'm using in my existing framework often need static
> initialization parameters to customize their processing.  So what I
> do is load all non-standard attributes into a HashMap stored in the
> Action's ActionConfig instance (equivalent of ActionMapping).  The
> Action can then add as many custom parameters as it wants to the
> action configuration file and fetch them by name from it's
> ActionConfig HashMap at runtime.

> My from I've read so far of the code, struts only uses one type of
> ActionMapping and so it would have to have set/get() methods for all
> of the custom attributes of all Actions.  This isn't practical if
> you have lots of custom attributes that are unique to each Action.

This type of general purpose Action configuration data may be outside of
the scope of what ActionMapping was designed for.  In any case, you could
put an instance of the HashMap containing Action configuration information
in the servlet context.  For the key, you could concatenate the action
class name with the configuration parameter name to accomodate any number
of actions.  Just load the hashmap (from something like a properties or
xml file) at application initialization time with a load-on-startup
servlet class (cf. DatabaseServlet in the example).  A simple approach,
but it might serve your needs.
 
> 
> David

-jh

-- 
Jeff Hutchison <jh...@inst.strykercorp.com>
Stryker Instruments, Kalamazoo, MI


Re: Struts--question about bean/form name.

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
Kevin Duffey wrote:

> Hi,
>
> I would say..use the source, and modify the actual source in some way for
> your needs. The downside is..as Craig updates the builds..you'll have to
> keep modifying the code again and again. Unless..you just make a hook call
> from inside the Struts source. Then you simply add that call to each new
> build, and paste in your method that handles whatever it is you need. I
> built the source and added a bunch of out.printlns because I was having
> problems with JDK1.3 and Orion..thus how I learned about the JDK 1.3 bug
> with resource bundles.

For customizations that are less fragile, you can also subclass ActionServlet
and override the methods that do the specific functionality you want to
change.  For example, if you wanted to do something special before and after
the usual call to the action class, you could do this:

    public class MyActionServlet extends ActionServlet {

        protected void processActionInstance(ActionMapping mapping,
            ActionForm formInstance, HttpServletRequest request,
            HttpServletResponse response)
                throws IOException, ServletException {

            doMyPreprocessing();
            super.processActionInstance(mapping, formInstance,
              request, response);
            doMyPostprocessing();

        }

    }

which will survive changes in the underlying code as long as the method
signatures are not changed.

At the moment, we're still in a little bit of flux as we accumulate and
implement all the good ideas that have been suggested.  Once a 1.0 release
occurs, incompatible changes to the method signatures will be avoided if at
all possible, to maintain backwards compatibility of customizations like the
one above.

Craig McClanahan





RE: Struts--question about bean/form name.

Posted by Kevin Duffey <kd...@buymedia.com>.
Hi,

I would say..use the source, and modify the actual source in some way for
your needs. The downside is..as Craig updates the builds..you'll have to
keep modifying the code again and again. Unless..you just make a hook call
from inside the Struts source. Then you simply add that call to each new
build, and paste in your method that handles whatever it is you need. I
built the source and added a bunch of out.printlns because I was having
problems with JDK1.3 and Orion..thus how I learned about the JDK 1.3 bug
with resource bundles.



RE: Struts--question about bean/form name.

Posted by David Chisholm <Da...@i2.com>.
In this case though I would like to avoid a custom mapping class that adds
set/get() accessors for new attributes because the attributes of the actions
may vary widely.

The Actions I'm using in my existing framework often need static
initialization parameters to customize their processing.  So what I do is
load all non-standard attributes into a HashMap stored in the Action's
ActionConfig instance (equivalent of ActionMapping).  The Action can then
add as many custom parameters as it wants to the action configuration file
and fetch them by name from it's ActionConfig HashMap at runtime.

My from I've read so far of the code, struts only uses one type of
ActionMapping and so it would have to have set/get() methods for all of the
custom attributes of all Actions.  This isn't practical if you have lots of
custom attributes that are unique to each Action.

Can you create a custom ActionMapping class that can store arbitrary
information?  (This doesn't appear possible.)  How about a new feature that
allows you to specify the custom ActionMapping on a per action basis in the
action.xml file.  For example:

 <action path="/logon"
   actionClass="org.apache.struts.example.LogonAction"
   formAttribute="logonForm"
   formClass="org.apache.struts.example.LogonForm"
   inputForm="/logon.jsp"
   actionMapping="my.custom.LogonActionMapping”
   useDefaultHomePage=”true”>
     <forward name="logon"      path="/logon.jsp"/>
     <forward name="success"    path="/mainMenu.jsp"/>
 </action>

When parsed, the custom action mapping would be instantiated and used to
store the custom ‘useDefaultHomePage’ attribute.

Is this feasible?

David


> -----Original Message-----
> From: jhut@inst.strykercorp.com [mailto:jhut@inst.strykercorp.com]
> Sent: Wednesday, June 21, 2000 7:48 AM
> To: struts-user@jakarta.apache.org
> Subject: RE: Struts--question about bean/form name.
>
>
> On Wed, 21 Jun 2000, David Chisholm wrote:
>
> > On a similar note, can you associate non-standard attributes
> for an action
> > in the action.xml, and then retrieve those values in the
> Action.perform()
> > method?  For example, adding the useDefaultHomePage attribute
> shown below to
> > the example logon action:
> >
> >   <action path="/logon"
> >     actionClass="org.apache.struts.example.LogonAction"
> >     formAttribute="logonForm"
> >     formClass="org.apache.struts.example.LogonForm"
> >     inputForm="/logon.jsp">
> >     <forward name="logon"      path="/logon.jsp"/>
> >     <forward name="success"    path="/mainMenu.jsp"/>
> >     useDefaultHomePage="true"
> >   </action>
>
> You can provide a custom ApplicationMapping that contains properties
> corresponding to additional attributes to the action tag.  For example
> with your example (putting the attribute in the tag):
>
> <action path="/logon"
>   actionClass="org.apache.struts.example.LogonAction"
>   formAttribute="logonForm"
>   formClass="org.apache.struts.example.LogonForm"
>   inputForm="/logon.jsp"
>   useDefaultHomePage="true">
>     <forward name="logon"      path="/logon.jsp"/>
>     <forward name="success"    path="/mainMenu.jsp"/>
> </action>
>
> You can then extend ActionMappingBase to provide access to this new
> property:
>
> public final class MyApplicationMapping extends ActionMappingBase {
>
>     private boolean use_default_home_page = false;
>
>     public boolean getUseDefaultHomePage() { return
> use_default_home_page; }
>     public void setUseDefaultHomePage(boolean v)
> {use_default_home_page = v; }
> }
>
> Supply the class name for your custom mapping as a init parameter for
> ActionServlet (see ActionServlet API docs) and the Struts Digester class
> will then populate MyApplicationMapping from action.xml for you.  From
> within you Action perform() method, cast the passed in ActionMapping to
> MyActionMapping, and Bob's your uncle.
>
> The example used to show this for "success" and "failure" mappings but
> Craig replaced them with the (more elegant) forward tags.
>
> Hope this helps,
>
> -jh
>
>
> --
> Jeff Hutchison <jh...@inst.strykercorp.com>
> Stryker Instruments, Kalamazoo, MI
>


RE: Struts--question about bean/form name.

Posted by jh...@inst.strykercorp.com.
On Wed, 21 Jun 2000, David Chisholm wrote:

> On a similar note, can you associate non-standard attributes for an action
> in the action.xml, and then retrieve those values in the Action.perform()
> method?  For example, adding the useDefaultHomePage attribute shown below to
> the example logon action:
> 
>   <action path="/logon"
>     actionClass="org.apache.struts.example.LogonAction"
>     formAttribute="logonForm"
>     formClass="org.apache.struts.example.LogonForm"
>     inputForm="/logon.jsp">
>     <forward name="logon"      path="/logon.jsp"/>
>     <forward name="success"    path="/mainMenu.jsp"/>
>     useDefaultHomePage="true"
>   </action>

You can provide a custom ApplicationMapping that contains properties
corresponding to additional attributes to the action tag.  For example
with your example (putting the attribute in the tag):

<action path="/logon"
  actionClass="org.apache.struts.example.LogonAction"
  formAttribute="logonForm"
  formClass="org.apache.struts.example.LogonForm"
  inputForm="/logon.jsp"
  useDefaultHomePage="true">
    <forward name="logon"      path="/logon.jsp"/>
    <forward name="success"    path="/mainMenu.jsp"/>  
</action>

You can then extend ActionMappingBase to provide access to this new
property:

public final class MyApplicationMapping extends ActionMappingBase {

    private boolean use_default_home_page = false;

    public boolean getUseDefaultHomePage() { return use_default_home_page; }
    public void setUseDefaultHomePage(boolean v) {use_default_home_page = v; }
}

Supply the class name for your custom mapping as a init parameter for
ActionServlet (see ActionServlet API docs) and the Struts Digester class
will then populate MyApplicationMapping from action.xml for you.  From
within you Action perform() method, cast the passed in ActionMapping to
MyActionMapping, and Bob's your uncle.

The example used to show this for "success" and "failure" mappings but
Craig replaced them with the (more elegant) forward tags.

Hope this helps,

-jh
 

-- 
Jeff Hutchison <jh...@inst.strykercorp.com>
Stryker Instruments, Kalamazoo, MI


RE: Struts--question about bean/form name.

Posted by David Chisholm <Da...@i2.com>.
On a similar note, can you associate non-standard attributes for an action
in the action.xml, and then retrieve those values in the Action.perform()
method?  For example, adding the useDefaultHomePage attribute shown below to
the example logon action:

  <action path="/logon"
    actionClass="org.apache.struts.example.LogonAction"
    formAttribute="logonForm"
    formClass="org.apache.struts.example.LogonForm"
    inputForm="/logon.jsp">
    <forward name="logon"      path="/logon.jsp"/>
    <forward name="success"    path="/mainMenu.jsp"/>
    useDefaultHomePage="true"
  </action>


One suggestion, add a 'public void init(ActionMapping mapping)' method to
Action that is called when the Action is instantiated to allow the Action to
perform any static initialization that it needs, and a 'public void
destroy()' to allow it to do any cleanup.  ActionBase would then provide
default implementations of these methods.

David


> -----Original Message-----
> From: Kevin Duffey [mailto:kduffey@buymedia.com]
> Sent: Wednesday, June 21, 2000 1:09 AM
> To: struts-user@jakarta.apache.org
> Cc: struts-dev@jakarta.apache.org
> Subject: Struts--question about bean/form name.
>
>
> Hi,
>
> is there any way in each of my action perform() methods to get
> the "name" of
> the bean that was set in the action.xml, so that I can use that
> String name
> for various purposes? For example, we don't use EJB yet, so we
> separate our
> logic into "logic_session" classes. These session classes are
> stored in the
> HttpSession, with a reference to our bean, and I would like to
> store them by
> using the name of the bean (form passed in) and appending
> "Session" to that
> name. In the JSP page I have to hardcode it though..which means I
> still have
> to change the bean name in two places..if I change it for any reason.
>
> Thanks.
>


RE: Struts--question about bean/form name.

Posted by David Chisholm <Da...@i2.com>.
On a similar note, can you associate non-standard attributes for an action
in the action.xml, and then retrieve those values in the Action.perform()
method?  For example, adding the useDefaultHomePage attribute shown below to
the example logon action:

  <action path="/logon"
    actionClass="org.apache.struts.example.LogonAction"
    formAttribute="logonForm"
    formClass="org.apache.struts.example.LogonForm"
    inputForm="/logon.jsp">
    <forward name="logon"      path="/logon.jsp"/>
    <forward name="success"    path="/mainMenu.jsp"/>
    useDefaultHomePage="true"
  </action>


One suggestion, add a 'public void init(ActionMapping mapping)' method to
Action that is called when the Action is instantiated to allow the Action to
perform any static initialization that it needs, and a 'public void
destroy()' to allow it to do any cleanup.  ActionBase would then provide
default implementations of these methods.

David


> -----Original Message-----
> From: Kevin Duffey [mailto:kduffey@buymedia.com]
> Sent: Wednesday, June 21, 2000 1:09 AM
> To: struts-user@jakarta.apache.org
> Cc: struts-dev@jakarta.apache.org
> Subject: Struts--question about bean/form name.
>
>
> Hi,
>
> is there any way in each of my action perform() methods to get
> the "name" of
> the bean that was set in the action.xml, so that I can use that
> String name
> for various purposes? For example, we don't use EJB yet, so we
> separate our
> logic into "logic_session" classes. These session classes are
> stored in the
> HttpSession, with a reference to our bean, and I would like to
> store them by
> using the name of the bean (form passed in) and appending
> "Session" to that
> name. In the JSP page I have to hardcode it though..which means I
> still have
> to change the bean name in two places..if I change it for any reason.
>
> Thanks.
>