http://jakarta.apache.org/struts/api/org/apache/struts/action/Action.html

Actions have access to the Servlet which has access to servlet init parameters defined in web.xml. Alternatively, you can define application init parameters and retrieve them through the ServletContext.

David



From: "paul" <[EMAIL PROTECTED]>
Reply-To: <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Subject: Init params in struts Action objects
Date: Fri, 21 Mar 2003 10:28:14 -1000

Hi,

I sent this message out yesterday, but I was not a member of the mailing
list then, and I read on the jakarta.apache.org/struts website that I needed
to be a member in order to send questions out to
[EMAIL PROTECTED] I'm just letting everybody know just in case
they recevied this message twice.


I was searching the mailing list for ways to initialize Action classes
similar to the init() method for servlets and I found this:

---------------------------------------

On Sat, 7 Jul 2001, Martin Cooper wrote:

> You can override the setServlet() method to do this. That method is called
> with a non-null value when an instance is being created and initialised,
and
> then called again with a null value when the instance is about to be
cleaned
> up.
>


This is the right answer, but I want to add one little warning -- be
*sure* that you call the superclass method!

public void setServlet(ActionServlet servlet) {

    super.setServlet(servlet);
    ... your custom initialization ...

}

> --
> Martin Cooper
>

Craig

---------------------------------------

So now I know where to initialize my Action classes, but I had one more
question that I couldn't seem to find the answer to. When using servlets,
you can set init parameters in the applications web.xml file. Then when you
wanted to use those parameters you could do something like:

public void init() {
  ServletConfig config = getServletConfig();
  String initParam = config.getInitParameter("paramName");
}

Now how would I go about doing the same thing with Action classes? This is
what I was thinking I might be able to do:

class MyAction extends Action {
  private static String initParam;
  public void setServlet(ActionServlet servlet) {
    super.setServlet(servlet);
    // How do I get the config object?
    // servlet.getServletConfig() ???
    initParam = config.getInitParameter("paramName");
  }
}

But because the Action class won't be listed in my web.xml file, where would
the servlet configuration be? In other words, where would the init
parameters go? Does the struts Action class extend HttpServlet? Would I be
allowed to add it into my web.xml file without affecting struts like this?


<servlet>
  <servlet-name>myaction</servlet-name>
  <servlet-class>MyPackage.MyAction</servlet-name>
  <init-param>
    <param-name>paramName</param-name>
    <param-value>value of paramName</param-value>
  </init-param>
</servlet>

Thanks,
Paul





---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



_________________________________________________________________
Protect your PC - get McAfee.com VirusScan Online http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963



--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to